Getting OS related service and Non-OS related servie

This topic contains 7 replies, has 4 voices, and was last updated by Profile photo of gautam panday gautam panday 7 months, 1 week ago.

  • Author
    Posts
  • #38340
    Profile photo of gautam panday
    gautam panday
    Participant

    Hi ,
    I am a starter in Power shell scripting..
    I am trying to retrieve the list of OS related and Non -OS related service..from n no. of Servers.
    Do we have any cmdlet for the same, if not.. how I can bifurcate the same.

    As in service console, I can see all Win32 related services are stored on C:\windows\system32
    and any Non-OS like anti-virus or any other application used to store in C:\program\... filed
    Please suggest..

  • #38341
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Get-Service or WMI's Win32_Service

  • #38343
    Profile photo of gautam panday
    gautam panday
    Participant

    hey Rob,
    Thanks for heading up..

    Gsv or gmi win32_service will list all services.. including the third party apps too. I wants bifurcation for the same... like win32 servicess and apps services..

  • #38354
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    Your problem is identifying the 'non-OS' services. There isn't a flag on the service object to indicate where it came from

    If you use
    Get-CimInstance Win32_Service | select Caption, PathName

    You'll see that many services have a path that starts C:\windows\system32\...
    That's probably your best bet but its not infallible e.g.
    Windows Modules Installer has a path of C:\WINDOWS\servicing\TrustedInstaller.exe

  • #38438
    Profile photo of gautam panday
    gautam panday
    Participant

    Thanks Richard..
    Get-CimInstances win32_service is much better option.

    I am using this option for getting a OS and Non -OS services from a list of server.

    function Get-OSandNonOSservices
    {
    param(
    [Parameter(Mandatory=$true, Position=0,
    ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [ValidateNotNull()]
    [string[]]$ComputerName
    )

    process {
    foreach ($c in $ComputerName) {
    $Osservices = (get-CimInstance Win32_Service -ComputerName $c) | select Caption, PathName | sort-object pathname | where {$_.Pathname -like "C:\Windows\*.*"} | Select Caption
    $NonOsservices = (get-CimInstance Win32_service -ComputerName $c) | Select Caption, PathName | where {$_.PathName -notlike "C:\Windows\*.*"} | select Caption

    new-object psobject -prop @{

    ComputerName = $c
    OSServices = $Osservices
    NonOSServices = $NonOsservices
    }}}}
    cat '.\computername.txt' | Get-OSandNonOSservices | Format-table | out-file -filepath "D:\Test\AllServices.csv"

    but I am not sure why this -computername is not appearing for .. Get-CimInstances win32_service

  • #38579
    Profile photo of gautam panday
    gautam panday
    Participant

    Any one can help me out here..

    Though i am importing a list of servers services , but the result is showing only for one server.
    I tried for get-CimInstance Win32_Service. please help.

    (Import-Csv "D:\Test\serverlist.csv") | forEach-Object {
    $ListofOsservice = Get-Service -ComputerName $_.ServerName | Select-Object -Property DisplayName,ServiceType | Where-Object {$_.ServiceType -like "Win32ShareProcess"}| Select DisplayName
    $NonOsservices = Get-Service -ComputerName $_.ServerName | Select-Object -Property DisplayName,ServiceType | Where-Object { $_.ServiceType -notlike "Win32ShareProcess"} | Select DisplayName
    }
    ######## "This is a list of Osservices"
    $listofOsservice | Format-table | Out-file -FilePath "D:\Test\Osservices.txt"
    ########## "This is a list of Non-Osservices"
    $NonOsservices | Format-Table | Out-file -FilePath "D:\Test\NonOsservices.txt

  • #38581
    Profile photo of Paul Frankovich
    Paul Frankovich
    Participant

    The reason is that as you are going through the loop you are overwriting you $ListofOsService and $NonOsServices. When you complete the loop you only have the two objects. Not everything you pulled in the loop.

    Move your Out-File into the loop and add -Append and you will get them all like you intend.

  • #38587
    Profile photo of gautam panday
    gautam panday
    Participant

    Excellent Paul.. it works ..bit more... I want to display the server name and their respective services as a display name ..

You must be logged in to reply to this topic.