I'm still learning PowerShell as I go along, so some of this may be fairly obvious to others. I'm trying to create a script that takes a list of server names from a separate .txt file, then runs a Get-Service to list out the status of several services. I want to add the actual server names back into the output so it's easier for us to identify which server has the issue we need to resolve.
Current script is something like this:
$appID = $appServers | ForEach-Object {
Get-Service "AppIDSvc" -ComputerName $_
}
Write-Host "Application Identity Service Status - App Servers"
$appID.Status
Text file just has a list of server names, so the output lists 4 lines that say:
Status | Name | DisplayName |
---|---|---|
Running | AppIDSvc | AppIDSvc |
Running | AppIDSvc | AppIDSvc |
Stopped | AppIDSvc | AppIDSvc |
Running | AppIDSvc | AppIDSvc |
I'd like to have $appID.Status go from looking like
Running
Running
Stopped
Running
to
Server1 - Running
Server2 - Running
Server3 - Stopped
Server4 - Running
Is this something I can do and have the correct server names match their results, so that we can properly identify the servers that are having issues?
I have tried to isolate the result set from the Status, Name, and DisplayName of the services to just the status, then enumerate the array next to the result set. I'm not having luck doing so and I don't feel confident I can match the server in the list to the output order of the results.
CodePudding user response:
Following sample from your code uses Add-Member
to add computer name to output
$appID = $appServers | ForEach-Object {
$service = Get-Service "AppIDSvc" -ComputerName $_
Add-Member -PassThru -InputObject $service -Type NoteProperty -Name Server -Value $_
}
Write-Host "Application Identity Service Status - App Servers"
$appID | Select-Object Server, Status