2
0
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.