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"



}
}



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