查询空值时在powershell中调用MethodUnll
我的powershell脚本将从ldap存储库查询和更新ldap属性。但我希望查询/更新的属性在ldap存储库中是一个空字段。据我所知,powershell无法接受空值,因此我的脚本返回了以下错误消息查询空值时在powershell中调用MethodUnll,powershell,Powershell,我的powershell脚本将从ldap存储库查询和更新ldap属性。但我希望查询/更新的属性在ldap存储库中是一个空字段。据我所知,powershell无法接受空值,因此我的脚本返回了以下错误消息 You cannot call a method on a null-valued expression. At D:\deployment\test.ps1:67 char:17 + ... $f = $l.Attributes['String7'].GetValu
You cannot call a method on a null-valued expression.
At D:\deployment\test.ps1:67 char:17
+ ... $f = $l.Attributes['String7'].GetValues('string')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
我输入了
设置StrictMode-Off
以接受空值,但它“似乎不像预期的那样工作请参考以下脚本
foreach ($l in $r.Entries)
{
//some process over here
if($ID -eq "xxxxx")
{
$N = $l.Attributes['String11'].GetValues('string')
if($N -eq "something here")
{
$f = $list.Attributes['String7'].GetValues('string')
if($f -eq $null)
{
//update string7 attribute
$l.Attributes['String7'].SetValues("x")
}
}
}
$r
将是来自ldap存储库的我的响应,String7
默认为空首先:
- PowerShell使用
字符作为注释字符,而不是#
/
- 您的代码缺少最后一个花括号
。(使用更好的代码缩进,您可以很容易地看到缺少的括号)}
try{..}catch{..}
construct,您将能够克服获得的错误并更新此属性。
比如:
foreach ($l in $r.Entries) {
# some process over here
if($ID -eq "xxxxx") {
$N = $l.Attributes['imString11'].GetValues('string')
if($N -eq "something here") {
try {
$f = $list.Attributes['String7'].GetValues('string')
}
catch {}
if(!$f) {
# update string7 attribute
$l.Attributes['String7'].SetValues("x")
}
}
}
}
希望对你有所帮助谢谢你的帮助,你救了我的命。sry用于//而不是#。因为我是新来powershell的