1

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
MjrPayne
  • 105
  • 1
  • 12
  • 1
    What's the error message? You have a typo in your `Select-Object` command by the way, it should be `-Descending` and not `-Decending` – Paolo Jan 18 '23 at 19:18
  • 1
    You aren't piping to select-object. – js2010 Jan 18 '23 at 19:34
  • Im not getting any errors. It does out put the information but in the ISE screen rather than the Out-GridView window. The typo was my fault. it is spelled correctly in the script. I had to retype due to separation of networks. – MjrPayne Jan 18 '23 at 19:35
  • @js2010 Invoke-Command -ComputerName $server {Get-PSDrive | Where {$_.Free -gt 0}} | yields an error "The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input" – MjrPayne Jan 18 '23 at 19:38

1 Answers1

3
  • Your Select-Object is missing pipeline input - pipe the Invoke-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.

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
}

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 call Out-GridView only once, for the results from all targeted computers:

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

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 a ForEach-Object call, which allows you to pipe to a single Out-GridView call:

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
mklement0
  • 382,024
  • 64
  • 607
  • 775