0

I would like to know if there is a way to add a If statement in the expression of a WMI-object | select ...

here is what I have :

Get-WmiObject -Class Win32_LogicalDisk -Filter DriveType=3 -ComputerName $ServersinFarm | Select @{Name='Server Name';Expression={$_.SystemName}} , DeviceID , @{Name="size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}}, @{Name="freespace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}}

and I want to add a column to the output If the freespace < 10gb that writes error

Any Ideas?

Filburt
  • 17,626
  • 12
  • 64
  • 115
JP Legris
  • 21
  • 1
  • 2
  • 7

1 Answers1

0

You can't conditionally add a calculated property. Something like this would work, though:

Get-WmiObject -Class Win32_LogicalDisk ... |
    Select ..., Freespace, ... |
    ForEach-Object {
        if ($_.freespace -lt 10GB) {
            $_ | Add-Member -Name 'Status' -Type NoteProperty -Value 'Error'
        }
        $_
    } | Select-Object -Exclude Freespace

What you can do with calculated properties is add a property indicating the status depending on the amount of free space:

Get-WmiObject -Class Win32_LogicalDisk ... |
    Select ..., @{n='Status';e={if ($_.Freespace -lt 10GB) {'Error'} else {'OK'}}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328