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
Solved

Put hostname in email in powershell script

Posted on 2014-02-24
4
1,974 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 40

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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