Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Start Automatic Services on Remote machines if stopped

$
0
0

Here’s a fairly straightforward way of outputting the errors if the StartService method failed. It doesn’t take any action, just displays the error on screen. The WMI methods return Win32 error codes, so you can translate them into useful error messages via the Win32Exception class.

Get-WmiObject win32_service -Filter "startmode = 'auto' and state != 'running'" -computername (Get-Content C:\ServerList.txt) |
ForEach-Object {
    $result = $_.StartService()
    if ($result.ReturnValue -ne 0)
    {
        $exception = New-Object System.ComponentModel.Win32Exception([int]$result.ReturnValue)
        Write-Host -ForegroundColor Red "Server: $($_.__SERVER), Error code: $($result.ReturnValue), Message: $($exception.Message)"
    }
}

Viewing all articles
Browse latest Browse all 13067

Trending Articles