【问题标题】:How can I concatenate a string with a link that has spaces in powershell如何将字符串与powershell中包含空格的链接连接起来
【发布时间】:2013-04-13 06:16:53
【问题描述】:

这是我的代码,用于检查用户密码何时到期,如果密码将在 14 天内到期,则通过电子邮件发送给他们。由于电子邮件正文中的链接包含空格,因此该链接不包含整个文件名。

#Add the Quest PowerShell snapin
Add-PsSnapIn Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue

#Clear the placeholder log file
Clear-Content O:\logs\network\passwordchangeemails.txt

#Set Email Variables
$today = Get-Date
$logdate = Get-Date -format yyyyMMdd
$emailFrom = "my.name@mycompany.com"
$body = "Good Morning, `n`n"
$body += "As a courtesy reminder, your Company password will be expiring soon. `n`n"
$body += "If you allow your password to expire, you will be unable to access our network, mail or QAD until you recieve assistance from the Helpdesk. `n`n"
**$body += "To avoid this, please change your network password as soon as possible. (Remote users, please follow the password change procedure for remote users. Click Here -> " + "\\mydfsshare.net\share\helpdesk\guides\Passwords - Change procedure for remote users.pdf ) `n`n"**
$body += "Feel free to contact the Help Desk by phone at 555-555-5555 or by email at Helpdesk@mycompany.com `n"
$body += "Thanks!"

#Get Active Directory Information
Get-QADUser -SizeLimit 0 | Select-Object samAccountName,mail,PasswordStatus | 
Where-Object {$_.PasswordStatus -ne "Password never expires" -and $_.PasswordStatus -ne "Expired" -and $_.PasswordStatus -ne "User must change password at next logon." -and $_.mail -ne $null} | 

#For each user, get variables
ForEach-Object {  
  $samaccountname = $_.samAccountName
  $mail = $_.mail 
  $passwordstatus = $_.PasswordStatus
  $passwordexpiry = $passwordstatus.Replace("Expires at: ","")
  $passwordexpirydate = Get-Date $passwordexpiry
  $daystoexpiry = ($passwordexpirydate - $today).Days

      #If days to expire is lessthan 14 days, send email to user
    if ($daystoexpiry -lt 14) {
    $emailTo = "$mail"
    $subject = "Company IT Notification: Your Network password will expire in $daystoexpiry day(s). "    
    Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -Body $body -SmtpServer 192.168.1.191
    Write-Host "Email was sent to $mail on $today"
    Add-Content O:\logs\network\passwordchangeemails.txt  "Email was sent to $mail on $today"
  }
}
$recipients = "my.name@mycompany.com"
#Copy contents of log file to email and send to IT Department
$content = [IO.File]::ReadAllText("O:\logs\network\passwordchangeemails.txt")
Send-MailMessage -To $recipients -From "my.name@mycompany.com" -Subject "Password change log for $today" -Body "This is the log from $today, $content" -SmtpServer 111.111.111.111

【问题讨论】:

  • 即使链接在该行的第一个字符串中没有连接也可以。

标签: powershell hyperlink concatenation string-concatenation


【解决方案1】:

抱歉,我现在无法测试,但如果 HTML 可以使用,请尝试按如下方式包装链接...

"<a href=""file://///mydfsshare.net/share/helpdesk/guides/Passwords - Change procedure for remote users.pdf"">Text you want the users to see goes here</a><br>`n

然后在 Send-Mailmessage 上设置 -BodyAsHtml:

Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -Body $body -SmtpServer 192.168.1.191 -BodyAsHtml

【讨论】:

  • 很高兴知道,谢谢...&不客气,很高兴能帮上忙!
猜你喜欢
  • 2018-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
相关资源
最近更新 更多