Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Put hostname in email in powershell script

Posted on 2014-02-24
4
Medium Priority
?
2,320 Views
Last Modified: 2014-02-24
I have a script what is working to send email from a powershell script but I need it get the local host's name and include it in the email. $Server name should be the local host name. Here is what I have that is not working.

$serviceName = " Application Server";
$Servername = $env:computername
$serviceStatus = (get-service "$serviceName").Status;

if ($serviceStatus -ne "Running") {
    Send-MailMessage -From '$Servername_ServiceCheck@Company.com' -To 'me@company.com' `
  -SmtpServer 'smtp.company.com' `
  -Subject 'The Application Server Service is Not Running (PS Script)' `
  -Body  $("The Application Server Service is Not Running:   Status is $serviceStatus `n`
             Please Check the service and restart if necessary")`
}
0
Comment
Question by:jimmylew52
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 6

Accepted Solution

by:
Spyder2010 earned 1000 total points
ID: 39882537
You could try concatenating the From: email address into another variable:

$serviceName = " Application Server";
$Servername = $env:computername
$serviceStatus = (get-service "$serviceName").Status;
$email = $Servername + '_ServiceCheck@Company.com'

if ($serviceStatus -ne "Running") {
    Send-MailMessage -From $email -To 'me@company.com' `
  -SmtpServer 'smtp.company.com' `
  -Subject 'The Application Server Service is Not Running (PS Script)' `
  -Body  $("The Application Server Service is Not Running:   Status is $serviceStatus `n`
             Please Check the service and restart if necessary")`
}
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 1000 total points
ID: 39882552
Try to include the server name in the subject line..
-Subject "The Application Server Service is Not Running on Server $Servername (PS Script)" `

Open in new window

0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39882680
Thank You for your input. Neither suggestion worked but they were helpful in finding the solution.

Replaced

 Send-MailMessage -From '$Servername_ServiceCheck@Company.com' -To 'me@company.com' `

with

  Send-MailMessage -From $servername'_ServiceCheck@company.com' -To 'me@company.com'
0
 
LVL 41

Expert Comment

by:footech
ID: 39883164
FYI, if you used double quotes instead of single, your original code would work without concatentation.  Variables aren't expanded inside single quotes.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Loops Section Overview
Screencast - Getting to Know the Pipeline

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question