【问题标题】:Powershell Set-ADUser userCertificate parameter type errorPowershell Set-ADUser userCertificate 参数类型错误
【发布时间】:2017-03-24 17:18:18
【问题描述】:

我有一个脚本,它主要执行以下操作:

$user = Get-ADUser $someUserName -Server $someCertServer -Properties *
$user.userCertificate |% { Set-ADUser $someUserName -Certificates @{Add=$_} }

它将证书数据从证书服务器复制到默认服务器。 $user.userCertificateMicrosoft.ActiveDirectory.Management.ADPropertyValueCollection 类型,$user.userCertificate[0]System.Byte[] 类型。根据docs,我可以通过Byte[] 并且很高兴。通过在上述脚本中使用 foreach 运算符,我得到了 Byte[]。

但是,Powershell 失败并显示错误消息

参数证书要求集合中的所有值都属于同一类型

(并在 @{Add=$_} 下划线;该消息可能不是 100% 准确,因为我必须将其翻译成英文)。由于使用了 foreach 运算符,因此只有一种类型:Byte[]。该错误消息是什么意思?如何将证书推入 ADUser 对象?

我也尝试将 Byte[] 转换为证书对象,但最终得到相同的错误消息:

$cert = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.Import($user.userCertificate[0])
Set-ADUser $someUserName -Certificates @{Add=$cert}

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    我看到的第一个问题是您调用了错误的属性以使示例正常工作。

    $user.userCertificate | ForEach-Object { Set-ADUser $someUserName -Certificates @{Add=$_}}
    

    虽然该对象上有一个名为userCertificate 的属性,其中包含[byte[]] 的集合。要使该代码按您希望的方式工作,您应该使用CertificatesCertificatesSecurity.Cryptography.X509Certificates.X509Certificate 的集合

    因此进行更改后,我能够将另一个用户证书添加到我自己的帐户中。

    $certUser = Get-Aduser "someGuy" -Properties certificates
    $certUser.Certificates | %{ Set-ADUser "ME" -Certificates @{Add=$_}}
    

    注意:最好只向 AD 请求您需要的属性。使用-properties * 是浪费精力和不必要的资源

    虽然没有涵盖这个确切的场景,但MSDN Blogs 上有一篇关于处理证书的精彩文章。


    作为字节数组的证书

    现在我们知道userCertificate 是字节数组的集合,也许我们可以研究一下如何使用它。

    我在使用带有 Set-Aduser 的证书参数的字节数组时遇到问题,即使它应该支持它。我得到了错误

    Set-ADUser : Cannot validate argument on parameter 'Certificates'. Values in the argument collection should be of Type: 'System.Security.Cryptography.X509Certificates.X509Certificate' 
    

    我确实让它工作了,但我必须首先将字节数组转换为一个证书对象,考虑到这似乎是 Certificates 属性已经是什么,这是多余的。

    $certUser.Usercertificate | ForEach-Object{
        Set-ADUser "ME" -certificate @{Add=[System.Security.Cryptography.X509Certificates.X509Certificate]$_}
    }
    

    【讨论】:

    • "没有名为 userCertificate 的属性" 但是,...有!我刚刚重新检查了。但是,我使用-Properties *,而您使用-Properties certificates。这似乎有所作为,我确实得到了和你一样的结果!因此,总而言之,使用该属性更改,我能够使其工作。太棒了,非常感谢你:)
    • 啊,我只能在20小时内奖励赏金。既然你是第一个并且你的答案包含解决方案,我明天会奖励它:)
    • @MarcoAlka 但是,...有 嗯,看起来确实如此。我的错。由于 ldap 属性和下一个示例中的示例,我做出了这个假设,不要使用它。它似乎也不是别名......我会尝试找出差异。也许userCertificate 没有填充相同的数据。 ...... -properties * 提取所有属性(被认为是不好的编码习惯)我只是要求一个。
    • @MarcoAlka 我已经更新了答案,现在可以更好地解释发生了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多