Link to home
Start Free TrialLog in
Avatar of jimmylew52
jimmylew52Flag for United States of America

asked on

Put hostname in email in powershell script

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")`
}
ASKER CERTIFIED SOLUTION
Avatar of Spyder2010
Spyder2010

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jimmylew52

ASKER

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'
FYI, if you used double quotes instead of single, your original code would work without concatentation.  Variables aren't expanded inside single quotes.