英文:
Positional Parameter issue
问题
我有一个脚本,最终会为AD中的每个用户创建一个变量($variableResult),然后遍历每个用户。我已经测试了将所有内容输出到日志文件 - 一切正常。最后一步是让它写入更改($variableResult)到AD属性(POBox)。它会比较这两者,如果它们不同,就会进行覆盖。
这是我遇到的问题:
Set-ADUser: 无法绑定参数 'Identity'。无法将值“CN=xxx,OU=Windows 10,OU=UserAccounts,OU=xxx,OU=xxx,DC=xxx,DC=xxx”转换为类型“Microsoft.ActiveDirectory.Management.ADUser”。错误:“无法将类型为“Deserialized.Microsoft.ActiveDirectory.Management.ADUser”的“CN=xxx,OU=Windows 10,OU=UserAccounts,OU=xxx,OU=xxx,DC=xxx,DC=xxx”值转换为类型“Microsoft.ActiveDirectory.Management.ADUser”。”
这是存储在变量中的内容:
PS > $variableResult
HQCSHOFF
我已经修改了脚本,只搜索我的用户名,在AD中错误地设置了POBox,所以脚本应该进行更正,但我却收到了以下错误:
if( $user.POBox -ne $variableResult ) {
set-aduser $user -Replace @{POBox="$variableResult"}
}
英文:
I have a script that ends up creating a variable for each user in AD ($variableResult), iterating through each user. I have tested everything outputting to a log file - all fine. The last stage is to get it to write changes ($variableResult) to AD Attribute (POBox). It compares the 2 and if they are different it will overwrite.
This is the issue that I have:
Set-ADUser: Cannot bind parameter 'Identity'. Cannot convert value "CN=xxx,OU=Windows 10,OU=UserAccounts,OU=xxx,OU=xxx,DC=xxx,DC=xxx" to type "Microsoft.ActiveDirectory.Management.ADUser". Error: "Cannot convert the "CN=xxx,OU=Windows 10,OU=UserAccounts,OU=xxx,OU=xxx,DC=xxx,DC=net" value of type "Deserialized.Microsoft.ActiveDirectory.Management.ADUser" to type "Microsoft.ActiveDirectory.Management.ADUser"."
Here is what is stored in the variable:
PS > $variableResult
HQCSHOFF
I have modified the script to only search for my user name, set the POBox incorrectly in AD, so that the script should correct but instead i get this error:
if( $user.POBox -ne $variableResult ) {
set-aduser $user -Replace @{POBox="$variableResult"}
}
答案1
得分: 4
错误表明$user
是一个来自远程会话的对象 (Deserialized.Microsoft.ActiveDirectory.Management.ADUser
),错误表明此对象无法强制转换为 ADUser
实例(-Identity
参数的类型):
Get-ADUser [-Identity] <ADUser> ...
经过我的测试,确实如此:
$user = Start-Job { Get-ADUser $env:USERNAME } |
Receive-Job -Wait -AutoRemoveJob
# Get-ADUser: Cannot bind parameter 'Identity'. Cannot convert value "CN=...
Get-ADUser $user
# InvalidArgument: Cannot bind parameter 'Identity'. Cannot convert value "CN=...
[Microsoft.ActiveDirectory.Management.ADUser] $user
简单的解决方法是使用可以用于构建 ADUser
实例的反序列化对象的属性之一:
- 显式名称
- GUID (objectGUID)
- 安全标识符 (objectSid)
- SAM 账户名称 (sAMAccountName)
总之,这应该解决问题:
if ($user.POBox -ne $variableResult) {
Set-ADUser $user.DistinguishedName -Replace @{ POBox = $variableResult }
}
有关更多详细信息,请参阅反序列化对象。
英文:
The error implies that $user
is an object coming from a remote session (Deserialized.Microsoft.ActiveDirectory.Management.ADUser
) and what the error is stating is that this object cannot be coerced into an ADUser
instance (the parameter type of -Identity
):
Get-ADUser [-Identity] <ADUser> ...
After testing this myself, this is indeed the case:
$user = Start-Job { Get-ADUser $env:USERNAME } |
Receive-Job -Wait -AutoRemoveJob
# Get-ADUser: Cannot bind parameter 'Identity'. Cannot convert value "CN=...
Get-ADUser $user
# InvalidArgument: Cannot bind parameter 'Identity'. Cannot convert value "CN=...
[Microsoft.ActiveDirectory.Management.ADUser] $user
The simple workaround is to use one of the properties from the deserialized object that can be used to construct an ADUser
instance:
- A distinguished name
- A GUID (objectGUID)
- A security identifier (objectSid)
- A SAM account name (sAMAccountName)
In summary, this should solve the problem:
if ($user.POBox -ne $variableResult) {
Set-ADUser $user.DistinguishedName -Replace @{ POBox = $variableResult }
}
See Deserialized objects for more details.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论