11<#
22 . SYNOPSIS
3- Script to create a self signed certificate.
3+ Script to create a self- signed certificate.
44
55 . DESCRIPTION
6- This script allows you to create a self signed certificate and its private key information file.
6+ This script allows you to create a self- signed certificate and its private key information file.
77 The certificate can be used to authenticate identities, like Azure app registrations.
8- The script creates the certificate in the PowerShell working directory.
98 The script also automatically removes the created certificate from the user's keystore.
109
1110 . PARAMETER certificateName <string> [required]
1211 The name of the certificate.
1312
1413 . PARAMETER certificatePwd <string> [required]
15- The name password for the certificate encryption.
14+ The password used for certificate encryption.
1615
1716 . PARAMETER monthsValid <int> [required]
1817 The number of months before the certificate becomes invalid.
18+
19+ . PARAMETER folderPath <string> [required]
20+ The (relative) folder path the certificate should be created in.
1921#>
2022
2123<# ---------------- Program execution ---------------- #>
2224
2325Function Create-SelfSignedCertificate (
24- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][string ]$certificateName ,
25- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][System.Security.SecureString ]$certificatePwd ,
26- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][int ]$monthsValid ) {
27- Try {
28- $certificateName = " $ ( $certificateName ) "
29-
30- Write-Host " Creating certificate..."
31- $cert = New-SelfSignedCertificate - Subject " CN=$ ( $certificateName ) " - CertStoreLocation " Cert:\CurrentUser\My" - KeyExportPolicy Exportable - KeySpec Signature - KeyLength 2048 - KeyAlgorithm RSA - HashAlgorithm SHA256 - NotAfter (Get-Date ).AddMonths($monthsValid )
32- Export-Certificate - Cert $cert - FilePath " ../$ ( $certificateName ) .cer" | Out-Null
33- Write-Host " Successfully created the certificate!" - ForegroundColor Green
34-
35- Write-Host " Creating private key for the certificate..."
36- Export-PfxCertificate - Cert $cert - FilePath " ../$ ( $certificateName ) .pfx" - Password $certificatePwd | Out-Null
37- Write-Host " Successfully created the private key!" - ForegroundColor Green
38-
39- Write-Host " Removing created certificate from the user's personal keystore..."
40- $keyStoreCertThumbPrint = Get-ChildItem - Path " Cert:\CurrentUser\My" | Where-Object { $_.Subject -Match $Name } | Select-Object Thumbprint
41- Remove-Item - Path " Cert:\CurrentUser\My\$ ( $keyStoreCertThumbPrint.Thumbprint ) " - DeleteKey - ErrorAction SilentlyContinue | Out-Null
42- Write-Host " Successfully removed the certificate from the personal keystore!" - ForegroundColor Green
43-
44- Write-Host " `n Done." - ForegroundColor Green
45- }
46- Catch [Exception ] {
47- Write-Host " `n An error occurred: $ ( $_.Exception.Message ) " - ForegroundColor Red
48- throw $_.Exception
26+ [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][string ]$certificateName ,
27+ [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][string ]$certificatePwd ,
28+ [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][int ]$monthsValid ,
29+ [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][string ]$folderPath ) {
30+ Try {
31+ $certificateSecurePwd = ConvertTo-SecureString $certificatePwd - AsPlainText - Force
32+
33+ If ($folderPath.Substring ($folderPath.Length - 1 ) -ne " /" -Or $folderPath.Substring ($folderPath.Length - 1 ) -ne " \" ) {
34+ $folderPath = $folderPath + " /"
4935 }
36+
37+ $fullPath = $folderPath + $certificateName
38+
39+ Write-Host " `n Creating certificate..."
40+ $cert = New-SelfSignedCertificate - Subject " CN=$ ( $certificateName ) " - CertStoreLocation " Cert:\CurrentUser\My" - KeyExportPolicy Exportable - KeySpec Signature - KeyLength 2048 - KeyAlgorithm RSA - HashAlgorithm SHA256 - NotAfter (Get-Date ).AddMonths($monthsValid )
41+ Export-Certificate - Cert $cert - FilePath " $ ( $fullPath ) .cer" | Out-Null
42+
43+ Write-Host " Creating private key for the certificate..."
44+ Export-PfxCertificate - Cert $cert - FilePath " $ ( $fullPath ) .pfx" - Password $certificateSecurePwd | Out-Null
45+
46+ Write-Host " Removing created certificate from the user's personal keystore..."
47+ $keyStoreCertThumbPrint = Get-ChildItem - Path " Cert:\CurrentUser\My" | Where-Object { $_.Subject -Match $Name } | Select-Object Thumbprint
48+ Remove-Item - Path " Cert:\CurrentUser\My\$ ( $keyStoreCertThumbPrint.Thumbprint ) " - DeleteKey - ErrorAction SilentlyContinue | Out-Null
49+
50+ Write-Host " `n ------------------------------------------------------------------------" - ForegroundColor Magenta
51+
52+ Write-Host " `n Successfully created the self-signed certificate!" - ForegroundColor Green
53+ Write-Host " Certificate thumbnail : $ ( $cert.Thumbprint ) " - ForegroundColor Green
54+ Write-Host " Certificate password : $ ( $certificatePwd ) " - ForegroundColor Green
55+ Write-Host " Output folder : $ ( $fullPath ) " - ForegroundColor Green
56+
57+ Write-Host " `n Done." - ForegroundColor Magenta
58+ }
59+ Catch [Exception ] {
60+ Write-Host " `n An error occurred: $ ( $_.Exception.Message ) " - ForegroundColor Red
61+ throw $_.Exception
62+ }
5063}
5164
52- Export-ModuleMember - Function Create- SelfSignedCertificate
65+ Export-ModuleMember - Function Create- SelfSignedCertificate
0 commit comments