Trying to pass variables to Send-MailMessage arguments and failing miserably

13,309

Solution 1

So if I'm reading the script (and understanding your description) correctly, you have two variables, $alertSubject and $selHost

Both of these variables are populated out of text files (config.txt and hosts.txt respectively.)

I'm assuming the line of the config.txt which ends up in the subject variable is literally typed out as: $selHost is offline!

The problem is that when PowerShell reads that file in, it's treating the string as, literally, a string. There's not Varibale Expansion going on there.

You have two options to resolve this easily:

Option 1: Declare the subject after getting the host name

You could do something like this:

$selHost = (GET-CONTENT $runPath\hosts.txt)[$it]
$alertSubject = "$selHost is offline!"

This would expand the variable and you'd end up with the output you're expecting.

Alternatively, if you still want to get the bulk of the subject from your config.txt file, I'd suggest changing it from "$selHost is offline!" to " is offline!" (notice the space at the start.)

The you could write it out as:

$alertSubject = (get-content $runPath\config.txt)[1] # You can leave this on line 10
# ...
$selHost = (GET-CONTENT $runPath\hosts.txt)[$it]
$alertSubjectSend = $selHost + $alertSubject # Use this 'send' variable in the Send-MailMessage cmdlet call

This will combine the two strings into one.

Option 2: Use the replace method

Send-MailMessage -To $alertRecipient -From $alertSender -Subject ($alertSubject.Repalce('$selHost', $selHost)) -Body $alertBody -SmtpServer $smtpServer

In closer focus: -Subject ($alertSubject.Repalce('$selHost', $selHost))

Notice, we're looking to replace '$selHost' (the literal string, the single quotes are important so that the variable doesn't expand). We want to replace it with $selHost (the value stored in the variable, no quotes so it expands though you could use double quotes if you wanted to.)

The whole thing is wrapped in brackets, so that that little chunck evaluates first and the result gets passed to the cmdlet.

You could also go with the 'saving it into a temp variable; method too:

$alertSubjectSend = $alertSubject.Repalce('$selHost', $selHost)

Solution 2

this is what I use in one of my scripts. Try taking the variables out of the quotes.

Send-MailMessage -From $From -To $To -Subject $Subject -SmtpServer $SMTPServer -BodyAsHtml

Also, what version of powershell are you running as I think Send-MailMessage is v2 and above.

$PSVersionTable.PSVersion
Share:
13,309

Related videos on Youtube

Catatonic27
Author by

Catatonic27

Updated on September 18, 2022

Comments

  • Catatonic27
    Catatonic27 almost 2 years

    So I have a problem.

    I’m trying to write a scripts that pings a list of hostnames in an external .txt file, and send an email alert if one of them goes down. I’m having a few problems with it, but the main one right now, is that I can’t find a way to insert a variable into the subject line or body of the message. The code I’m using to send to email alert now looks like this:

    send-mailmessage -to "$alertRecipient" -from "$alertSender" -subject "$alertSubject" -body "$alertBody" -smtpServer "$smtpServer"
    

    Now, I’ve used a lot of variable values in that command, and they all work to some extent. I’m mostly looking at $alertSubject and $alertBody. The values of both of those variables are pulled out of another .txt file and are both equal to “$selHost is offline!”.

    $selHost is the variable I use in the script to be equal to the hostname the script is working on at any given time. If I do write-host $selHost pretty much anywhere in the script, I’ll get something like “SomeComputerName” or “192.168.1.123” and its value changes every time the script switches to the next host.

    I want the literal value of $selHost to be in the message body of the alert when it gets sent out, but the way I have it written, when the alert gets sent it just says “$selHost is offline!” giving me a completely unhelpful level of insight into the nature of the potential problem.

    Pastebin link of my script as it is now.

    Send-Mail-Message command on line 30, $selHost defined on line 23, $alertSubject defined on line 10, $alertBody defined on line 12.

    TL;DR: I want to embed the value of a variable into the arguments of the Send-MailMessage command, but I’m just getting the name of the variable instead and I don’t know what to do.