4 Commands to maintain a local code singing cert.
6 This is a hack to solve allowing PowerShell to trust a PowerShell profile hosted on a UNC path, without allowing 'Unrestricted' execution policy.
8 I created this because it annoys me that my mandatory UNC path profile counts as a remote script, and I'm too pedantic to un-restrict all remote scripts just to allow my profile to load.
12 Works fine under PowerShell Core 6 with ExecutionPolicy RemoteSigned. PowerShell 7
16 Set-ExecutionPolicy RemoteSigned
18 Import-Module c:\src\dotfiles\psmodules\sign.psm1
23 # Install it - you will be promted whether to trust it.
25 > Install-CodeSignCert
27 # Sign your UNC path PowerShell profile
28 > Invoke-CodeSign $profile
32 $certName = "Local Code Sign Cert"
35 function New-CodeSignCert {
36 $cert = New-SelfSignedCertificate -CertStoreLocation Cert:\CurrentUser\My -Type CodeSigningCert -Subject $certName -KeyAlgorithm RSA -KeyLength 2048 -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -KeyUsage "DigitalSignature"
40 function Get-CodeSignCert {
41 $certName = "Local Code Sign Cert"
42 return Get-ChildItem -Path Cert:\CurrentUser\My | Select-Object -First 1
43 # | ? Subject -EQ "CN=$certName"
46 function Install-CodeSignCert {
48 $cert = Get-CodeSignCert
49 Move-Item -Path $cert.PSPath -Destination "Cert:\CurrentUser\Root"
52 function Invoke-CodeSign {
56 $cert = Get-CodeSignCert
57 Set-AuthenticodeSignature -FilePath $Path -Certificate $cert
60 Invoke-CodeSignProfile {
61 Invoke-CodeSign -Path $profile
64 Export-ModuleMember -Function New-CodeSignCert
65 Export-ModuleMember -Function Get-CodeSignCert
66 Export-ModuleMember -Function Install-CodeSignCert
67 Export-ModuleMember -Function Invoke-CodeSign
68 Export-ModuleMember -Function Invoke-CodeSignProfile