如何将字符串与PowerShell中包含空格的链接连接起来

时间:2013-04-19 18:54:03

标签: powershell hyperlink concatenation string-concatenation

以下是我的代码,用于检查用户密码何时过期,如果在不到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

1 个答案:

答案 0 :(得分: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