UnknownError when using Powershell ISE to Set-AuthenticodeSignature

This is discussed on Microsoft Connect here

When attempting to assign a certificate to a powershell script, it does not assign and gives a status of ‘UnknownError’

$cert = @(Get-ChildItem cert:\CurrentUser\My -codesigning | Where-Object {$_.subject -match "Powershell User"})[0]
Set-AuthenticodeSignature "\\path\myscript.ps1" $cert

SignerCertificate  Status          Path 
—————–  ——          —- 
                   UnknownError    myscript.ps1

This happens because when using Powershell ISE the default encoding is ‘Unicode Big Endian’

If the file is recreated using UTF-8 then the script is correctly assigned a certificate

#recreate the script into a new file
type "\\Path\myscript.ps1" | out-file "\\Path\myscript_New.ps1" -encoding utf8
$cert = @(Get-ChildItem cert:\CurrentUser\My -codesigning `
   | Where-Object {$_.subject -match "Powershell User"})[0]
#use the UTF-8 encoded file
Set-AuthenticodeSignature "\\Path\myscript_New.ps1" $cert

SignerCertificate  Status          Path 
—————–  ——          —-  
Cert_Thumbprint    Valid           myscript_New.ps1

Advertisements

3 Responses to UnknownError when using Powershell ISE to Set-AuthenticodeSignature

  1. Pingback: Creating Self-Signing Certificates and assigning to Powershell scripts « Mohsin's DBA Blog

  2. Pingback: Confluence: Team PC

  3. Pingback: Confluence: Team PC

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: