Powershell:如何从SearchResultEntry对象的属性中获取base64值?

Powershell:如何从SearchResultEntry对象的属性中获取base64值?,powershell,ldap,base64,pki,tobase64string,Powershell,Ldap,Base64,Pki,Tobase64string,我的目标是从LDAP连接获取CA的已发布CRL。 我有一个搜索LDAP(不是Active Directory!)的功能,它会按预期返回System.DirectoryServices.Protocols.SearchResultEntryCollection $results = LDAPsearch "$_LDAP_server`:$_LDAP_searchPort" "cn=$CA,$_LDAP_searchBase" '(&(certific

我的目标是从LDAP连接获取CA的已发布CRL。 我有一个搜索LDAP(不是Active Directory!)的功能,它会按预期返回
System.DirectoryServices.Protocols.SearchResultEntryCollection

$results = LDAPsearch "$_LDAP_server`:$_LDAP_searchPort" "cn=$CA,$_LDAP_searchBase" '(&(certificateRevocationList=*))'
ForEach ($element in $results){
    $element.Attributes['cn'].GetValues('string')
    $element.Attributes['certificateRevocationList;binary'].GetValues('string')
}
上面正确读取了返回的每个元素的
cn
属性值,但是
certificaterejournalist
以一种奇怪的格式返回,它与我期望的Base64字符串完全不对应(例如,如果将数据导出到LDIF文件或使用Linux ldapsearch命令,则可以读取该字符串)

如何获取实际的Base64值

不幸的是,您只能将“byte[]”或“string”作为参数传递给GetValues方法(这里的“Base64String”选项对我很有用,但是

下面的当前输出(其中正确写入了
cn
值,但未写入
证书职业列表
):


将原始CRL检索为
字节[]
,然后自己转换为base64:

$crlBin = $element.Attributes['certificateRevocationList;binary'].GetValues('byte[]')
$crlB64 = [Convert]::ToBase64String($crlBin)

只是添加了我最终使用的选项,因为我的最终目标实际上是将CRL保存为一个文件(并能够解析它),所以简单得多:

这将以DER格式写入实际的CRL文件(然后,如果需要,可以使用
certutil
openssl
轻松切换到PEM)。我最初的想法是从提取的Base64值构造一个PEM格式的CRL文件,但我看得太远了


我留下马蒂亚斯的回答作为答案,因为他实际上最好地回答了我的问题;最后我没有指定我想要一个CRL文件。

谢谢。我只需要将
|%{$}
作为
GetValues('byte[])
返回一个数组(我只希望从中返回一个值)。。。或者只需选择第一个对象:
GetValues('byte[])[0]
。这样就行了。
$crlBin = $element.Attributes['certificateRevocationList;binary'].GetValues('byte[]')[0]
[IO.File]::WriteAllBytes("$_localDir\CRL_$CA.crl",$crlBin)