在 PowerShell 中,我想使用 New-Object
调用单参数 .Net 构造函数 new X509Certificate2(byte[] byteArray)
。问题是当我使用 powershell 中的字节数组执行此操作时,我得到 p>
New-Object : Cannot find an overload for "X509Certificate2" and the argument count: "516".
最佳答案
这种使用new-object
的方法应该有效:
$cert = new-object System.Security.Cryptography.X509Certificates.X509Certificate `
-ArgumentList @(,$bytes)
诀窍是 PowerShell 需要一个构造函数参数数组。当只有一个参数并且它是一个数组时,它可能会混淆 PowerShell 的重载解析算法。上面的代码通过将字节数组放入仅包含该一个元素的数组中来帮助解决此问题。
更新:在 PowerShell >= v5 中,您可以直接调用构造函数,如下所示:
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate]::new($bytes)
关于arrays - 如何为采用单个数组参数的构造函数调用 New-Object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12870109/