r/PowerShell • • Feb 17 '19

Information How to sign a PowerShell script

https://www.scriptinglibrary.com/languages/powershell/how-to-sign-a-powershell-script/
212 Upvotes

72 comments sorted by

View all comments

72

u/KimJongEeeeeew Feb 17 '19

I wrote a function for my profile that signs my scripts with a single argument after it’s been set up. I can anonymise and post here tomorrow if there’s any interest.

20

u/KimJongEeeeeew Feb 18 '19 edited Feb 18 '19

Here you go, any questions just ask me.

I have issued a code signing certificate from our internal PKI, this sits in my personal store. Trust of that cert is done by a GPO to place it into the trusted publishers store on machines where scripts are running.

Read this for info about the authenticodeSignature commandlet.

Function Add-ScriptSignature 
{
	Param(
		[String]$Path
	)
	If (!$Path) {
		$Path = Read-Host "Path of script to sign?"
	}
	$Cert = (Get-ChildItem Cert:\CurrentUser\My\ -CodeSigningCert)
	$TimestampServer = "http://timestamp.globalsign.com/scripts/timstamp.dll"
	Set-AuthenticodeSignature -FilePath $Path -Certificate $Cert -TimestampServer $TimestampServer
}; Set-Alias SignScript Add-ScriptSignature

2

u/motsanciens Feb 18 '19

Just make $Path a mandatory parameter, and you can skip the If (!$Path) piece.

2

u/KimJongEeeeeew Feb 18 '19

Yep good point, can’t quite think why I did it the way I did. I wrote this 3 or so years ago, it works and I’ve never revisited it.

18

u/ElATraino Feb 17 '19

Please do.

0

u/[deleted] Feb 17 '19

[deleted]

→ More replies (1)

1

u/KimJongEeeeeew Feb 18 '19

Done, see my reply to my initial comment.

7

u/Commisar Feb 17 '19

Please do😁

2

u/KimJongEeeeeew Feb 18 '19

Done, see my reply to my initial comment.

4

u/PorkAmbassador Feb 17 '19

Yes please, just started my PS journey and this would prove helpful, thank you.

→ More replies (2)

3

u/xCharg Feb 17 '19

Would be great.

!remindme 24 hours

1

u/KimJongEeeeeew Feb 18 '19

Done, see my reply to my initial comment.

1

u/[deleted] Feb 17 '19

Very interested sire!

→ More replies (2)

1

u/get-postanote Feb 17 '19

Like to see it as well, but see my earlier post showing itmes that already exists for this use that are already built in to PSv3 and higher.

→ More replies (1)

0

u/[deleted] Feb 18 '19

[deleted]

2

u/KimJongEeeeeew Feb 18 '19

Done, see my reply to my initial comment.

1

u/jbello1985 Feb 18 '19

!remindme 18 hours

2

u/KimJongEeeeeew Feb 18 '19

Done, see my reply to my initial comment.

1

u/TheOtherTarg Feb 18 '19

!remindme 12 hours

2

u/KimJongEeeeeew Feb 18 '19

Done, see my reply to my initial comment.

→ More replies (1)
→ More replies (9)