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-Objectis missing pipeline input - pipe theInvoke-Commandcall's output to it.Instead of
-InputObject, use-Property:Note:
-InputObjectis the parameter that facilitates pipeline input, and is usually not meant to be used directly.As with
Sort-Object,-Propertyis the first positional parameter, so you may omit-Propertyin the call below.Also note that
-ComputerNamecan accept an array of computer names, which are then queried in parallel, so if you want to query all computers and then callOut-GridViewonly once, for the results from all targeted computers:To group the results by target computer, use
Sort-Object pscomputername, root -DescendingIf you'd rather stick with your sequential, target-one-server-at-a-time approach, change from a
foreachstatement - which cannot be used directly as pipeline input - to aForEach-Objectcall, which allows you to pipe to a singleOut-GridViewcall: