Restart Computer -email using powershell

I would like to restart the server using powershell and also want to know whether or not the reboot was successful.

I have created the powershell script:

Restart-Computer -ComputerName Qyhz –Force  -ErrorVariable status

if ($status.fullyqualifiederrorid -like "*Failed*")

{

Write-Log -Message 'server restart failed.' -Path c:\temp\test.log

}

else

{

Write-Log -Message 'server restart succeeded.' -Path c:\temp\test.log

}

send -mailmessage -from operating.system@abc.com -To asdf@abc.com -subject test -attachment 'c:\Temp|test.log' -smtp server "127.0.0.1"

Can any one suggest me how to send a status email?

In this case, Try/Catch/Finally may work for you. In your Restart-Computer command, instead of the -ErrorVariable parameter, use -ErrorAction Stop to catch the error and email it.

pwshliquori

You will only know if the reboot is successful, after the machine restarts. This script would not be in play on a reboot, so there is nothing to get from it.

You need a scheduled task to fire on startup and send the email. No real reason to write to or read a log file.