Listing all running processes on devices in AD for a domain

I am working through “Powershell in a month of lunches” and am a bit stumped by one lab. Listing all running processes on all devices in a specified AD domain. I have not been able to pipe the Get-ADComputer results through Get-Process, am I missing “For Each” ? Or, is it better to output to a file and have Get-Process read from the file?

What does “I have not been able …” mean? Do you get any errors? If “yes” why don’t you share them?

There is an errata for that lab:

Page 122:
Answer 4 is answer 5.
The answer to question 4 follows:
Get-ADComputer -filter * |
Select-Object @{n='computername';e={$_.name}} | Get-Process

Learn Windows PowerShell Errata

So, you may ask, why isn’t is working?
The AD computer object’s Name property is the name of the computer, but the Name parameter on Get-Process is the name of the process. When you pass an AD computer object to Get-Process through the pipeline, Get-Process will try to bind the passed Name property to its Name parameter. That will fail because it’s unlikely you’ve got a process running called SERVER01.

The Select-Object statement in the answer effectively changes the Name property to ComputerName, which allows Get-Process to correctly bind it to its ComputerName parameter.

2 Likes

Thanks, that explanation is the simpler form of what I have been reading. But I get this error now:

PS C:\Users\gregory_majersky\desktop> Get-ADComputer -Filter * | Select-Object @{n=‘computername’;e={$_.name}} | Get-Process
Get-Process : Couldn’t connect to remote machine.
At line:1 char:76

  • … ilter * | Select-Object @{n=‘computername’;e={$_.name}} | Get-Process
  •                                                           ~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:slight_smile: [Get-Process], InvalidOperationException
    • FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetProcessCommand

“Get-Process : Couldn’t connect to remote machine.” Do I have to have “WMI-Object” installed on these machines? Once I ran the commands, PS hung for a while so it was looking around.

The same when I run this command, designed to filter down the list of machines: PS C:\Users\gregory_majersky\desktop> Get-ADComputer -Filter ‘name -like “780*”’ | Select-Object @{n=‘computername’;e={$_.name}} | Get-Process
Get-Process : Couldn’t connect to remote machine.
At line:1 char:94

  • … “780*”’ | Select-Object @{n=‘computername’;e={$_.name}} | Get-Process
  •                                                           ~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:slight_smile: [Get-Process], InvalidOperationException
    • FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetProcessCommand

Do I have to specify the DNS name of each machine?

Are ALL of the computer accounts you get back from your AD switched on and available on the network when you run this snippet? :wink: :wink: :wink:

BTW: It’s highly recommended to use a -SearchBase when querying the AD to reduce the stress you put on it.

1 Like

That is a great question and the answer is "most likely not ALL, but most should be powered on. We are in a hybrid situation so people need to RDP to their desk machines, cant do that without being on and having network access.

That wasn’t really meant as a question at all, but rather as food for thought. If a computer is unreachable you will certainly get an error when you try to reach it. I thought that should be obvious.

You can either add error handling to your script or test if the computer is available before you try to query it.