CodexBloom - Programming Q&A Platform

PowerShell 7.3 - guide with Retrieving Service Status from Multiple Remote Servers

👀 Views: 72 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
PowerShell Get-Service RemoteManagement

I'm stuck on something that should probably be simple. I'm trying to retrieve the status of a specific Windows service from multiple remote servers using PowerShell 7.3, but I'm working with issues with the `Get-Service` cmdlet when dealing with multiple concurrent requests. I have a list of server names in a text file and I'm using a simple foreach loop to iterate through them. Here's the code I wrote: ```powershell $servers = Get-Content -Path 'C:\servers.txt' $serviceName = 'wuauserv' foreach ($server in $servers) { try { $service = Get-Service -Name $serviceName -ComputerName $server -ErrorAction Stop Write-Output "Service '$serviceName' on '$server' is '$($service.Status)'." } catch { Write-behavior "Failed to retrieve service from '$server': $_" } } ``` I expected to get the status of the service on each specified server, but I often receive the following behavior message: ``` Get-Service : want to find any service with service name 'wuauserv' on computer 'RemoteServerName'. ``` This only occurs intermittently, and it seems to be more frequent with servers that are under heavy load. I've tried increasing the timeout with `-Timeout` parameter in the `Get-Service` cmdlet, but it's still not resolving the scenario. Additionally, I've checked that the service does exist on all servers, and I can manually check their status with no problems. Is there a better way to retrieve the service status concurrently without running into these intermittent issues? Any suggestions or workarounds would be appreciated. I'm working on a CLI tool that needs to handle this. Is there a better approach?