Adding a hyperlink to an email generated through PowerShell

只愿长相守 提交于 2021-02-10 15:13:44

问题


I have a script that currently does several things. It creates a user account in Active Directory, writes data to a SQL table and sends an email to the account requestor with the account's user name and password.

We'd love to add a hyperlink to that email so that the requestor can click to view their original request form, but I can't quite seem to get the syntax right.

Because double quotes are used in the PowerShell syntax as well as the HTML link, I defined the link as a variable and inserted that variable into the -body section of the email to eliminate double quote confusion, though this may not be necessary.

Can anyone help me insert a link in this email? Many thanks!

CURRENT COPY:

"The user account you requested (request #$ReqID) has been created."

We'd like $ReqID to hyperlink to the Web form.

THE VARIABLE I'VE DEFINED:

$link = '<a href="http://tsturl/detail.aspx?reqID=$reqID">$ReqID</a>'

But it displays in the email body like this:

The user account you requested (request #<a href="http://tsturl/detail.aspx?reqID=$reqID">$ReqID</a>) has been created.

Help?


回答1:


Swap your quotes around, i.e.:

$link = "<a href='http://tsturl/detail.aspx?reqID=$reqID'>$ReqID</a>"

Or do:

$link = '<a href="http://tsturl/detail.aspx?reqID=$reqID">$ReqID</a>'
$link = $ExecutionContext.InvokeCommand.ExpandString($link)

Further to your comment, if you want the mail body to render as HTML, an thus display a link, then you'll need to tell your mail client that the body is HTML. $link is just a plain old string and doesn't know that it's HTML.

From your previous question, I'm guessing you're using the Send-MailMessage cmdlet. If so then you need to specify the -BodyAsHtml switch.



来源:https://stackoverflow.com/questions/28706312/adding-a-hyperlink-to-an-email-generated-through-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!