Solved

Put hostname in email in powershell script

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

21 Experts available now in Live!

Get 1:1 Help Now