smartguy5000

Enroll-WebServerCert

Sep 20th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [cmdletbinding()]
  2. Param()
  3.  
  4. function New-CertificateRequest {
  5. Param(
  6. [Parameter(Mandatory=$true, HelpMessage = "Please enter the subject beginning with CN=")]
  7. [ValidatePattern("CN=")]
  8. [string]$subject,
  9. [Parameter(Mandatory=$false, HelpMessage = "Please enter the SAN domains as a comma separated list")]
  10. [array]$SANs,
  11. [Parameter(Mandatory=$false, HelpMessage = "Please enter the Online Certificate Authority")]
  12. [string]$OnlineCA,
  13. [Parameter(Mandatory=$false, HelpMessage = "Please enter the Online Certificate Authority")]
  14. [string]$CATemplate = "WebServer"
  15. )
  16.  
  17.     ### Preparation
  18.     $subjectDomain = $subject.split(',')[0].split('=')[1]
  19.     If ($subjectDomain -match "\*.")
  20.         {
  21.             $subjectDomain = $subjectDomain -replace "\*", "star"
  22.         }
  23.     $CertificateINI = "$subjectDomain.ini"
  24.     $CertificateREQ = "$subjectDomain.req"
  25.     $CertificateRSP = "$subjectDomain.rsp"
  26.     $CertificateCER = "$subjectDomain.cer"
  27.  
  28.     ### INI file generation
  29.     new-item -type file $CertificateINI -force
  30.     add-content $CertificateINI '[Version]'
  31.     add-content $CertificateINI 'Signature="$Windows NT$"'
  32.     add-content $CertificateINI ''
  33.     add-content $CertificateINI '[NewRequest]'
  34.     $temp = 'Subject="' + $subject + '"'
  35.     add-content $CertificateINI $temp
  36.     add-content $CertificateINI 'Exportable=TRUE'
  37.     add-content $CertificateINI "FriendlyName=$env:computername.$env:userdnsdomain - Web Server $($(Get-Date).ToShortDateString())"
  38.     add-content $CertificateINI 'KeyLength=2048'
  39.     add-content $CertificateINI 'KeySpec=1'
  40.     add-content $CertificateINI 'KeyUsage=0xA0'
  41.     add-content $CertificateINI 'MachineKeySet=True'
  42.     add-content $CertificateINI 'ProviderName="Microsoft RSA SChannel Cryptographic Provider"'
  43.     add-content $CertificateINI 'ProviderType=12'
  44.     add-content $CertificateINI 'SMIME=FALSE'
  45.     add-content $CertificateINI 'RequestType=PKCS10'
  46.     add-content $CertificateINI '[Strings]'
  47.     add-content $CertificateINI 'szOID_ENHANCED_KEY_USAGE = "2.5.29.37"'
  48.     add-content $CertificateINI 'szOID_PKIX_KP_SERVER_AUTH = "1.3.6.1.5.5.7.3.1"'
  49.     add-content $CertificateINI 'szOID_PKIX_KP_CLIENT_AUTH = "1.3.6.1.5.5.7.3.2"'
  50.     if ($SANs)
  51.         {
  52.             add-content $CertificateINI 'szOID_SUBJECT_ALT_NAME2 = "2.5.29.17"'
  53.             add-content $CertificateINI '[Extensions]'
  54.             add-content $CertificateINI '2.5.29.17 = "{text}"'
  55.    
  56.             foreach ($SAN in $SANs)
  57.                 {
  58.                     $temp = '_continue_ = "dns=' + $SAN + '&"'
  59.                     add-content $CertificateINI $temp
  60.                 }
  61.         }
  62.  
  63.     ### Certificate request generation
  64.     if (test-path $CertificateREQ)
  65.         {
  66.             Remove-Item $CertificateREQ
  67.         }
  68.     certreq -new $CertificateINI $CertificateREQ
  69.  
  70.     ### Online certificate request and import
  71.     if ($OnlineCA)
  72.         {
  73.             if (test-path $CertificateCER)
  74.                 {
  75.                     Remove-Item $CertificateCER
  76.                 }
  77.             if (test-path $CertificateRSP)
  78.                 {
  79.                     Remove-Item $CertificateRSP
  80.                 }
  81.             certreq -submit -attrib "CertificateTemplate:$CATemplate" -config $OnlineCA $CertificateREQ $CertificateCER
  82.    
  83.             certreq -accept $CertificateCER
  84.         }
  85. }
  86.  
  87. function Install-Cert {
  88. Param(
  89. [Parameter(Mandatory=$true, HelpMessage = "Please enter the name of the IIS Website you wish to bind your certificate to")]
  90. [string]$iisSite = "Default Web Site"
  91. )
  92. Import-Module WebAdministration
  93.  
  94. #preparation
  95. $cert = $($(Get-ChildItem Cert:\LocalMachine\my | Where-Object {$_.FriendlyName -like "*$($(Get-Date).ToShortDateString())*"}).Thumbprint)
  96. Write-Output $cert
  97.  
  98. $bindings = Get-WebBinding
  99.  
  100. #see if any https bindings exist
  101. ForEach ($binding in $bindings)
  102.     {
  103.         If ($binding.protocol -eq 'https')
  104.             {
  105.                 $httpsBindingExist = $true
  106.                 break
  107.             }
  108.         else
  109.             {
  110.                 $httpsBindingExist = $false
  111.             }
  112.     }
  113.  
  114. #create or update bindings and apply certs
  115. If (!($httpsBindingExist))
  116.     {
  117.         New-WebBinding -Name $iisSite -Protocol https -IP '*' -Port 443
  118.     }
  119. If (Test-Path "IIS:\SslBindings\0.0.0.0!443")
  120.     {
  121.         Get-Item -path cert:\localmachine\my\$cert | Set-Item -path IIS:\Sslbindings\0.0.0.0!443
  122.     }
  123. else
  124.     {
  125.         Get-Item -path cert:\localmachine\my\$cert | New-Item -path IIS:\Sslbindings\0.0.0.0!443
  126.     }
  127. }
  128.  
  129.  
  130.  
  131. New-CertificateRequest -subject "CN=$env:computername.$env:USERDNSDOMAIN" -SANs "$env:computername.$env:USERDNSDOMAIN" -OnlineCA "{fqdn of ca\CA name}"
  132.  
  133. Install-Cert -iisSite "Default Web Site"
  134. iisreset /noforce
Advertisement
Add Comment
Please, Sign In to add comment