Skip to content

Commit 3e6fc34

Browse files
authored
Create Sign_files.ps1
1 parent 17fe8f2 commit 3e6fc34

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Sign_files.ps1

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Sign all scripts in folder recursively by a self-signed certificate
2+
$CertName = "Team Sophia"
3+
$FolderPath = "src"
4+
$ExtensionsToSearchIn = @(".ps1", ".psm1", ".psd1")
5+
# Get-ChildItem -Path Cert:\LocalMachine\My, Cert:\CurrentUser\My | Where-Object -FilterScript {$_.Subject -eq "CN=$CertName"} | Remove-Item
6+
7+
# Generate a self-signed Authenticode certificate in the local computer's personal certificate store
8+
$Parameters = @{
9+
Subject = $CertName
10+
NotAfter = (Get-Date).AddMonths(24)
11+
CertStoreLocation = "Cert:\LocalMachine\My"
12+
Type = "CodeSigningCert"
13+
}
14+
$authenticode = New-SelfSignedCertificate @Parameters
15+
16+
# Add the self-signed Authenticode certificate to the computer's root certificate store
17+
# Create an object to represent the LocalMachine\Root certificate store
18+
$rootStore = [System.Security.Cryptography.X509Certificates.X509Store]::new("Root","LocalMachine")
19+
# Open the root certificate store for reading and writing
20+
$rootStore.Open("ReadWrite")
21+
# Add the certificate stored in the $authenticode variable
22+
$rootStore.Add($authenticode)
23+
# Close the root certificate store
24+
$rootStore.Close()
25+
26+
# Add the self-signed Authenticode certificate to the computer's trusted publishers certificate store
27+
# Create an object to represent the LocalMachine\TrustedPublisher certificate store
28+
$publisherStore = [System.Security.Cryptography.X509Certificates.X509Store]::new("TrustedPublisher","LocalMachine")
29+
# Open the TrustedPublisher certificate store for reading and writing
30+
$publisherStore.Open("ReadWrite")
31+
# Add the certificate stored in the $authenticode variable
32+
$publisherStore.Add($authenticode)
33+
# Close the TrustedPublisher certificate store
34+
$publisherStore.Close()
35+
36+
# Get the code-signing certificate from the local computer's certificate store with the name "Sophia Authenticode" and store it to the $codeCertificate variable
37+
$codeCertificate = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object -FilterScript {$_.Subject -eq "CN=$CertName"}
38+
39+
# TimeStampServer specifies the trusted timestamp server that adds a timestamp to script's digital signature
40+
# Adding a timestamp ensures that your code will not expire when the signing certificate expires
41+
# -Include *.ps1, *.psm1, *.psd1 is obvious, but it's slow
42+
# There is no need to user $PSScriptRoot\$FolderPath
43+
Get-ChildItem -Path $FolderPath -Recurse -File | Where-Object -FilterScript {$_.Extension -in $ExtensionsToSearchIn} | ForEach-Object -Process {
44+
$Parameters = @{
45+
FilePath = $_.FullName
46+
Certificate = $codeCertificate
47+
TimeStampServer = "http://timestamp.digicert.com"
48+
}
49+
Set-AuthenticodeSignature @Parameters
50+
}

0 commit comments

Comments
 (0)