![Powershell - 使用管理员凭据的 Active Directory](https://linux22.com/image/1562371/Powershell%20-%20%E4%BD%BF%E7%94%A8%E7%AE%A1%E7%90%86%E5%91%98%E5%87%AD%E6%8D%AE%E7%9A%84%20Active%20Directory.png)
我正在尝试使用我的管理员凭据运行 powershell 脚本。我对“powershell 脚本世界”还很陌生,所以我一直在查看现有脚本并将它们合并/修改在一起。
我有字符串:
$targetdomain = ""
Write-Verbose "Getting credentials"
$domaincredential = Get-Credential -UserName "$targetdomain\$env:USERNAME-ADM" -Message "Use a credential that has permission to create users in the target domain $targetdomain"
这些提示我输入 -adm 帐户信息。但是当我尝试运行脚本时,它说
Set-ADUser:访问权限不足,无法执行操作
如果通过“右键单击图标->以管理员身份运行->输入凭据”打开 powershell,然后复制脚本,那么它就可以正常工作。
所以我的问题是。我做错了什么?我猜是凭证行,但我就是搞不清楚哪里出了问题。我也试过用“ $cred = Get-Credential domain\username
”,但也没用。
您可以在下面看到完整的脚本:
$targetdomain = ""
Write-Verbose "Getting credentials"
$domaincredential = Get-Credential -UserName "$targetdomain\$env:USERNAME-ADM" -Message "Use a credential that has permission to create users in the target domain $targetdomain"
foreach($line in Get-Content "request.txt") {
if ($line.length -eq 0) {
$line = " "
}
if($line -match "First name of user: ") {
$name = $line.Substring(21).trim()
}
if($line -match " Last name of user: ") {
$lastname = $line.Substring(22).trim()
}
if ($line.Substring(0,1) -eq "" -and $line.Length -eq 6) {
$LID = $line.trim()
}
}
Set-ADUser -Identity $LID -Add @{ProxyAddresses = ""}
Set-ADUser -Identity $LID -Add @{ProxyAddresses = ""}
答案1
仔细查看代码:您正在请求用户的凭据,并将其保存到$domaincredential
以后不会使用的变量中。
向 cmdlet 调用添加-Credential $domaincredential
参数Set-ADuser
:
Set-ADUser -Identity $LID `
-Add @{ProxyAddresses = "NOTES:$name $lastname/DK$LID/UNIBANK"} `
-Credential $domaincredential