Solved

Put hostname in email in powershell script

Posted on 2014-02-24
4
1,920 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
4 Comments
 
LVL 6

Accepted Solution

by:
Spyder2010 earned 250 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 250 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 39

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

816 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now