Skip to content

Instantly share code, notes, and snippets.

@SteloNLD
Last active March 1, 2024 07:01
Show Gist options
  • Save SteloNLD/6ac0d91cbe0dfd06aba3da30d2049604 to your computer and use it in GitHub Desktop.
Save SteloNLD/6ac0d91cbe0dfd06aba3da30d2049604 to your computer and use it in GitHub Desktop.
Powershell, get status from multiple services and computers.
cls
#Services are declared here, but you can of course load them from something like a CSV file.
$Servicelist = @("WinRM", "Winmgmt", "DHCP")
#Computers are declared here, but you can of course load them from something like a CSV file.
$Computerlist = @("LocalHost", "Server1","Server2","Server3")
#HashTable, wil store the services with running status for each server,
$ServiceStatus = @{}
#Computer loop
Foreach ($Computer in $Computerlist)
{
#Retrieve al services from computer and extract specified services.
$ServicesFound = Get-Service -ComputerName $Computer | Where-Object {$Servicelist -contains $_.Name} | Select Name, Status
#Loop trough the found services.
Foreach ($Service in $ServicesFound)
{
#If service is not yet declared in the hashtable, then do so and add the computer status.
If(!($ServiceStatus["$($Service.Name)"])) {
$ServiceStatus["$($Service.Name)"] = $Service | Select @{N='Service'; E={$($_.Name)}}, @{N="$Computer"; E={$_.Status}}
}
#If service is already declared in the hashtable, then only add the current computer status to it.
else {
$ServiceStatus["$($Service.Name)"] = $ServiceStatus["$($Service.Name)"] | Select *, @{N="$Computer"; E={$Service.Status}}
}
}
}
# Option A Export as HTML
$ServiceStatus.Values | ConvertTo-Html -Fragment
# Option B Export as Objects
$ServiceStatus.Values
# Option C Show GridView
$ServiceStatus.Values | Out-GridView
@SteloNLD
Copy link
Author

Hello, I have tried your code, but there is some issue , for example if for local host service WinRM is Stopped its showing for all services that is stopped.

Hi, if you look at line 17 you see that the part <#-ComputerName $Computer#> is a comment, if you replace this with -ComputerName $Computer then it should work, i had this added for testing purposes i will update the gist, thank you for noticing.

@KanyETL
Copy link

KanyETL commented Mar 1, 2024

Thank you, now its work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment