Last active
November 10, 2018 02:20
-
-
Save kimcuhoang/c806dd1855d9cb81a48d75321ecd7b5a to your computer and use it in GitHub Desktop.
This script will remove the given hostname and also its SSL Certificate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
param( | |
[string] $SitePrefix = "habitathome", | |
[string] $hostName = "localhost", | |
[string] $CertificateName = "localhost", | |
[string[]]$sites = @("CommerceAuthoring:5000", "CommerceMinions:5010", "CommerceOps:5015", "CommerceShops:5005", "SitecoreBizFx:4200", "SitecoreIdentityServer:5050") | |
) | |
$ErrorActionPreference = 'Stop' | |
Import-Module WebAdministration | |
Function Remove-Binding { | |
foreach ($site in $sites) { | |
$siteinfo = $site.split(':') | |
$sitename = "$($siteinfo[0])_$($SitePrefix)" | |
$siteport = $siteinfo[1] | |
#region ValidatePreconditions | |
if( -not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) | |
{ | |
Write-Warning "Add bindings requires Administrator rights" | |
return | |
} | |
if( -not (Test-Path -Path (Join-Path "IIS:\Sites" -ChildPath $sitename ) ) ) | |
{ | |
Write-Warning "Site '$sitename' does not exist." | |
return | |
} | |
#endregion | |
$bindings = Get-WebBinding -Name $sitename; | |
if( $bindings.bindingInformation -match $hostName ) | |
{ | |
Write-Verbose "Removes a binding '$hostName' from an IIS '$webSite' site." | |
Remove-WebBinding -Name $sitename -IPAddress "*" -Port $siteport -HostHeader $hostName | |
} | |
} | |
} | |
Function Remove-Certificate { | |
$cert = Get-ChildItem -Path "cert:\LocalMachine\My" | Where-Object { $_.subject -like "CN=$hostName" -and $_.FriendlyName -match "Sitecore Commerce Services SSL Certificate" } | |
if ($cert -and $cert.Thumbprint) { | |
$certPath = "cert:\LocalMachine\My\" + $($cert.Thumbprint) | |
Write-Host "Removing certificate $certificateName ($certPath)" -ForegroundColor Green | |
Remove-Item $certPath | |
} | |
else { | |
Write-Host "Could not find certificate $certificateName under cert:\LocalMachine\My" -ForegroundColor Yellow | |
} | |
} | |
Remove-Binding | |
Remove-Certificate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment