Search code examples
powershellpowershell-remoting

Where-Object works locally but not remotely


I'm trying to learn PowerShell. I wrote a small script to monitor my Exchange servers. I can't understand why this line work on the local machine without problem

Get-WmiObject -Class win32_volume -Filter 'drivetype = 3' | Where-Object 'Label' -ne "System Reserved" | ft SystemName, DriveLetter, Label, @{LABEL='FreeSpaceGB'; EXPRESSION={"{0:N0}" -f ($_.freespace/1GB)}}

but if I put the same code into the invoke-command I get this error.

Invoke-Command -ComputerName $server -credential $c -ScriptBlock {
    Get-WmiObject -Class win32_volume -Filter 'drivetype = 3' | Where-Object 'Label' -ne "System Reserved" | ft SystemName, DriveLetter, Label, @{LABEL='FreeSpaceGB'; EXPRESSION={"{0:N0}" -f ($_.freespace/1GB)}}
    }

Cannot bind argument to parameter 'FilterScript' because it is null. + CategoryInfo : InvalidData: (:) [Where-Object], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.WhereObject Command

I tried everything I can think of without luck. I even check with gm and the Label do exist... Any tip..?


Solution

  • Looks like the endpoint you're connecting to is PowerShell 2.0, which doesn't have the simplified Where-object syntax.

    Try this where-object instead:

    Where-Object {$_.Label -ne "System Reserved"}