PowerShell Code Signing Unknown Error

Michael Hathaway 21 Reputation points
2023-04-03T11:12:10.29+00:00

Hi Folks,

Am hitting a bit of an issue when trying to sign some powershell scripts, I am following this basic articlehttps://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_signing?view=powershell-7.3

I am using certificates from my own PKI, the root and issuing CA certs have been imported into the machine trust stores and the code signing certificate is valid, the issue comes when I run the step to sign the powershell script, I get an unknownerror.

CodeSign Screenshot 2023-04-03 120345

If I run the following I do see my code signing cert in the cert store

code signing list Screenshot 2023-04-03 121131

Any ideas?

Thanks

Mike

Windows for business Windows Client for IT Pros Devices and deployment Configure application groups
Windows for business Windows Server User experience PowerShell
Windows for business Windows Server Devices and deployment Configure application groups
Windows for business Windows Client for IT Pros User experience Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-04-03T14:34:36.8766667+00:00

    Looking at the description of the Set-AuthenticodeSignature, both the scripts' path and the certificate both have a "Position" value of 1. That may be an error in the help file, but why not try using the parameter names?

    Set-AuthenticodeSignature -Path add-signature.ps1 -Certificate $cert
    

  2. Rich Matheisen 47,901 Reputation points
    2023-04-03T19:08:19.8233333+00:00

    Open your Add-Signature.ps1 script file in a hex editor (I use this one: https://mh-nexus.de/en/hxd/) and examine the 1st two or 3 bytes of the file.

    • Unicode-16 BigEndian = 0xFF 0xFF
    • Unicode-16 LittleEndian = 0xFF 0xFE
    • UTF-8 = 0xFF 0xBB 0x BF
    • UTF-8 (No BOM) = just the data in the file

    I don't know if still true, but the PowerShell ISE used to save files in Unicode-16 BigEndian, and that used to cause this problem with code signing.

    0 comments No comments

  3. Gary Nebbett 6,216 Reputation points
    2023-04-04T07:21:37.41+00:00

    Hello Michael, Can you check the following two things:

    1. Details of the signing certificate.
    2. Whether a signature has been added to the .ps1 file

    I just tried the sequence of commands that you used and got similar results. In my case, the certificate "properties" (external to the certificate, can be modified) enabled all "purposes", but the certificate did not contain an "Enhanced Key Usage" or similar extension specifying code signing.

    The .ps1 file was modified, but PowerShell also tried to verify the signature and ETW tracing showed that this failed because of "A certificate's basic constraint extension has not been observed.".

    Your problem may differ in some details from mine, but the essence might be the same.

    Gary

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.