locked
List the state of multipul services on all servers in the domain RRS feed

  • Question

  • I'm trying to list the state of specific services that are running on all servers in the domain ie. DHCP Server or DNS Server

    At the moment i have the following script working for one service only and its querying a list of servers in a txt file which is not ideal -


    $Computers = Get-Content "C:\Servers.txt"
    $WmiObject = @{
    Class = "Win32_Service"
    }
    foreach ($Computer in $Computers)
    {
    $Select = @{
    Property = "SystemName","Name","StartMode","State"
    }
    Get-WmiObject @WmiObject -ComputerName $Computer | where{$_.Name -eq "DNS Server"} | Select-Object @Select
    }

    Tuesday, August 21, 2012 10:41 AM

Answers

  • $results = @()
    $WmiObject = @{
     Class = "Win32_Service"
     Filter = "Name='Alerter' or Name='DHCPServer'"}
    $Select = @{Property = "SystemName","Name","StartMode","State"}
    $as = [adsisearcher]'(objectcategory=computer)'
    $computers = $as.FindAll()
    foreach ($computer in $computers) {
        $name = $computer.properties.item('Name')
        $results += Get-WmiObject @WmiObject -ComputerName $name | Select-Object @Select }
    $results | Export-Csv 'Results.csv' -NoTypeInformation


    Grant Ward, a.k.a. Bigteddy

    • Proposed as answer by Bigteddy Tuesday, August 21, 2012 4:27 PM
    • Marked as answer by Yan Li_ Wednesday, August 22, 2012 6:38 AM
    Tuesday, August 21, 2012 2:21 PM

