powershell: 判断PSObject中是否有指定property的靠谱方法

来源:互联网 发布:联通大数据平台 编辑:程序博客网 时间:2024/05/20 23:31

如何判断一个PSObject中是否定义指定名称的属性,下面的代码中举出了三个方法

$test=New-Object PSObject -Property @{    compiler=$null    }#方法一(不完全靠谱)    $test.compiler -ne $null#方法二(Get-Member -inputobject $test -name "compiler" ) -ne $null#方法三($test.PSobject.Properties.name -match "compiler")

上面三个方法,
方法一虽然最简单却不完全靠谱,因为如果compiler是$null时,返回结果是错的。
靠谱的办法是二和三,
而方法三要求powerShell 3.0以上的版本才有效

参考:

https://stackoverflow.com/questions/26997511/how-can-you-test-if-an-object-has-a-specific-property

阅读全文
0 0
原创粉丝点击