1

I wrote a script to tell me if the SEP Master Service is running for all computer listed in a file, start the service if its stopped, and let me know if it doesn't exist. It seems to work with starting the service, but i'm having issues seeing the output for the status on each computer. If the service is running on each computer in the list, it'll show me the output fine. If one of the computer doesn't have the service running, it is started, but i don't see the write-output message from the first if statement, for the computers that already had the service running. What i'll like to see is the output showing the running status for all machines with the service running, when the services is started on machines where it was stopped, and would like to see the message telling me what machines the service is not on.

$computers = Get-Content -Path "C:\temp2\generic_service2.bat"
$serivce = Get-Service -name SepMasterService -computername $computer
foreach ($computer in $computers) {

    $ServiceStatus = $serivce.Status
    $ServiceDisplayName = $serivce.DisplayName

    if ($ServiceStatus -eq 'Running') {
        Write-Output "Service OK - Status of $ServiceDisplayName is $ServiceStatus on $computer"
    }
    elseif ($ServiceStatus -eq 'stopped') {
        Start-Service -Name SepMasterService -PassThru
    }
    else {
        Write-Output "Service doesn't exist"
    }
}
0

1 Answer 1

1

Your variable on line2 is misspelled as $serivce. Although this does not affect the workings of your script, it is best practice to not leave things like that in your code. It totally asks for someone else to come along and "fix" it in only one place, and then your script breaks.

Also, I'm guessing that line belongs inside the foreach and not outside?

Because of the order of your statements, you will only see the Service OK message if the service is running when you check. If you start it in the elseif, it was not started when you tested it, so it is expected from your code that you will not see anything written to the screen if you have to restart it. If you want something written to the screen when you restart, add it after the Start-Service like this:

if ($ServiceStatus -eq 'Running') {
    Write-Output "Service OK - Status of $ServiceDisplayName is $ServiceStatus on $computer"
}
elseif ($ServiceStatus -eq 'stopped') {
    Start-Service -Name SepMasterService -PassThru
    Write-Output "Whatever you want to see when you restart the service"
}
else {
    Write-Output "Service doesn't exist"
}
Sign up to request clarification or add additional context in comments.

2 Comments

That worked. Any suggestions on how i could change the script to only check for the service on computers in the list that are online? I'm getting slow results whenever a computer on the list is not pingable on the network.
You can always use a Test-Connection -ComputerName $ComputerName -Count 1 first and only do your service checks if that succeeds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.