Author Posts

February 8, 2018 at 4:00 pm

I run:

$server = Get-Content -path C:\ServerList.txt
$server | foreach { (Get-Service -Name WinRM* -computername $_) | Where {$_.Status -eq "Stopped"}
Select-Object Status, Name, DisplayName |
ConvertTo-CSV | Out-File "C:\winrmstatus.csv"}

Not getting what I want in csv, it is blank.

February 8, 2018 at 6:49 pm

Are you sure the service is stopped? If you query a specific service and it's started, your where command isn't going to return anything.

February 8, 2018 at 7:25 pm

There is only one WinRM service, so the WinRM* is not really a thing to use.

Note, if it won't print to the screen, it won't go to a file.
Try it this way instead.

     (Get-ADComputer -Filter *).Name | %{
     Get-Service -Name WinRM -ComputerName $_ `
     | Select MachineName,Status,Name,DisplayName `
     | Where Status -eq 'Running'
     } | Format-Table -AutoSize


    MachineName   Status Name  DisplayName                              
    -----------   ------ ----  -----------                              
    DC01     Running WinRM Windows Remote Management (WS-Management)
    EX01     Running WinRM Windows Remote Management (WS-Management)
    SQL01    Running WinRM Windows Remote Management (WS-Management)
    ...

February 10, 2018 at 11:31 am

That should be

(Get-ADComputer -Filter *).Name | 
 foreach {
     Get-Service -Name WinRM -ComputerName $_ | 
     Select MachineName,Status,Name,DisplayName | 
     Where Status -eq 'Running'
     } | Format-Table -AutoSize

Never use % as an alias in scripts
The pipe symbol is treated as a line continuation character so using ` is redundant AND very hard to see in many editors

February 13, 2018 at 3:52 pm

Running your cmd Richard returns:
Get-Service : Cannot find any service with service name 'WinRM'.
At line:3 char:6
+ Get-Service -Name WinRM -ComputerName $_ |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (WinRM:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand

Essentially trying to query a list of servers and need a csv returned with status of service whether it is stopped or started would be nice.

February 13, 2018 at 4:04 pm

The code to get that is in your original post, the problem is you are asking for only where the service is stopped.

So taking your original code, and fixing it:

$server = Get-Content -path C:\ServerList.txt
$server | foreach { get-service -computername $_ -servicename winrm | select machinename, status, name, displayname } | export-csv C:\report.csv

February 13, 2018 at 4:11 pm

Jon,

Thank you! that worked!