Powershell 获取ADComputer |获取进程
当我运行此命令时:Powershell 获取ADComputer |获取进程,powershell,Powershell,当我运行此命令时: Get-Process -name csrss -ComputerName ( Get-AdComputer -Filter { (name -eq "gate") -or (name -eq "client") } | Select-Object -ExpandProperty name ) | Select-Object Name,MachineName Get-AdComputer -Filter {(name -eq "gate")
Get-Process -name csrss -ComputerName (
Get-AdComputer -Filter {
(name -eq "gate") -or (name -eq "client")
} | Select-Object -ExpandProperty name
) | Select-Object Name,MachineName
Get-AdComputer -Filter {(name -eq "gate") -or (name -eq "client")} |
Select-Object @{Name='computername';Expression={$_.name}} |
Get-Process -name csrss |
Select-Object name,machinename
或
我从两台计算机获取选定的进程:
Name MachineName
---- -----------
csrss CLIENT
csrss GATE
csrss CLIENT
csrss GATE
我得到这个输出:
Name MachineName
---- -----------
csrss GATE
csrss GATE
我仅从一台机器(DC=localhost)获得输出:
名称MachineName
---- -----------
csrss DC
csrss DC
为什么会发生这种情况?为什么我只能从一台机器获取进程
我无法获得逻辑,在第三种情况下,
get进程如何获得ComputerName
。您遇到了困难,因为get ADComputer
返回的是两个对象,而不是一个带有ComputerName
两值数组的对象。以下是四种不同的场景:
# 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}
将此概念应用于代码将如下所示:
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
}
或者使用ForEach对象内部的管道
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
}
如果向gps命令添加-computername$\uu.computername
是否有效?@MartinBrandl否。如果添加-computername$\uu.computername
,则会出现错误。如果我添加-computername{$\u.computername}
,结果是一样的。它必须是一行命令吗?@Seth我不确定你说的对不对,但我不想找到做某事的最佳方法,所以它不必是一行命令。我的意思是,因为我刚刚开始学习Powershell,我试图理解它是如何工作的,或者为什么不工作。所以我需要知道,为什么在第三种情况下,我不能得到我想要的东西
# 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}
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
}
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
}