Of course I cannot reproduce your error because I don’t have your environment and I don’t even have all the code you’re using.
In your situation I’d try the following:
Instead of using variable substitution in strings I’d try to use the format operator -f.
So instead of
$Body =
@"
<p>Dear $($AdManagerobj.GivenName),<br>
<p><br>
<p>The password of your admin account $($Obj.Name), has expired. Therefore you will no longer be able to use this account to log on.<br>
<p>You can reset the password via $($link), by logging in with your admin credentials.<br>
"@
I’d try this:
$Body =
@'
<p>Dear {0},<br>
<p><br>
<p>The password of your admin account {1}, has expired. Therefore you will no longer be able to use this account to log on.<br>
<p>You can reset the password via {2}, by logging in with your admin credentials.<br>
'@ -f $($AdManagerobj.GivenName), $($Obj.Name), $($link)
If that does not help I’d try to put quotes around the variable name when called … so instead of
$Params = @{
Message = @{
Subject = $MesTitle
Body = @{
ContentType = $type
Content = $body
}
ToRecipients = @(
@{
EmailAddress = @{
Address = $recipient
}
}
)
}
SaveToSentItems = $save
}
I’d call it like this:
$Params = @{
Message = @{
Subject = $MesTitle
Body = @{
ContentType = $type
Content = "$body"
}
ToRecipients = @(
@{
EmailAddress = @{
Address = $recipient
}
}
)
}
SaveToSentItems = $save
}
Besides this I’d recommend considering refactoring your script to get rid of some repetitive code. I’d probably even “outsource” the HTML fragments into external files. For example PowerShell data files. This way you don’t have to deal with “non PowerShell code” inside your PowerShell scripts.