How do I call New-Object for a constructor which takes a single array parameter?

后端 未结 2 1306
心在旅途
心在旅途 2020-12-03 13:17

In PowerShell, I want to use New-Object to call a single-argument .Net constructor new X509Certificate2(byte[] byteArray). The problem is when I do

2条回答
  •  离开以前
    2020-12-03 14:05

    Surprisingly to me, I tried this and it seems it works:

    [byte[]] $certPublicBytes = something
    $cert = [System.Security.Cryptography.X509Certificates.X509Certificate] $certPublicBytes
    return $cert
    

    I don't yet know by what magic it works, so your explanatory comments are appreciated. :)

    (Note: I since found that using square-brackets-type-name as I did above, can also lead to other errors, such as 'Cannot convert value "System.Byte[]" to type "System.Security.Cryptography.X509Certificates.X509Certificate". Error: "Cannot find the requested object.' The explicit New-Object approach recommended by Keith seems better!)

提交回复
热议问题