PowerShell-Scripts/general/Accept-CSR.ps1

46 lines
2.1 KiB
PowerShell
Raw Normal View History

2024-12-06 16:27:18 +10:00
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$DomainName
)
Import-Module WebAdministration
2024-12-05 21:38:06 +10:00
# Get Certificate
2024-12-06 16:27:18 +10:00
Write-Host -ForegroundColor Yellow "Importing $DomainName Certificate...."
2024-12-05 21:38:06 +10:00
$Path = ($pwd).Path
2024-12-06 16:27:18 +10:00
$CertificateName = (Get-ChildItem $Path | Where { $_.Name -like "*$DomainName*.crt" }).Name
#certreq -accept "$Path\$CertificateName"
2024-12-05 21:38:06 +10:00
# Get Certificate details
2024-12-06 16:27:18 +10:00
$OldCert = Get-ChildItem Cert:\LocalMachine\My | Where-Object { $_.DnsNameList -like "*$DomainName*" } | Select-Object -Property Thumbprint, Subject, @{n = 'ExpireInDays'; e = { ($_.notafter - (Get-Date)).Days } } | Where-Object { $_.ExpireInDays -lt 300 }
$NewCert = Get-ChildItem Cert:\LocalMachine\My | Where-Object { $_.DnsNameList -like "*$DomainName*" } | Select-Object -Property Thumbprint, Subject, @{n = 'ExpireInDays'; e = { ($_.notafter - (Get-Date)).Days } } | Where-Object { $_.ExpireInDays -gt 300 }
$SelectedSite = Get-WebBinding
# Remove the existing binding
Remove-WebBinding -Name "Default Web Site" -Protocol "https"
# Create the binding for the selected site without the certificate
New-WebBinding -Name "Default Web Site" -Protocol "https" -Port 443 -SslFlags 0
2024-12-05 21:38:06 +10:00
2024-12-06 16:27:18 +10:00
# Get the binding for the selected site
$binding = Get-WebBinding -Name "Default Web Site" -Protocol "https"
2024-12-05 21:38:06 +10:00
2024-12-06 16:27:18 +10:00
# Add the new SSL certificate to the binding using Thumbprint
$binding.AddSslCertificate($NewCert.Thumbprint, "My")
2024-12-05 21:38:06 +10:00
#Remove Old Certificate
$Thumb = $OldCert.Thumbprint
Get-ChildItem Cert:\LocalMachine\My\$Thumb | Remove-Item
2024-12-06 16:27:18 +10:00
#$Cert_PWD = ConvertTo-SecureString -String "Passw0rd!" -Force -AsPlainText
#$exportPath = 'exported.pfx'
#$iiscert = Get-ChildItem Cert:\LocalMachine\My\$Thumb
#Export-PfxCertificate -Cert $iiscert -FilePath $exportPath -Password $Cert_PWD -Force
#Set-RDCertificate -Role RDGateway -ImportPath "$exportPath" -Password $Cert_PWD -Force
#Set-RDCertificate -Role RDWebAccess -ImportPath "$exportPath" -Password $Cert_PWD -Force
#Set-RDCertificate -Role RDRedirector -ImportPath "$exportPath" -Password $Cert_PWD -Force
#Set-RDCertificate -Role RDPublishing -ImportPath "$exportPath" -Password $Cert_PWD -Force
2024-12-05 21:38:06 +10:00