ColleenH218
ColleenH218

Reputation: 35

Adding a hyperlink to an email generated through PowerShell

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?

Upvotes: 1

Views: 23943

Answers (1)

Kev
Kev

Reputation: 119826

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.

Upvotes: 3

Related Questions