Monday, 7 March 2016

How to run schedule task if a particular processes is running and monitor it until it closes and send mail

$SERVER1= "SERVER1"
$SERVER2="SERVER2"

write-host "Currently Working on Server $server1"

$procexist  = Get-WmiObject -Class win32_process -ComputerName $SERVER1 | where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS

if($procexist){


schtasks /run /s "$server1" /TN servicecheck


for($i=0;$i -le 3$i++){

write-host "in Iteration Loop for the $i time on $SERVER1"
sleep 4

$procexist  = Get-WmiObject -Class win32_process -ComputerName $SERVER1| where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS
if($procexist)

{

write-host "Still Process exist on  $SERVER1"


}


else{break}
}



}

else{

}


$procexist  = Get-WmiObject -Class win32_process -ComputerName $SERVER1| where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS

if($procexist){
write-host "Still process exist  on Server $SERVER2"
write-host "Sending Mail and Exiting"
Send-MailMessage -to "administrator@superdc.com" -From "DG_ADMINS@superdc.com" -Subject "action required" -Body "Notepad is still running your attention is required on $server1" -SmtpServer "dc.superdc.com"
exit
}
else{

write-host "NOTEPAD IS NOT RUNNING"
write-host "Restarting Server $SERVER1"
#Restart-Computer -ComputerName $server -Force
}



write-host "Currently Working on Server $server2"

$procexist  = Get-WmiObject -Class win32_process -ComputerName $SERVER2 | where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS

if($procexist){


schtasks /run /s "$server2" /TN servicecheck

Write-Host "notepad is running on $SERVER2"

Start-Sleep 10


for($i=0;$i -le 3$i++){

write-host "in Iteration Loop for the $i time on $SERVER2"
sleep 4

$procexist  = Get-WmiObject -Class win32_process -ComputerName $SERVER2| where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS
if($procexist)

{

write-host "Still Process exist on  $SERVER2"


}


else{break}
}



}

else{

}


$procexist  = Get-WmiObject -Class win32_process -ComputerName $SERVER2| where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS

if($procexist){
write-host "Still process exist  on Server $SERVER2"
write-host "Sending Mail and Exiting"
Send-MailMessage -to "administrator@superdc.com" -From "DG_ADMINS@superdc.com" -Subject "action required" -Body "Notepad is still running your attention is required on $server2" -SmtpServer "dc.superdc.com"
exit
}
else{

write-host "NOTEPAD IS NOT RUNNING"
write-host "Restarting Server $SERVER2"
#Restart-Computer -ComputerName $server -Force
}



Start-Sleep 20

Send-MailMessage -to "administrator@superdc.com" -From "DG_ADMINS@superdc.com" -Subject "OBI Reboots completed" -Body "Obi task has completed" -SmtpServer "dc.superdc.com"
exit


No comments:

Post a Comment

Powershell function to get theremote server IP details,Subnetmask,Gateway,DHCP Enabled status,DNS Servers,Wins and Macaddress

#Powershell function to get theremote server IP details,Subnetmask,Gateway,DHCP Enabled status,DNS Servers,Wins and Macaddress # use...