Get WinRm service status on Multiple servers

Welcome Forums General PowerShell Q&A Get WinRm service status on Multiple servers

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
9 months ago.

  • Author
    Posts
  • #93288

    Participant
    Points: 0
    Rank: Member

    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.

  • #93307
    Jon

    Participant
    Points: 24
    Rank: Member

    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.

  • #93318

    Participant
    Points: 208
    Helping Hand
    Rank: Participant

    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)
        ...
    
    
  • #93408

    Participant
    Points: 0
    Rank: Member

    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

  • #93561

    Participant
    Points: 0
    Rank: Member

    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.

  • #93564
    Jon

    Participant
    Points: 24
    Rank: Member

    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
    
  • #93567

    Participant
    Points: 0
    Rank: Member

    Jon,

    Thank you! that worked!

The topic ‘Get WinRm service status on Multiple servers’ is closed to new replies.