rj07thomas

Create 3-hostname certificate with PowerShell

Oct 19th, 2023
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 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 multiple SANs/ hostnames
  15.  
  16. $SAN1 = Read-Host "Please enter SAN 1"
  17. $SAN2 = Read-Host "Please enter SAN 2"
  18. $SAN3 = Read-Host "Please enter SAN 3"
  19.  
  20. #----Creates folder structure based on SAN/hostname
  21.  
  22. New-Item -Path ".\" -Name "$SAN1" -ItemType "directory"
  23. New-Item -Path ".\$SAN1" -Name "$dtShort" -ItemType "directory"
  24. New-Item -Path ".\$SAN1\$dtShort" -Name "$tmShort" -ItemType "directory"
  25.  
  26. #----Assigns file path and name to the certificate request file, and copies the request template to that location
  27.  
  28. $fileName = ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.inf"
  29. copy .\00certDetails.inf $filename
  30.  
  31. #----Creates additional SANs with FQDNs
  32.  
  33. $SAN1fqdn1 = $SAN1+".your.domain.here"
  34. $SAN1fqdn2 = $SAN1+".your.otherdomain.here"
  35.  
  36. $SAN2fqdn1 = $SAN2+".your.domain.here"
  37. $SAN2fqdn2 = $SAN2+".your.otherdomain.here"
  38.  
  39. $SAN3fqdn1 = $SAN3+".your.domain.here"
  40. $SAN3fqdn2 = $SAN3+".your.otherdomain.here"
  41.  
  42. #----Modifies a SAN string to match the formatting required for certreq.exe
  43.  
  44. $fullSAN = '{text}dns='+$SAN1+'&dns='+$SAN1fqdn1+'&dns='+$SAN1fqdn2+'&dns='+$SAN2+'&dns='+$SAN2fqdn1+'&dns='+$SAN2fqdn2+'&dns='+$SAN3+'&dns='+$SAN3fqdn1+'&dns='+$SAN3fqdn2
  45.  
  46. #----Gets the contents of the request file and replaces placeholders with actual values for the initial hostname and additional SANs
  47.  
  48. (Get-Content -Path $filename) |
  49. ForEach-Object {$_ -Replace 'FQDN', $SAN1fqdn1} |
  50. Set-Content -Path $filename
  51.  
  52. (Get-Content -Path $filename) |
  53. ForEach-Object {$_ -Replace 'CUSTOMSANS', $fullSAN} |
  54. Set-Content -Path $filename
  55.  
  56. #----Start of certreq/certutil process: converts the modified .inf template file into a .req file
  57.  
  58. $host.ui.RawUI.ForegroundColor = "red"
  59. Write-Host "Step 1"
  60. $host.ui.RawUI.ForegroundColor = "white"
  61. certreq -new $fileName ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.req"
  62.  
  63. #----Checks the .req file with certutil
  64.  
  65. $host.ui.RawUI.ForegroundColor = "red"
  66. Write-Host "Step 2"
  67. $host.ui.RawUI.ForegroundColor = "white"
  68. certutil ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.req"
  69.  
  70. #----Submits the .req file to the CA and outputs to a .cer file
  71.  
  72. $host.ui.RawUI.ForegroundColor = "red"
  73. Write-Host "Step 3"
  74. $host.ui.RawUI.ForegroundColor = "white"
  75. certreq -attrib "CertificateTemplate:TemplateWithoutSpaces" -submit ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.req" ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.cer"
  76.  
  77. #----Installs the certificate
  78.  
  79. $host.ui.RawUI.ForegroundColor = "red"
  80. Write-Host "Step 4"
  81. $host.ui.RawUI.ForegroundColor = "white"
  82. certreq -accept ".\$SAN1\$dtShort\$tmShort\00certDetails$SAN1.cer"
  83.  
  84. #Script ends after installing the certificate
Advertisement
Add Comment
Please, Sign In to add comment