Get WinRm service status on Multiple servers

This topic contains 6 replies, has 4 voices, and was last updated by  chrodriguez1 3 months, 1 week ago.

  • Author
    Posts
  • #93288

    chrodriguez1
    Participant

    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

    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

    postanote
    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

    Richard Siddaway
    Moderator

    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

    chrodriguez1
    Participant

    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

    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

    chrodriguez1
    Participant

    Jon,

    Thank you! that worked!

You must be logged in to reply to this topic.