Solved

Powershell to get all running services on all services

Posted on 2016-10-07
6
73 Views
Last Modified: 2016-10-11
I have been asked to get a list of all running services on all of our servers.  I'm close to getting this with Powershell, but I'm running into issues formatting the output.  Here is what I have:

$Servers = Get-ADComputer -Filter * -Properties * | Where {$_.OperatingSystem -like "* SERVER *"} | select Name | sort name
foreach ($Server in $Servers)
{
$OnlineStatus = Test-Connection -ComputerName $server.name -BufferSize 16 -Count 1 -ErrorAction 0 -Quiet
     if ($OnlineStatus -eq "true")
     {
      Get-Service -ComputerName $server.name | where {$_.status -eq "running"} | select $server.name, name, status
      }
}

My questions are:
1.  It looks like the code above gets the services for each server, but I need to get that into a table that looks like:
        Server                       Service                    Status
             SVR1                                     ServiceA                             Running
             SVR1                                     ServiceB                             Running
             SVR2                                     ServiceA                             Running

2.  Is there a way to provide credentials to the code above so I can run it against domain controllers?

Thanks in advance for your help.  Have a good weekend.
Nick
0
Comment
Question by:ndalmolin_13
6 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 41834238
Get-Service doesn't have a -credential parameter..
https://connect.microsoft.com/PowerShell/feedback/details/567007/get-service-cmdlets-needs-a-credential-parameter

Try WMI..
$cred = get-Credential -credential <your domain user here>
$Servers = Get-ADComputer -Filter * -Properties * | Where {$_.OperatingSystem -like "* SERVER *"} | select Name | sort name
foreach ($Server in $Servers)
{
$OnlineStatus = Test-Connection -ComputerName $server.name -BufferSize 16 -Count 1 -ErrorAction 0 -Quiet
     if ($OnlineStatus -eq "true")
     {
      Get-WMIObject Win32_Service -filter "state = 'running'" -computer $server.name -credential $cred | Select __SERVER,Name,DisplayName,State
      }
}

Open in new window

0
 
LVL 14

Expert Comment

by:Jason Crawford
ID: 41835755
Are you logged in as a domain admin when you run the script?  Is an error generated when attempting to run the script against your DCs?  

Try this:

$results = @()

foreach ($Server in (Get-ADComputer -Filter * -Properties * | Where-Object {$_.OperatingSystem -like "* SERVER *"})) {
  if (Test-Connection -ComputerName $server.name -BufferSize 16 -Count 1 -ErrorAction 0 -Quiet) {
      foreach ($service in (Get-Service -ComputerName $server.name | Where-Object {$_.status -eq "running"})) {
        $object = New-object -TypeName PSobject
        $object | Add-Member -Type NoteProperty -Name 'Server Name' -Value $server.name
        $object | Add-Member -Type NoteProperty -Name 'Service Name' -Value $service.name
        $object | Add-Member -Type NoteProperty -Name 'Status' -Value $service.status
        $results += $object
      }
   }
}

$results | Select-Object 'Server Name','Service Name','Status' | Export-Csv -Path "$env:USERPROFILE\desktop\services_report.csv" -NoTypeInformation

Open in new window


I'm not in a location where I can access my sandbox at the moment so it's completely untested.  As such it may have a bug or two we can work out together.  The end result is the services_report.csv file saved on your desktop.
0
 
LVL 14

Accepted Solution

by:
Justin Yeung earned 500 total points
ID: 41835962
  Get-Service -ComputerName $server.name | where {$_.status -eq "running"} | select $server.name, name, status

Open in new window

I guess the easiest way is to change this

select $server.name to select @{n="server name";e={$server.name}}

 Get-Service -ComputerName $server.name | where {$_.status -eq "running"} |  select @{n="server name";e={$server.name}}
, name, status

Open in new window

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 41838636
Here is your original script cleaned up. There's no need to return all of the properties for Get-ADComputer or creating a variable to house the Test-Connection. Pulling all of the properties just increases the overhead of the script.

$Servers = Get-ADComputer -Filter * -Properties OperatingSystem | Where {$_.OperatingSystem -like "*SERVER*"} | Select Name | Sort Name
ForEach ($Server in $Servers)
{
     If (Test-Connection -ComputerName $server.name -BufferSize 16 -Count 1 -ErrorAction 0 -Quiet)
     {
     Get-Service -ComputerName $Server.Name | Where {$_.status -eq "running"} | select @{N="ServerName";E={$Server.Name}},Name,Status
     }
}

Open in new window


You can also Export this to a CSV file by changing some things up.

$Servers = Get-ADComputer -Filter * -Properties OperatingSystem | Where {$_.OperatingSystem -like "*SERVER*"} | Select Name | Sort Name

$myReport = @()

ForEach ($Server in $Servers)
{
     If (Test-Connection -ComputerName $Server.Name -BufferSize 16 -Count 1 -ErrorAction 0 -Quiet)
     {

     $Running = Get-Service -ComputerName $Server.Name | Where {$_.status -eq "running"} | Select Name,Status

     ForEach ($Service in $Running)
     {

        $myService = "" | Select ServerName,ServiceName,Status
        $myService.ServerName = $Server.Name
        $myService.ServiceName = $Service.Name
        $myService.Status = $Service.Status
        $myReport += $myService
     }

     }
}

$myReport | Export-CSV C:\Temp\ServiceReport.csv -nti

Open in new window

2
 
LVL 1

Author Closing Comment

by:ndalmolin_13
ID: 41839179
Worked great
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41839557
@ndalmolin_13, Just wondering about the credentials part which you asked, coz Get-service wont take care of it, unless you are using a credential to run the script which has access to all servers..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now