sitecore-bindings.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#
# sitecore_bindings.ps1
#



function Add-Binding($SiteName, $HostHeader )
{
    $bindings = Get-WebBinding -Name $SiteName
    
    if( $bindings.bindingInformation -match $HostHeader )
    {
        Write-Verbose "A binding '$HostHeader' already exist on site '$SiteName'"
    }
    else
    {
        try
        {
            Write-Verbose "Adds a binding '$HostHeader' to an IIS '$SiteName' site"

            New-WebBinding -Name $SiteName -IPAddress "*" -Port 80 -HostHeader $HostHeader;
        }
        catch
        {
            Write-Warning "Adds a binding '$HostHeader' failed - $($_.Exception.Message)"
        }
    }
}

function Add-SitecoreBindings
{
    [cmdletbinding(SupportsShouldProcess=$true)]
     Param(
        [ValidateNotNullOrEmpty()] 
        $Hosts, 
        [ValidateNotNullOrEmpty()] 
        [string]$WebSite,

        [switch]$www
     )

        #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 $WebSite ) ) )
        {
            Write-Warning "Site '$WebSite' does not exist."
            return
        }
        #endregion

        foreach($hostName in $Hosts)
        {
            Add-Binding -Site $WebSite -HostHeader $hostName
             
            if( ($www -eq $true) -and ($hostName.StartsWith("www") -eq $false) )
            {
                Add-Binding -Site $WebSite -HostHeader "www.$hostName"
            }
        }
}


function Remove-SitecoreBindings
{
    [cmdletbinding(SupportsShouldProcess=$true)]
     Param(
        [ValidateNotNullOrEmpty()] 
        $Hosts, 
        [ValidateNotNullOrEmpty()] 
        [string]$WebSite
     )

        #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 $WebSite ) ) )
        {
            Write-Warning "Site '$WebSite' does not exist."
            return
        }
        #endregion

        foreach($hostName in $Hosts)
        {
            $bindings = Get-WebBinding -Name $WebSite;

            if( $bindings.bindingInformation -match $hostName )
            {
                Write-Verbose "Removes a binding '$hostName' from an IIS '$webSite' site."
                Remove-WebBinding -Name $WebSite -IPAddress "*" -Port 80 -HostHeader  $hostName
            }
            
            if( $bindings.bindingInformation -match "www.$hostName" )
            {
                Write-Verbose "Removes a binding 'www.$hostName' from an IIS '$webSite' site."
                Remove-WebBinding -Name $WebSite -IPAddress "*" -Port 80 -HostHeader  "www.$hostName"
            }
        }

}

Export-ModuleMember -Function Add-SitecoreBindings
Export-ModuleMember -Function Remove-SitecoreBindings