我有一个脚本,我试图从服务器列表(以及使用的空间和空闲空间)中收集驱动器号,然后gridview结果。
$servers = Get-Content "path.txt"
foreach ($server in $servers) {
Invoke-Command -ComputerName $server {Get-PSDrive | Where {$_.Free -gt 0}}
Select-Object -InputObject usedspace,freespace,root,pscomputername |
Sort-Object root -Descending | Out-Gridview
}
我可以让它显示列表上每个服务器的驱动器信息,但gridview不起作用。我曾尝试移动括号周围(前后gridview)以及管道元素,但没有运气。
谁能告诉我我做错了什么?我觉得这很简单,但是我在网上找到的所有例子都没有使用foreach命令,我认为这与扔掉它有关。-
您的
Select-Object
缺少管道输入-管道Invoke-Command
调用的输出给它。 -
用
代替-Property
:-InputObject
注:
。-InputObject
是便于管道输入的参数,通常不能直接使用与
Sort-Object
一样,-Property
是第一个位置参数,因此在下面的调用中可以省略-Property
。
foreach ($server in Get-Content "path.txt") {
Invoke-Command -ComputerName $server { Get-PSDrive | Where { $_.Free -gt 0 } } |
Select-Object -Property usedspace, freespace, root, pscomputername |
Sort-Object root -Descending |
Out-Gridview
}
还需要注意的是,-ComputerName
可以接受数组的计算机名,然后查询并行,所以如果你想查询所有计算机,然后只调用Out-GridView
一次,从所有目标计算机的结果:
Invoke-Command -ComputerName (Get-Content "path.txt") {
Get-PSDrive | Where Free -gt 0
} |
Select-Object -Property usedspace, freespace, root, pscomputername |
Sort-Object root -Descending |
Out-Gridview
Sort-Object pscomputername, root -Descending
按目标计算机对结果进行分组如果您宁愿坚持顺序的,每次一个目标服务器的方法,从foreach
语句(不能直接用作管道输入)更改为ForEach-Object
调用,这允许您管道到单个Out-GridView
调用:
Get-Content "path.txt" |
ForEach-Object {
Invoke-Command -ComputerName $_ { Get-PSDrive | Where Free -gt 0 }
} |
Select-Object -Property usedspace, freespace, root, pscomputername |
Sort-Object root -Descending |
Out-Gridview