-
Notifications
You must be signed in to change notification settings - Fork 1
/
CertificateGenerator.ps1
27 lines (22 loc) · 1.02 KB
/
CertificateGenerator.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function CreateCertificate {
Param(
[Parameter(Mandatory = $true)]
[string]$certificateName,
[Parameter(Mandatory = $true)]
[SecureString]$certificatePassword,
[Parameter(Mandatory = $false)]
[ValidateRange(1, [int]::MaxValue)]
[Int]$expirationInYears = 20
)
$pfxFilePath = $certificateName + ".pfx"
$cerFilePath = $certificateName + ".cer"
$certStartDate = Get-Date
$certEndDate = $certStartDate.AddYears($expirationInYears)
$certPasswordSecureString = ConvertTo-SecureString $certificatePassword -AsPlainText -Force
$cert = New-SelfSignedCertificate -DnsName $certificateName -CertStoreLocation cert:\CurrentUser\My -KeySpec KeyExchange -NotAfter $certEndDate -NotBefore $certStartDate
$certThumbprint = $cert.Thumbprint
$cert = (Get-ChildItem -Path cert:\CurrentUser\My\$certThumbprint)
Export-Certificate -Cert (Get-ChildItem -Path cert:\CurrentUser\My\$certThumbprint) -FilePath $cerFilePath
Export-PfxCertificate -Cert $cert -FilePath $pfxFilePath -Password $certPasswordSecureString
}
CreateCertificate