将ADPropertyValueCollection转换为System.Collections.Hashtable

时间:2019-03-29 14:58:44

标签: powershell

当我这样读取AD用户的“证书”属性时:

$allProfileRawCerts = (Get-ADUser -Server example.com -Filter {EmailAddress -eq $Mail} -Property Certificates).Certificates

结果数据类型为ADPropertyValueCollection。

IsPublic IsSerial Name                                     BaseType                                                                                                                                         
-------- -------- ----                                     --------                                                                                                                                         
True     False    ADPropertyValueCollection                System.Collections.CollectionBase   

但是,如果我要使用它来设置属性,则显然应该使用不同的数据类型:

Get-ADUser -Server example.com -Filter {EmailAddress -eq $Mail} | Set-ADUser -Certificates $array
Set-ADUser : Cannot convert 'Microsoft.ActiveDirectory.Management.ADPropertyValueCollection' to the type 'System.Collections.Hashtable' required by parameter 'Certificates'. Specified method is not 
supported.
At line:1 char:100
+ ... 68 -Filter {EmailAddress -eq $Mail} | Set-ADUser -Certificates $allProfileRawCerts
+                                                                    ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADUser

如何转换它以便可以使用?

1 个答案:

答案 0 :(得分:0)

结果证明RTFM是一个很好的建议。

-Certificate不仅仅接受证书列表,它还接受“ operation” =“ certificate”的哈希表,例如:

-Certificates @{Add=value1,value2,...};@{Remove=value3,value4,...}

所以答案是(我认为仍在测试中):

Get-ADUser -Server example.com -Filter {EmailAddress -eq $Mail} | Set-ADUser -Certificates @{Add=$allpProfileCerts}