Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Script starts by clearing terminal screen
- Clear-Host
- #----Retrives date into string and modifies it to remove special characters
- $dt = Get-Date
- $dtShort = $dt.ToShortDateString()
- $dtShort = $dtShort -replace '[/]',""
- $tmShort = $dt.ToShortTimeString()
- $tmShort = $tmShort -replace '[:]',"-"
- #----Asks user to input multiple SANs/ hostnames
- $SAN1 = Read-Host "Please enter SAN 1"
- $SAN2 = Read-Host "Please enter SAN 2"
- $SAN3 = Read-Host "Please enter SAN 3"
- #----Creates folder structure based on SAN/hostname
- New-Item -Path ".\" -Name "$SAN1" -ItemType "directory"
- New-Item -Path ".\$SAN1" -Name "$dtShort" -ItemType "directory"
- New-Item -Path ".\$SAN1\$dtShort" -Name "$tmShort" -ItemType "directory"
- #----Assigns file path and name to the certificate request file, and copies the request template to that location
- $fileName = ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.inf"
- copy .\00certDetails.inf $filename
- #----Creates additional SANs with FQDNs
- $SAN1fqdn1 = $SAN1+".your.domain.here"
- $SAN1fqdn2 = $SAN1+".your.otherdomain.here"
- $SAN2fqdn1 = $SAN2+".your.domain.here"
- $SAN2fqdn2 = $SAN2+".your.otherdomain.here"
- $SAN3fqdn1 = $SAN3+".your.domain.here"
- $SAN3fqdn2 = $SAN3+".your.otherdomain.here"
- #----Modifies a SAN string to match the formatting required for certreq.exe
- $fullSAN = '{text}dns='+$SAN1+'&dns='+$SAN1fqdn1+'&dns='+$SAN1fqdn2+'&dns='+$SAN2+'&dns='+$SAN2fqdn1+'&dns='+$SAN2fqdn2+'&dns='+$SAN3+'&dns='+$SAN3fqdn1+'&dns='+$SAN3fqdn2
- #----Gets the contents of the request file and replaces placeholders with actual values for the initial hostname and additional SANs
- (Get-Content -Path $filename) |
- ForEach-Object {$_ -Replace 'FQDN', $SAN1fqdn1} |
- Set-Content -Path $filename
- (Get-Content -Path $filename) |
- ForEach-Object {$_ -Replace 'CUSTOMSANS', $fullSAN} |
- Set-Content -Path $filename
- #----Start of certreq/certutil process: converts the modified .inf template file into a .req file
- $host.ui.RawUI.ForegroundColor = "red"
- Write-Host "Step 1"
- $host.ui.RawUI.ForegroundColor = "white"
- certreq -new $fileName ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.req"
- #----Checks the .req file with certutil
- $host.ui.RawUI.ForegroundColor = "red"
- Write-Host "Step 2"
- $host.ui.RawUI.ForegroundColor = "white"
- certutil ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.req"
- #----Submits the .req file to the CA and outputs to a .cer file
- $host.ui.RawUI.ForegroundColor = "red"
- Write-Host "Step 3"
- $host.ui.RawUI.ForegroundColor = "white"
- certreq -attrib "CertificateTemplate:TemplateWithoutSpaces" -submit ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.req" ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.cer"
- #----Installs the certificate
- $host.ui.RawUI.ForegroundColor = "red"
- Write-Host "Step 4"
- $host.ui.RawUI.ForegroundColor = "white"
- certreq -accept ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.cer"
- #Script ends after installing the certificate
Advertisement
Add Comment
Please, Sign In to add comment