kjacobsen icon

Restart stopped services which are set to automatic

kjacobsen | PRO | 04/01/12 08:28:36 AM UTC | 0 ⭐ | 295 👁️ | Never ⏰ | []
PowerShell |

860 B

|

None

|

0 👍

/

0 👎

# Code Snippet from aperturescience.su
# Parameters
 
[Parameter(Position=1, Mandatory=$false, HelpMessage="hostname(s)")]$hosts = "google.com",
 
# Body
 
#get the services which are auto and are stopped
 $enabledsvc = (Get-WmiObject "win32_service") | Where-Object {($_.startmode -eq "Auto") -and ($_.state -eq "stopped")}
 
#if we get some services
if ($enabledsvc -ne $null)
{
    foreach ($ensvc in $enabledsvc)
    {
        #set $fix to try if we want to restart services 
        if ($fix)
        { 
            Write-Verbose "$($a[0].displayname)"
            $ensvc.startservice()
            send-email "Service $($a[0].displayname) is not running and I tried to fix it" $false           
        } 
        else
        {
            send-email "Service $($a[0].displayname) is not running and I am not fixing it" $false
        }
    }
} 
else
{
    send-email "all services running" $true
}
 
# Code Snippet from aperturescience.su

Comments