rj07thomas

Create single hostname certificate with PowerShell

Jun 6th, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #Script starts by clearing terminal screen
  2. Clear-Host
  3.  
  4. #----Retrives date into string and modifies it to remove special characters
  5.  
  6. $dt = Get-Date
  7.  
  8. $dtShort = $dt.ToShortDateString()
  9. $dtShort = $dtShort -replace '[/]',""
  10.  
  11. $tmShort = $dt.ToShortTimeString()
  12. $tmShort = $tmShort -replace '[:]',"-"
  13.  
  14. #----Asks user to input a SAN/ hostname
  15.  
  16. $SAN1 = Read-Host "Please enter SAN 1"
  17.  
  18. #----Creates folder structure based on SAN/hostname
  19.  
  20. New-Item -Path ".\" -Name "$SAN1" -ItemType "directory"
  21. New-Item -Path ".\$SAN1" -Name "$dtShort" -ItemType "directory"
  22. New-Item -Path ".\$SAN1\$dtShort" -Name "$tmShort" -ItemType "directory"
  23.  
  24. #----Assigns file path and name to the certificate request file, and copies the request template to that location
  25.  
  26. $fileName = ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.inf"
  27. copy .\00certDetails.inf $filename
  28.  
  29. #----Creates additional SANs with FQDNs
  30.  
  31. $SAN1fqdn1 = $SAN1+".domain.local"
  32. $SAN1fqdn2 = $SAN1+".otherdomain.local"
  33.  
  34. #----Modifies a SAN string to match the formatting required for certreq.exe
  35.  
  36. $fullSAN = '{text}dns='+$SAN1+'&dns='+$SAN1fqdn1+'&dns='+$SAN1fqdn2
  37.  
  38. #----Gets the contents of the request file and replaces placeholders with actual values for the initial hostname and additional SANs
  39.  
  40. (Get-Content -Path $filename) |
  41. ForEach-Object {$_ -Replace 'FQDN', $SAN1fqdn1} |
  42. Set-Content -Path $filename
  43.  
  44. (Get-Content -Path $filename) |
  45. ForEach-Object {$_ -Replace 'CUSTOMSANS', $fullSAN} |
  46. Set-Content -Path $filename
  47.  
  48. #----Start of certreq/certutil process: converts the modified .inf template file into a .req file
  49.  
  50. $host.ui.RawUI.ForegroundColor = "red"
  51. Write-Host "Step 1"
  52. $host.ui.RawUI.ForegroundColor = "white"
  53. certreq -new $fileName ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.req"
  54.  
  55. #----Checks the .req file with certutil
  56.  
  57. $host.ui.RawUI.ForegroundColor = "red"
  58. Write-Host "Step 2"
  59. $host.ui.RawUI.ForegroundColor = "white"
  60. certutil ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.req"
  61.  
  62. #----Submits the .req file to the CA and outputs to a .cer file
  63.  
  64. $host.ui.RawUI.ForegroundColor = "red"
  65. Write-Host "Step 3"
  66. $host.ui.RawUI.ForegroundColor = "white"
  67. certreq -attrib "CertificateTemplate:TemplateWithoutSpaces" -submit ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.req" ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.cer"
  68.  
  69. #----Installs the certificate
  70.  
  71. $host.ui.RawUI.ForegroundColor = "red"
  72. Write-Host "Step 4"
  73. $host.ui.RawUI.ForegroundColor = "white"
  74. certreq -accept ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.cer"
  75.  
  76. #Script ends after installing the certificate
Advertisement
Add Comment
Please, Sign In to add comment