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 Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
13,303 questions
Remote Desktop
Remote Desktop
A Microsoft app that connects remotely to computers and to virtual apps and desktops.
4,617 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,565 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,640 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Marcin Policht 26,540 Reputation points MVP
    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,056 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.