Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Script to Reboot hyper-v virtual machine with email notification

$
0
0
Hello, I have been playing with this all day today but cannot seem to get it to do what I want. I want to restart 4-6 Virtual machines housed in hyper-v using a script. Then I want to send out an email letting myself know it was successful. Here is what I have been trying to work with but no matter what I have changed I cant seem to get it to work.
Import-Module Hyper-v
$VM = "server1, server2, server3, server4"
Stop-VM -name $VM
start-sleep -s 240
$getVM = get-vm $vm
if ($getvm.Enabledstate -eq "3"){
$date = Get-date
start-VM -name $vm
}
start-sleep -s 60
$getVm2 = Get-VM -VM $vm -server HYPERVHOST
if ($getVM2.enabledstate -eq "2") {
$messageParameters = @{
                Subject = "$VM reboot status for $((Get-Date).ToShortDateString())"
                Body = " The Server $vm has rebooted at - $date. The server is up and operational."
                From = "noreply@hotmail.com"
                To = "asdf@hotmail.com.com"
                SmtpServer = "SMTPSERVER" }
send-mailmessage @messageparameters -bodyashtml
}
if ($getVM2.enabledstate -ne "3") {
$messageParameters2 = @{
                Subject = "$VM reboot status for $((Get-Date).ToShortDateString())"
                Body = " The Server $vm has rebooted at  - $date. Please check the vm $vm as there may be an issue. It has not fully restarted"
                From = "noreply@hotmail.com"
                To = "asdf@hotmail.com"
                SmtpServer = "SMTPSERVER" }
send-mailmessage @messageparameters2 -bodyashtml
}

 

Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>