No error when selecting non-existing property

前端 未结 3 1878
梦谈多话
梦谈多话 2021-01-14 12:07

I want PowerShell to throw an error when trying to select non-existing properties, but instead I get empty column as output. Example:

$ErrorActionPreference=         


        
3条回答
  •  迷失自我
    2021-01-14 12:58

    Something like this...?

    $props = 'Id','ProcessName','xxx'
    $availableProps = Get-Process *ex*|Get-Member -MemberType Properties | Select -ExpandProperty Name
    $missingProps = $props | Where-Object {-not ($availableProps -contains $_)}
    if ($missingProps) {
      Write-Error "invalid property(s) $missingProps"
      throw { [System.Management.Automation.PropertyNotFoundException] }
    }
    
    Get-Process *ex* | Select-Object $props
    

提交回复
热议问题