DSCResources/xIISWebBinding/xIISWebBinding.psm1

function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $SiteName,

        [parameter(Mandatory = $true)]
        [ValidateSet("Absent","Present")]
        [System.String]
        $Ensure,

        [parameter(Mandatory = $true)]
        [System.String]
        $Protocol,

        [parameter(Mandatory = $true)]
        [System.String[]]
        $Information
    )

    #Write-Verbose "Use this cmdlet to deliver information about command processing."

    #Write-Debug "Use this cmdlet to write debug information while troubleshooting."


    
    $returnValue = @{
    SiteName = $SiteName
    Ensure = $Ensure
    Information = $Information
    }

    $returnValue
    
}


function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $SiteName,

        [parameter(Mandatory = $true)]
        [ValidateSet("Absent","Present")]
        [System.String]
        $Ensure,

        [parameter(Mandatory = $true)]
        [System.String]
        $Protocol,

        [parameter(Mandatory = $true)]
        [System.String[]]
        $Information
    )
    foreach($item in $Information)
    {
        $x = GetElement -InfoString $item
        $x.Add("Name",$SiteName)
        $x.Add("protocol",$Protocol)
        Remove-WebBinding @x -ErrorAction SilentlyContinue
        if($Ensure -eq "present")
        {
            New-WebBinding @x
        }
    }

    #Write-Verbose "Use this cmdlet to deliver information about command processing."

    #Write-Debug "Use this cmdlet to write debug information while troubleshooting."

    #Include this line if the resource requires a system reboot.
    #$global:DSCMachineStatus = 1


}


function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $SiteName,

        [parameter(Mandatory = $true)]
        [ValidateSet("Absent","Present")]
        [System.String]
        $Ensure,

        [parameter(Mandatory = $true)]
        [System.String]
        $Protocol,

        [parameter(Mandatory = $true)]
        [System.String[]]
        $Information
    )

    $AllBinding=Get-WebBinding -Name $SiteName  -Protocol $Protocol
    $re = $false
    $x = 0
    foreach($item in $AllBinding)
    {
        foreach ($in in $Information)
        {
            if($item.bindingInformation -eq $in)
            {
                $x++
            }
        }
    }
    if(($x -eq $Information.Length) -and ($Ensure -eq "present"))
    {
         $re = $true
    }
    if(($x -eq 0) -and ($Ensure -eq "absent"))
    {
         $re = $true
    }   
    $re
}

Function TestIPAddress
{
    [outputtype([bool])]
    param
    (
        [string]$IPAddress
    )
    try
    {
        $x= [ipaddress]::Parse("$IPAddress")
        $true
    }
    catch [System.Exception]
    {
        $false
    }

}
Function GetElement
{
    [outputtype([hashtable])]
    param
    (
        [string]$InfoString
    )
    $has = @{}
    $has.Add("IPAddress",$InfoString.Split(":")[0])
    $has.Add("Port",$InfoString.Split(":")[1])
    if($InfoString.Split(":")[2])
    {
        $has.Add("HostHeader",$InfoString.Split(":")[2])
    }
    
    return $has
}


Export-ModuleMember -Function *-TargetResource