Tuesday 15 March 2016

How to automate and start w3svc service if it is in stopped stated and send mail

$a = "<style>"
$a = $a + "BODY{background-color:Green;}"
$a = $a + "TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 2px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 2;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "</style>"
$servers =Get-Content -Path C:\serverslist.txt

$servicestateok =Get-WmiObject -Class win32_service -ComputerName $servers | Where-Object {$_.name -like "w3svc"} | ?  {$_.state -match "Running"} | Select-Object __SERVER,DisplayName,StaTE | FT -AutoSize

$servicestatentok =Get-WmiObject -Class win32_service -ComputerName $servers | Where-Object {$_.name -like "w3svc"} | ?  {$_.state -match "stopped"} | Select-Object __SERVER,DisplayName,StaTE | FT -AutoSize


foreach ($server in $servers)

{}

if ($servicestateok)

{



 Write-Host "service is running no actvion is required"



}


else

{



{

Write-Host "service is not ok hence starting it"

Write-Host "currently working on $servers"



Start-Service -inputobject $(get-service -ComputerName $servers -Name w3svc)

Start-Sleep 15

$SER=Get-WmiObject -Class win32_service -ComputerName $servers | Where-Object {$_.name -like "w3svc"} | ?  {$_.state -match "Running"} | Select-Object __SERVER,DisplayName,State  | ConvertTo-Html -head $a


Send-MailMessage -to "administrator@superdc.com" -From "DG_ADMINS@superdc.com" -Subject "Service status"   -SmtpServer "dc.superdc.com" -BodyAsHtml "$SER"



}
}



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


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

}


}

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