[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Powershell to get all running services on all services

Posted on 2016-10-07
6
Medium Priority
?
117 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 16

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 2000 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
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!

 
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
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…

649 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