|
| 1 | +<# |
| 2 | +.Synopsis |
| 3 | + Script to Create Self Signed Certificate use for code signing. |
| 4 | +.How to use: |
| 5 | + Run the script in PowerShell ISE in Admin mode. |
| 6 | +.Note: |
| 7 | + Run the following command "Set-ExecutionPolicy RemoteSigned" to ensure execution from PowerShell ISE. |
| 8 | +#> |
| 9 | + |
| 10 | +Param( |
| 11 | + [string]$FriendlyName = "DebitExpress Business Solutions", |
| 12 | + [string]$DnsName = "DebitExpress Business Solutions", |
| 13 | + [string]$Subject = "CN=DebitExpress Business Solutions", |
| 14 | + |
| 15 | + [Parameter(Mandatory = $true)] |
| 16 | + [Alias("y")] |
| 17 | + [System.Object]$YearsValid, |
| 18 | + |
| 19 | + [Parameter(Mandatory = $true)] |
| 20 | + [Alias("p")] |
| 21 | + [string]$Pass, |
| 22 | + |
| 23 | + [Parameter(Mandatory = $true)] |
| 24 | + [Alias("n")] |
| 25 | + [string]$CertName, |
| 26 | + |
| 27 | + [Parameter(Mandatory = $true)] |
| 28 | + [Alias("o")] |
| 29 | + [string]$CertFolder, |
| 30 | + |
| 31 | + [SecureString]$SecurePassword |
| 32 | +) |
| 33 | + |
| 34 | +$SecurePassword = ConvertTo-SecureString $Pass -AsPlainText -Force |
| 35 | + |
| 36 | +# Get current date |
| 37 | +$ValidFrom = [DateTime]::Now |
| 38 | + |
| 39 | +$ValidUntill = [DateTime]::Now.AddYears($YearsValid) |
| 40 | + |
| 41 | +# Create certificate folder if not exists |
| 42 | +if (!(Test-Path $CertFolder)) { |
| 43 | + New-Item -Path $CertFolder -Force -ItemType Directory |
| 44 | +} |
| 45 | + |
| 46 | +$CerFilePath = $CertFolder + "\\" + $CertName + ".cer" |
| 47 | +$PfxFilePath = $CertFolder + "\\" + $CertName + ".pfx" |
| 48 | + |
| 49 | +# Create Certificate |
| 50 | +$Cert = New-SelfSignedCertificate -FriendlyName $FriendlyName -DnsName $DnsName -Type CodeSigning -Subject $Subject -NotBefore $ValidFrom -NotAfter $ValidUntill -KeyExportPolicy Exportable -KeyAlgorithm RSA -KeyLength 2048 -HashAlgorithm "SHA256" -Provider "Microsoft Strong Cryptographic Provider" -CertStoreLocation "Cert:\LocalMachine\My" |
| 51 | + |
| 52 | +#Generate .pfx |
| 53 | +Export-PfxCertificate -Cert $Cert -FilePath $PfxFilePath -Password $SecurePassword |
| 54 | + |
| 55 | +#Export to .cer format |
| 56 | +Export-Certificate -Type CERT -Cert $Cert -FilePath $CerFilePath |
| 57 | + |
| 58 | +Write-Host Task Completed ! -ForegroundColor Green |
0 commit comments