I have a script that I am trying to collect drive letters from a list of servers (as well as used space and free space) and then gridview the results out.
$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
}
I can get it to display the drive information for each server on the list but gridview does not work. I have tried moving the brackets around (before and after gridview) as well as piping elements but have had no luck.
Can anyone advise me as to what I am doing wrong? I feel like it is something simple but all of the examples I am finding online do not use the foreach command which I think has to do with throwing it off.
Your
Select-Object
is missing pipeline input - pipe theInvoke-Command
call's output to it.Instead of
-InputObject
, use-Property
:Note:
-InputObject
is the parameter that facilitates pipeline input, and is usually not meant to be used directly.As with
Sort-Object
,-Property
is the first positional parameter, so you may omit-Property
in the call below.Also note that
-ComputerName
can accept an array of computer names, which are then queried in parallel, so if you want to query all computers and then callOut-GridView
only once, for the results from all targeted computers:To group the results by target computer, use
Sort-Object pscomputername, root -Descending
If you'd rather stick with your sequential, target-one-server-at-a-time approach, change from a
foreach
statement - which cannot be used directly as pipeline input - to aForEach-Object
call, which allows you to pipe to a singleOut-GridView
call: