如何判断一个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
本文介绍了三种在PowerShell中检查PSObject是否具有特定属性的方法。第一种方法简单但不完全可靠,后两种方法更为准确,其中第三种方法要求PowerShell3.0以上版本。
3063

被折叠的 条评论
为什么被折叠?



