Solved

Powershell to get all running services on all services

Posted on 2016-10-07
6
96 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 15

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

690 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