Sunday 6 March 2016

OBI PROJECT

$servers = @("server1","Server2")

foreach($server in $servers){

write-host "Currently Working on Server $server"

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


write-host "Process Exist and we are executing the schedule task on  $server"


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


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


write-host "Still process exist  on Server $server"

if($procexist){




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

write-host "in Iteration Loop for the $i time on $server"
sleep 10

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

{

write-host "Still Process exist on  $server sending mail"





}

else{break}
}

}
else{

}



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

if($procexist){
write-host "Still process exist  on Server $server"
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 $server" -SmtpServer "dc.superdc.com"
exit
}
else{
write-host "Restarting Server $server"
Restart-Computer -ComputerName $server -Force
}



} else{

write-host "There is no process exist on "$server". Directly rebooting it."

Restart-Computer -ComputerName $server -Force

}


}

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...