You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从电子表格导入服务账户并设置密码永不过期时遇错误

解决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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 01:45:38