1

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"
    }
}
2
  • Oh damn! I was creating new binding and then removing all of them as IIS_SSS_HTTP_PORT was not defined. Thank you so much @CFou. This fixed my problem. Commented Nov 6, 2020 at 9:28
  • @CFou hello can you post your comment as answer Commented Nov 19, 2020 at 10:35

1 Answer 1

1

@Mehul Parmar, as you asked, I have deleted my comment and moved it into an answer:

Is the dir command lists something ? It probably should in your case. My testing have no result and works well without error. You can test if the element already exists :

if (-not (Test-Path 0.0.0.0!$($IIS_SSS_HTTPS_PORT)))

"MyServer" is a weird name for a web site, don't forget that bindings are bound to sites not to server. One more thing : Get-WebBinding -Port $IIS_SSS_HTTP_PORT -Name "MyServer" | Remove-WebBinding will remove all bindings if $IIS_SSS_HTTP_PORT is not defined. May be you should prefer this one :

Get-WebBinding -Protocol http -Name "MyServer" | Remove-WebBinding

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.