outgridview不排序结果



我有一个脚本,我试图从服务器列表(以及使用的空间和空闲空间)中收集驱动器号,然后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

最新更新