1
votes

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.

1
does it work if you add -computername $_.computername to the gps command?Martin Brandl
@MartinBrandl No. If i add -computername $_.computername I get an error. If i add -computername {$_.computername} the result is the same.3y6pe
Does it have to be a one line command?Seth
@Seth I'm not sure if I got you right, but i'm not trying to find the best way to do something, so it doesn't have to be one line command. I mean, because i just started to learn Powershell, i'm trying to understand how it works, or why it doesn't. So I need to know, why exactly in the third case I can't get what i want. :)3y6pe

1 Answers

1
votes

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
    }