vendredi 4 janvier 2019

Set-ItemProperty for http removes existing bindings for website in IIS

One of my application (say app1) running under website in IIS created https binding during deployment. However when another application (say app2) under same website deployed recently via power shell script, it removed previously added https binding and broke app1.

When I looked into deployment script of app2, I realized there is a function to check if binding already exist - if yes, simply call Set-ItemProperty to update that binding or else create the one. This idea looks fine to me - basically it says create binding specific to application or update if already existing. But am not sure, why Set-ItemProperty for http removed https binding (in fact all others as well like net.tcp, net.pipe etc)

Below is function from that deployment script.

Import-Module -Name WebAdministration
    function SetBindingsIIS
    {
    param
    (
       [Parameter(Mandatory)]
       [ValidateNotNullOrEmpty()]
       [string]$WebsiteName,
       [HashTable]$protocol
    )
    $Status=$null
    $GetProtocolName= $protocol["Protocol"]
    $BindingsCollection=Get-ItemProperty -Path "IIS:\Sites\$WebsiteName" -Name Bindings 
    $ProtocolExists=$BindingsCollection.Collection | Where-Object{$_.protocol -eq $GetProtocolName}
        Try
        {
            if($ProtocolExists -eq $null)
            {
                New-ItemProperty -Path IIS:\Sites\$WebsiteName -Name Bindings -Value $protocol -Force
            }
            else
            {
                Set-ItemProperty -Path "IIS:\Sites\$WebsiteName" -Name Bindings -Value $protocol -Force
            }
            $Status="Success"
        }
        Catch
        {
            $ErrorMessage=$_.Exception.Message        
            $Status="Error in Add/Update bindings : $ErrorMessage"
        }

        return $Status
    }

Running this function simply removes all existing bindings already configured for web site in IIS

SetBindingsIIS -WebsiteName "TestMiddleTierSite" -protocol @{Protocol="http";BindingInformation=":81:"}




Aucun commentaire:

Enregistrer un commentaire