Powershell to remotely check server services and start if off.

Jose P 21 Reputation points
2024-11-22T22:04:58.6533333+00:00

Bluff:
Looking for a PowerShell script that will do the following:

  1. Check that multiple servers are available
  2. Remote into each server and check specified services
  3. Turn those services on if they are not running

Current script attempt below, but I receive an error at the Start-Service area that "Cannot bind argument to parameter -Name because it's null."

Thank you everyone for your help.

$ServerName = Get-content C:\User\desktop\ServerList
$ServiceList = @("Service1", "Service2")
for each ($Server in $ServerName) {
 if (Test-Connection -ComputerName $Server -Quiet) {
  foreach ($Service in $ServiceList) {
   $ServiceStatus = Get-Service -ComputerName $Server -Name $Service
    if ($ServiceStatus.Status -ne "Running") {
     Write-Host "Service $Service in Server $Server is not running. Attempting to start.."
     Invoke-Command -ComputerName $Server -ScriptBlock {
       Start-Service -Name $Service
   }
  }
 }
} else {
Write-Host "Server $Server is not reachable"
}
Windows for business Windows Client for IT Pros User experience Remote desktop services and terminal services
Windows for business Windows Server User experience PowerShell
Windows for business Windows Server User experience Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Marcin Policht 49,640 Reputation points MVP Volunteer Moderator
    2024-11-22T22:21:36.96+00:00

    Try the following

    # Define list of server names and services
    $ServerName = Get-Content "C:\User\desktop\ServerList"
    $ServiceList = @("Service1", "Service2")
    foreach ($Server in $ServerName) {
        # Check if the server is reachable
        if (Test-Connection -ComputerName $Server -Quiet) {
            foreach ($Service in $ServiceList) {
                try {
                    # Check the status of the service on the remote server
                    $ServiceStatus = Get-Service -ComputerName $Server -Name $Service
                    # If the service is not running, attempt to start it
                    if ($ServiceStatus.Status -ne "Running") {
                        Write-Host "Service $Service on Server $Server is not running. Attempting to start..."
                        # Start the service remotely using Invoke-Command
                        Invoke-Command -ComputerName $Server -ScriptBlock {
                            param($ServiceName)
                            # Start the service
                            Start-Service -Name $ServiceName
                        } -ArgumentList $Service
                    } else {
                        Write-Host "Service $Service on Server $Server is already running."
                    }
                } catch {
                    Write-Host "Error checking or starting service $Service on Server $Server: $_"
                }
            }
        } else {
            Write-Host "Server $Server is not reachable."
        }
    }
    
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2024-11-22T22:40:21.05+00:00

    The variable $Service doesn't exist on the remote machine.

    Change this: Start-Service -Name $Service to this: Start-Service -Name $using:Service.

    The effect is to parameterize the contents of the variable inside the script block using the local variable on the source machine.

    You should note that the Get-Service -ComputerName does NOT work in PowerShell 7. You should check the status of the services in the script block of the Invoke-Command if you want forward compatibility. That would also decrease runtime in PS5 by not making (and tearing down) as many connections to each remote machine.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.