All replies

  • To get the results for more than one service you could try this in your Where statement...

    ... where{$_.Name -Match 'DNS|DHCP Server'} ...

    The match evaluates a regular expression... the pipe symbol | in this regular expression means 'DNS' _or_ 'DHCP Server'

    You may have to adjust 'DNS' and 'DHCP Server' to match what you're looking for.

    You can also add more services to the expression, separated by '|'

    It's generally more efficient to use a filter in the WMI query... (the remote machine does the filtering work)

    gwmi Win32_Service -filter "Name='DHCP' or Name='WinRm'"

    String the whole lot together...

    gwmi Win32_Service -filter "Name='DHCP' or Name='WinRm'" -computer (Get-Content 'C:\Servers.txt') | Select SystemName,Name,StartMode,State

    I'm guessing you would prefer to get your list of servers from AD?


    Just a humble SysAdmin

    Tuesday, August 21, 2012 11:06 AM
  • Another way of querying multiple services is with the Get-Service cmdlet:

    PS C:\scripts> Get-Service winrm, winmgmt
    
    Status   Name               DisplayName                           
    ------   ----               -----------                           
    Running  winmgmt            Windows Management Instrumentation    
    Stopped  winrm              Windows Remote Management (WS-Manag...


    Grant Ward, a.k.a. Bigteddy

    Tuesday, August 21, 2012 11:21 AM
  • Excellent thanks RiffyRiot, i've inserted a filter so my command now looks like this-

    $Computers = Get-Content "R:\Servers.txt"
    $WmiObject = @{
    Class = "Win32_Service"
    Filter = "Name='Alerter' or Name='DNS'"
    }
    foreach ($Computer in $Computers)
    {
    $Select = @{
    Property = "SystemName","Name","StartMode","State"
    }
    Get-WmiObject @WmiObject -ComputerName $Computer | Select-Object @Select
    }

    I would prefer to query Multipul OU's in AD or the whole domain if possible.

    Tuesday, August 21, 2012 11:28 AM
  • Thanks for your suggestion Bigteddy but i'd prefer to use Get-WmiObject because i need to display the StartMode.
    Tuesday, August 21, 2012 11:29 AM
  • This is how you would retrieve the computer names from Active Directory:

     $WmiObject = @{
     Class = "Win32_Service"
     Filter = "Name='Alerter' or Name='DNS'"}
     
    $Select = @{Property = "SystemName","Name","StartMode","State"}
    
    $as = [adsisearcher]'(objectcategory=computer)'
    $computers = $as.FindAll()
    
    foreach ($computer in $computers) {
        $name = $computer.properties.item('Name')
        Get-WmiObject @WmiObject -ComputerName $name | Select-Object @Select
        }
       


    Grant Ward, a.k.a. Bigteddy

    Tuesday, August 21, 2012 12:37 PM
  • Brilliant, that works but unfortunately when i try to export it to a CSV it recreates the file after every computer that it scans.

    Do you know how i can limit the search for servers only and how i can export all results to a single CSV?

    This is the script i'm using so far:

    $WmiObject = @{
     Class = "Win32_Service"
     Filter = "Name='Alerter' or Name='DHCPServer'"}
    $Select = @{Property = "SystemName","Name","StartMode","State"}
    $as = [adsisearcher]'(objectcategory=computer)'
    $computers = $as.FindAll()
    foreach ($computer in $computers) {
        $name = $computer.properties.item('Name')
        Get-WmiObject @WmiObject -ComputerName $name | Select-Object @Select | Export-csv c:\Services.csv}
    Many thanks for your help so far.


    • Edited by Jo Gaffney Tuesday, August 21, 2012 1:21 PM
    Tuesday, August 21, 2012 12:54 PM
  • Change this line as follows:

    $as = [adsisearcher]'(&(objectCategory=computer)(operatingSystem=*server*))'

    Grant Ward, a.k.a. Bigteddy


    • Edited by Bigteddy Tuesday, August 21, 2012 1:27 PM
    Tuesday, August 21, 2012 1:26 PM
  • Thanks again. The only problem i have now is exporting the results to a csv. When i  put "| Export-csv c:\Services.csv" at the end then it tries to create the file after every server that it scans. Do you have any idea how to export all the results to a single CSV?

    Tuesday, August 21, 2012 1:52 PM
  • $results = @()
    $WmiObject = @{
     Class = "Win32_Service"
     Filter = "Name='Alerter' or Name='DHCPServer'"}
    $Select = @{Property = "SystemName","Name","StartMode","State"}
    $as = [adsisearcher]'(objectcategory=computer)'
    $computers = $as.FindAll()
    foreach ($computer in $computers) {
        $name = $computer.properties.item('Name')
        $results += Get-WmiObject @WmiObject -ComputerName $name | Select-Object @Select }
    $results | Export-Csv 'Results.csv' -NoTypeInformation


    Grant Ward, a.k.a. Bigteddy

    • Proposed as answer by Bigteddy Tuesday, August 21, 2012 4:27 PM
    • Marked as answer by Yan Li_ Wednesday, August 22, 2012 6:38 AM
    Tuesday, August 21, 2012 2:21 PM
  • Get-WMIObject work with String-Array in ComputerName:

    $Server = Get-Content D:\Server.txt
    
    Get-WmiObject -ComputerName $Server -Query @"
    SELECT
    	SystemName,Name,StartMode,State
    FROM
    	Win32_Service
    WHERE
    	Name = 'BITS'
    or
    	Name = 'Dhcp'
    or
    	Name = 'WinRM'
    "@ |
    	Sort-Object -Property SystemName,Name |
    	Format-Table -Property SystemName,Name,StartMode,State -AutoSize
    


    Mit freundlichen Grüßen Jens Kalski

    Tuesday, August 21, 2012 2:46 PM
  • Instead of reading Computernames from a textfile you can read it directly from the domain:

    Import-Module ActiveDirectory
    
    $Server = Get-ADComputer -SearchBase 'ou=server,ou=computer,dc=......' -Filter { Name -like "SRV-*" } | Select-Object -ExpandProperty Name
    


    Mit freundlichen Grüßen Jens Kalski

    Tuesday, August 21, 2012 2:54 PM
  • Thanks very much Bigteddy, that works perfectly.
    Tuesday, August 21, 2012 4:00 PM