Solved

Powershell to get all running services on all services

Posted on 2016-10-07
6
89 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…

726 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