3y6pe
3y6pe

Reputation: 13

Get-ADComputer | Get-Process

When I run this:

Get-Process -name csrss -ComputerName (
    Get-AdComputer -Filter {
        (name -eq "gate") -or (name -eq "client")
    } | Select-Object -ExpandProperty name
) | Select-Object Name,MachineName

or

Get-Process -name csrss -ComputerName gate,client | Select-Object Name,MachineName

I get selected processes from the two computers:

Name                                MachineName                
----                                ----------- 
csrss                               CLIENT
csrss                               GATE 
csrss                               CLIENT                 
csrss                               GATE 

But when I run this:

Get-AdComputer -Filter {(name -eq "gate") -or (name -eq "client")} |
Select-Object @{Name='computername';Expression={$_.name}} |
Get-Process -name csrss | 
Select-Object name,machinename

I get this output:

Name                                MachineName                
----                                ----------- 
csrss                               GATE                
csrss                               GATE 

If i change name client to DC (it's a localhost) it shows processes only from DC. And it doesn't matter in which order I type the computer names (GATE first or DC first).

Even if I type this:

Get-AdComputer -Filter * |
Select-Object @{Name='computername';Expression={$_.name}} |
Get-Process -name csrss |
Select-Object name,machinename

I get output only from one machine (DC=localhost):

Name                                MachineName                
----                                ----------- 
csrss                               DC                
csrss                               DC 

Why does this happen? Why do I get processes only from one machine?

I can't get the logic, how Get-Process is getting ComputerName in the third case.

Upvotes: 1

Views: 1006

Answers (1)

BenH
BenH

Reputation: 10034

You are having difficulties because Get-ADComputer is returning two objects rather than one object with a two value array for computername. Here are four scenarios to illustrate the difference:

# One Object, two value property
[pscustomobject]@{computername="GATE","CLIENT"} |
    Get-Process csrss

# Two Objects, one value property
[pscustomobject]@{computername="GATE"},[pscustomobject]@{computername="CLIENT"} |
    Get-Process csrss

# Two Objects, one value property using a ForEach-Object
[pscustomobject]@{computername="GATE"},[pscustomobject]@{computername="CLIENT"} |
    ForEach-Object { Get-Process csrss -Computername $_.Computername}

# Two Objects, one value property using a ForEach-Object with a nested pipe
[pscustomobject]@{computername="GATE"},[pscustomobject]@{computername="CLIENT"} |
    ForEach-Object { $_ | Get-Process csrss}

Applying this concept to your code would look like this:

Get-AdComputer -Filter {(name -eq "gate") -or (name -eq "client")} |
    Select-Object @{Name='computername';Expression={$_.name}} | 
    ForEachObject {
        Get-Process -name csrss -Computername $_.Computername | 
        Select-Object name,machinename
    }

Or alternatively using the pipeline inside the ForEach-Object

Get-AdComputer -Filter {(name -eq "gate") -or (name -eq "client")} |
    Select-Object @{Name='computername';Expression={$_.name}} | 
    ForEachObject { $_ |
        Get-Process -name csrss | 
        Select-Object name,machinename
    }

Upvotes: 1

Related Questions