-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generate_Certificate.ps1
94 lines (74 loc) · 3.64 KB
/
Generate_Certificate.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<#PSScriptInfo
.VERSION 2.00
.GUID 134de175-8fd8-4938-9812-053ba39eed83
.AUTHOR HAO BAN/hao.ban@ehealthsask.ca/banhao@gmail.com
.COMPANYNAME
.COPYRIGHT
.TAGS
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.PRIVATEDATA
.SYNOPSIS
.EXAMPLE
.DESCRIPTION
Creation Date: <02/10/2022>
.Parameter
#>
#-------------------------------------------------------------------------------------------------------------------------------------------------------
#variables
$PolicyFile = $Args[0]
$ENVIRONMENT = $Args[1]
$CN = Get-Content .\$PolicyFile | findstr "Subject" | %{ $_.Split(',')[0]; } | %{ $_.Split('=')[2]; }
$Email = $(Get-Content .\$PolicyFile | findstr "Subject" | %{ $_.Split('=')[-1]; }).Trim('"')
$CertificateTemplate = $(Get-Content .\$PolicyFile | findstr "CertificateTemplate" | %{ $_.Split('=')[1]; }).trim()
$FriendlyName = $(Get-Content .\$PolicyFile | findstr "FriendlyName" | %{ $_.Split('=')[1]; }).trim()
$Folder = "$PSScriptRoot\Certificates"
$CSRFile = $PolicyFile + ".csr"
if (-not (Test-Path -Path $Folder)){
New-Item -Path $Folder -ItemType "directory"
}
Certreq.exe -New .\$PolicyFile $Folder\$CSRFile
Start-Sleep -s 3
if( test-path $Folder\$CSRFile ){
Write-OutPut $CSRFile "generated successfully" >> .\Certificate_output.log
### Generate the Certificate.
$CertFile = $PolicyFile + ".cer"
if ($ENVIRONMENT -eq 'PROD' -and $CertificateTemplate -eq 'ExternalClientAuth4yearOffline'){
$CAServer = ""
}
elseif($ENVIRONMENT -eq 'PROD' -and ($CertificateTemplate -eq 'Client1' -or $CertificateTemplate -eq 'Client2')){
$CAServer = ""
}
elseif($ENVIRONMENT -eq 'UAT'){
$CAServer = ""
}
$RequestIdOutPut = certreq -f -q -Submit -Attrib "CertificateTemplate:$CertificateTemplate" -config $CAServer $Folder\$CSRfile $Folder\$CertFile
$RequestId = $RequestIdOutPut[0] | %{ $_.Split(':')[1]; } | foreach{ $_.ToString().Trim() }
Start-Sleep -s 1
if( test-path $Folder\$CertFile ){
Write-OutPut $CertFile "generated successfully. Request ID: " $RequestId "on" $CAServer >> .\Certificate_output.log
$Thumbprint = $($(certutil $Folder\$CertFile)[-4] -split ":")[1] | foreach{ $_.ToString().Trim()}
$Expiry = $($(certutil $Folder\$CertFile)[17] -split " ")[2] | foreach{ $_.ToString().Trim()}
### Import the Certificate
Import-Certificate -FilePath $Folder\$CertFile -CertStoreLocation cert:\CurrentUser\My
Start-Sleep -s 1
### Export the Certificate
$PFXFile = $FriendlyName + ".pfx"
$PASSWORD = -join ((48..57) + (97..122) | Get-Random -Count 12 | % {[char]$_})
$SecurePASSWD = ConvertTo-SecureString -String $PASSWORD -Force -AsPlainText
Get-ChildItem -Path cert:\CurrentUser\my\$Thumbprint | Export-PfxCertificate -FilePath $Folder\$PFXFile -Password $SecurePASSWD
Start-Sleep -s 1
if( test-path $Folder\$PFXFile ){
Write-OutPut $PFXFile "generated successfully" >> .\Certificate_output.log
$EmailSubject = "Request #" + $RequestId + ": Certificate(s) from eHS to Be Installed for [*" + $CN + "*] on" + $CAServer
Send-MailMessage -SmtpServer ExchangeServer -To $Email -From WebExBot@company.com -Subject $EmailSubject -Body $PASSWORD -Attachments $Folder\$PFXFile
}else{ Write-OutPut "Didn't generate the PFX File successfully" >> .\Certificate_output.log }
}else{ Write-OutPut "Didn't generate the Certificate successfully" >> .\Certificate_output.log }
}else{ Write-OutPut "Didn't generate the CSR File successfully" >> .\Certificate_output.log }
Write-OutPut "---------------------------------------------------------" >> .\Certificate_output.log
del .\$PolicyFile