Solved

Powershell to get all running services on all services

Posted on 2016-10-07
6
61 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
Comment Utility
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
Comment Utility
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
Comment Utility
  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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Expert Comment

by:Joshua Grantom
Comment Utility
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
Comment Utility
Worked great
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
@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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Set OWA language and time zone in Exchange for individuals, all users or per database.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

12 Experts available now in Live!

Get 1:1 Help Now