I have written this method to use the installed SSL certificate and enable https binding in IIS. But when I am calling this method then I am error this error from Powershell.
IIS: Cannot create a file when that file already exists.Exception.Message
This is the function to enable binding. All the required variables I am reading from .json file
function IIS-SSL-SETUP {
$Global:iisStatus = $started
try {
$pwd = ConvertTo-SecureString -String $PFX_PASSWORD -Force -AsPlainText
Import-PfxCertificate -FilePath $PFX_FILE_LOCATION Cert:\LocalMachine\My -Password $pwd
$pfx.import($PFX_FILE_LOCATION, $PFX_PASSWORD, "Exportable,PersistKeySet")
$store = new-object System.Security.Cryptography.X509Certificates.X509Store([System.Security.Cryptography.X509Certificates.StoreName]::Root, "localmachine")
$store.open("MaxAllowed")
$store.add($pfx)
$store.close()
Import-Module WebAdministration
Set-Location IIS:\
if ($null -eq (Get-WebBinding "MyServer" | Where-Object { $_.bindingInformation -eq "*:$($IIS_SSS_HTTPS_PORT):" })) {
New-WebBinding -Name "MyServer" -IP "*" -Port $IIS_SSS_HTTPS_PORT -Protocol https
Get-WebBinding -Port $IIS_SSS_HTTP_PORT -Name "MyServer" | Remove-WebBinding
cd SslBindings
dir
$pfx.Import($PFX_FILE_LOCATION, $PFX_PASSWORD, 'DefaultKeySet')
$certThumbprint = "\LocalMachine\My\$($pfx.Thumbprint)"
get-item Cert:$certThumbprint | new-item 0.0.0.0!$($IIS_SSS_HTTPS_PORT) //I am getting this error at this line
$Global:iisStatus = "Passed"
}
else {
$Global:iisStatus = "Failed"
$Global:iisMsg = "Port $($IIS_SSS_HTTPS_PORT) is already in use, please mention some different port number in sslConfig.json."
$Global:iisMsgColor = "Yellow"
}
}
catch {
$Global:iisStatus = "Failed"
$Global:iisMsgColor = "Red"
$Global:iisMsg = "IIS: $_.Exception.Message"
}
}
IIS_SSS_HTTP_PORTwas not defined. Thank you so much @CFou. This fixed my problem.