locked
Powershell Get-Service Select vs. Format-Table RRS feed

  • Question

  • I'm working on a brief script to query a remote machine for it's services, which services the services "require" to run, as well as what services are dependent on that service to run.

    The information is all there, and I can dump the data to the screen easily enough with Format-Table.  I'd *like* to dump the information to a tab-separated file, or Excel... I've dumped to excel before using a loop and individual queries, but this has me stumped.

    Bear with my fairly inarticulate description here, but it looks like the "RequiredServices" and "DependentServices" are an array - when dumped to the screen they appear in braces.  

    ------------

    Ex:

    Get-Service -ComputerName $computer | Format-Table MachineName,Status,Name,DisplayName,RequiredServices,DependentServices -AutoSize -Wrap

    Result:

    mycomputername Stopped RpcLocator                     Remote Procedure Call (RPC) Locator                  {LanmanWorkstation}                           {}                                           
    mycomputername Running RpcSs                          Remote Procedure Call (RPC)                          {DcomLaunch}                                  {xmlprov, WZCSVC, wscsvc, WmiApSrv...}   

    ------------



    The problem is, this is only useful to the screen, and I'd like to dump this information into columns in Excel.

    I tried the same thing using Select instead:



    ------------

    Code:

    Get-Service -ComputerName $computer | Select MachineName,Status,Name,DisplayName,RequiredServices,DependentServices | export-csv -delimiter "`t" -Path "\\Pathtofile\$computer.TestOutput.ttx"

    Result (from file):

    "mycomputername " "Stopped" "RpcLocator" "Remote Procedure Call (RPC) Locator" "System.ServiceProcess.ServiceController[]" "System.ServiceProcess.ServiceController[]"
    "mycomputername " "Running" "RpcSs" "Remote Procedure Call (RPC)" "System.ServiceProcess.ServiceController[]" "System.ServiceProcess.ServiceController[]"

    ------------

    The "System.ServiceProcess.ServiceController[]" is what I'm assuming is a returned array in the Format-Table.

    How can I get that data from "System.ServiceProcess.ServiceController[]" into a single cell's worth of output for export?

    I would greatly appreciate any input.
    Sunday, December 7, 2014 6:10 PM

Answers

  • Sorry - I should have known that wouldn't work.

    This does:

    # get calculated properties
    $properties=@(
         'MachineName',
         'Status',
         'Name',
         'DisplayName',
         @{N='RequiredServices';    E={$_.RequiredServices -join ';'    }},
         @{N='DependentServices'; E={$_.DependentServices -join ';'}}
    )
    
    #Get required service info
    Get-Service -ComputerName omega| 
         Select-Object $properties |
         Export-Csv ToExcel.csv -NoTypeInfo
    
    #open in Excel
    .\ToExcel.csv
    


    ¯\_(ツ)_/¯

    • Proposed as answer by Valeras Friday, December 12, 2014 1:54 PM
    • Marked as answer by B.Adler FL Friday, December 12, 2014 7:05 PM
    Sunday, December 7, 2014 7:03 PM

All replies

  • In Excel you need to do this:

    # get calculated properties
    $properties=@(
         'MachineName',
         'Status',
         'Name',
         'DisplayName',
         @{N='RequiredServices';    E={"$($_.RequiredServices)"    }},
         @{N='DependentServices'; E={"$($_.DependentServices)"}}
    )
    
    #Get required service info
    Get-Service -ComputerName omega| 
         Select-Object $properties |
         Export-Csv ToExcel.csv -NoTypeInfo
    
    #open in Excel
    .\ToExcel.csv
    


    ¯\_(ツ)_/¯

    Sunday, December 7, 2014 6:27 PM
  • This mostly works and thanks so much!  What I seeing in the returned data, though, is that in cases where there are single required and/or dependent services, output is "System.ServiceProces.ServiceController", while in cases where there are multiple required and/or dependent services, output shows the services listed in a single cell.

    For instance, here's the output for DcomLaunch:

    "machinename" "Running" "DcomLaunch" "DCOM Server Process Launcher" "" "xmlprov WZCSVC wscsvc WmiApSrv WinRM SharedAccess 6to4 winmgmt VSS TlntSvr Irmon FastUserSwitchingCompatibility TermService RasAuto RasMan TapiSrv STSchedEx StiSvc srservice Spooler Sophos Web Control Service Sophos Device Control Service Sophos AutoUpdate Service ShellHWDetection Schedule SAVService MSDTC SamSs RemoteRegistry RemoteAccess ProtectedStorage PolicyAgent NtmsSvc Netman napagent MSIServer Messenger hkmsvc helpsvc SENS EventSystem ersvc Dot3svc EapHost dmadmin dmserver CryptSvc COMSysApp BITS AudioSrv RpcSs"

    Here's the screen output using Filter-Table - where you can see the empty field, shown above as "" is a {}.  I *do* like that your turn on the code shows *ALL* the services instead of truncating.

    machinename  Running DcomLaunch                     DCOM Server Process Launcher                         {}                                            {xmlprov, WZCSVC, wscsvc, WmiApSrv...}

    However, here's the output for "RasMan" - 

    The tab separated file shows:

    "30888sure04" "Stopped" "RasMan" "Remote Access Connection Manager" "System.ServiceProcess.ServiceController" "System.ServiceProcess.ServiceController"

    Where the Filter-Table shows:

    30888sure04 Stopped RasMan                         Remote Access Connection Manager                     {Tapisrv}                                     {RasAuto} 

    The difference I see is that the Dependent and Required service in this case is a single-word name, and only one service, rather than multiple.

    Is this because of the formatting in the code below?  I really need to dig deeper into this part to understand better what's going on.

         @{N='RequiredServices';    E={"$($_.RequiredServices)"    }},
         @{N='DependentServices'; E={"$($_.DependentServices)"}

    Sunday, December 7, 2014 6:51 PM
  • Sorry - I should have known that wouldn't work.

    This does:

    # get calculated properties
    $properties=@(
         'MachineName',
         'Status',
         'Name',
         'DisplayName',
         @{N='RequiredServices';    E={$_.RequiredServices -join ';'    }},
         @{N='DependentServices'; E={$_.DependentServices -join ';'}}
    )
    
    #Get required service info
    Get-Service -ComputerName omega| 
         Select-Object $properties |
         Export-Csv ToExcel.csv -NoTypeInfo
    
    #open in Excel
    .\ToExcel.csv
    


    ¯\_(ツ)_/¯

    • Proposed as answer by Valeras Friday, December 12, 2014 1:54 PM
    • Marked as answer by B.Adler FL Friday, December 12, 2014 7:05 PM
    Sunday, December 7, 2014 7:03 PM
  • Is this because of the formatting in the code below?  I really need to dig deeper into this part to understand better what's going on.

         @{N='RequiredServices';    E={"$($_.RequiredServices)"    }},
         @{N='DependentServices'; E={"$($_.DependentServices)"}

    Research "calculated properties".

    ¯\_(ツ)_/¯

    Sunday, December 7, 2014 7:04 PM
  • Thanks - that worked flawlessly!  

    I'll definitely check into calculated properties.

    Sunday, December 7, 2014 7:33 PM