从电子表格导入服务账户并设置密码永不过期时遇错误
解决Set-AzureADUser参数ObjectId为空的错误
错误说明
你遇到的错误翻译为:
Set-AzureADUser : 无法将参数绑定到“ObjectId”,因为该参数值为null
问题根源
脚本的ForEach块中使用了$user.UserPrincipalName,但$user变量并未被定义,导致ObjectId参数接收到空值,触发报错。
修正后的脚本
方式1:使用管道默认对象$_
直接用$_引用当前从CSV读取的对象:
Import-CSV "C:\Users\$env:username\Desktop\azureadusers.csv" | ForEach { Set-AzureADUser -ObjectId $_.UserPrincipalName -PasswordPolicies DisablePasswordExpiration }
方式2:显式定义变量
如果习惯用命名变量,可在块内先赋值:
Import-CSV "C:\Users\$env:username\Desktop\azureadusers.csv" | ForEach { $user = $_ Set-AzureADUser -ObjectId $user.UserPrincipalName -PasswordPolicies DisablePasswordExpiration }
额外检查点
确保你的CSV文件中存在**UserPrincipalName列**,且列名拼写完全一致(区分大小写),若列名错误,同样会导致UserPrincipalName值为空。
内容的提问来源于stack exchange,提问作者On3N3xus
相关产品推荐
相关产品推荐

