cEPRSWebDirProperties.psm1

[DscResource()]
Class cEPRSWebDirProperties
{
    [DscProperty(Key)]
    [String]$WebsiteName
    [DscProperty()]
    [String]$AnonymousAuthentication
    [DscProperty()]
    [String]$WindowsAuthentication
    [DscProperty()]
    [String]$HandlerMappingPermission
    [DscProperty()]
    [String]$SSLSetting

    [cEPRSWebDirProperties]Get()
    {
        [xml]$WindowsAuthenticationcurrentXML = C:\Windows\System32\inetsrv\appcmd.exe list config "$($this.WebsiteName)" -section:system.webServer/security/authentication/windowsAuthentication

        [xml]$AnonymousAuthenticationcurrentXML = C:\Windows\System32\inetsrv\appcmd.exe list config "$($this.WebsiteName)" -section:system.webServer/security/authentication/anonymousAuthentication

        [xml]$HandlerMappingPermissioncurrentXML = C:\Windows\System32\inetsrv\appcmd.exe list config "$($this.WebsiteName)" -section:handlers

        [xml]$SSLSettingcurrentXML = C:\Windows\System32\inetsrv\appcmd.exe list config "$($this.WebsiteName)" -section:access            

        $HandlerMappingPermissionCurrentStatus = $HandlerMappingPermissioncurrentXML.GetElementsByTagName("handlers").accessPolicy

        $WindowsAuthenticationCurrentStatus = $WindowsAuthenticationcurrentXML.GetElementsByTagName("windowsAuthentication").enabled

        $AnonymousAuthenticationCurrentStatus = $AnonymousAuthenticationcurrentXML.GetElementsByTagName("anonymousAuthentication").enabled

        $SSLSettingCurrentStatus = $SSLSettingcurrentXML.GetElementsByTagName("access").sslFlags
        
            $this.WindowsAuthentication = $WindowsAuthenticationCurrentStatus
            $this.AnonymousAuthentication = $AnonymousAuthenticationCurrentStatus
            $this.HandlerMappingPermission = $HandlerMappingPermissionCurrentStatus
            $this.AnonymousAuthenticationSSLSetting = $SSLSettingCurrentStatus

        return $this
    }
    [bool]Test()
    {
        [xml]$WindowsAuthenticationcurrentXML = C:\Windows\System32\inetsrv\appcmd.exe list config "$($this.WebsiteName)" -section:system.webServer/security/authentication/windowsAuthentication

    [xml]$AnonymousAuthenticationcurrentXML = C:\Windows\System32\inetsrv\appcmd.exe list config "$($this.WebsiteName)" -section:system.webServer/security/authentication/anonymousAuthentication

    [xml]$HandlerMappingPermissioncurrentXML = C:\Windows\System32\inetsrv\appcmd.exe list config "$($this.WebsiteName)" -section:handlers

    [xml]$SSLSettingcurrentXML = C:\Windows\System32\inetsrv\appcmd.exe list config "$($this.WebsiteName)" -section:access

    $HandlerMappingPermissionCurrentStatus = $HandlerMappingPermissioncurrentXML.GetElementsByTagName("handlers").accessPolicy


    $WindowsAuthenticationCurrentStatus = $WindowsAuthenticationcurrentXML.GetElementsByTagName("windowsAuthentication").enabled

    $AnonymousAuthenticationCurrentStatus = $AnonymousAuthenticationcurrentXML.GetElementsByTagName("anonymousAuthentication").enabled 

    $SSLSettingCurrentStatus = $SSLSettingcurrentXML.GetElementsByTagName("access").sslFlags  
    
        $bool = $false

            if ($WindowsAuthenticationCurrentStatus -eq $this.WindowsAuthentication)
            {
                Write-Verbose "Windows Authentication are matching the desired state"

                $bool = $true
            }
            else
            {
                Write-Verbose "Windows Authentication not matching"
                Write-Verbose "$WindowsAuthenticationCurrentStatus, $($this.WindowsAuthentication)"
            }
            if ($AnonymousAuthenticationCurrentStatus -eq $this.AnonymousAuthentication)
            {
                Write-Verbose "AnonymousAuthentication are matching the desired state"

                $bool = $true
            }
            else
            {
                $bool = $false
                Write-Verbose "AnonymousAuthentication not matching"
                Write-Verbose "$AnonymousAuthenticationCurrentStatus, $($this.AnonymousAuthentication)"
            }
            if ($HandlerMappingPermissionCurrentStatus -eq $this.HandlerMappingPermission)
            {
                Write-Verbose "HandlermappingPermission are matching the desired state"
                $bool = $true
            }
            else
            {
                $bool = $false
                Write-Verbose "HandlermappingPermission not matching"
                Write-Verbose "$HandlerMappingPermissionCurrentStatus,$($this.HandlerMappingPermission)"
            }
            if ($SSLSettingCurrentStatus -eq $this.SSLSetting)
            {
                Write-Verbose "SSLSetting are matching the desired state"
                $bool = $true
            }
            else
            {
                $bool = $false
                Write-Verbose "SSLSetting not matching"
                Write-Verbose "$SSLSettingCurrentStatus,$($this.SSLSetting)"
            }
        Write-Verbose $bool
        $bool
        return $false
    }
    Set()
    {
        [xml]$WindowsAuthenticationcurrentXML = C:\Windows\System32\inetsrv\appcmd.exe list config "$($this.WebsiteName)" -section:system.webServer/security/authentication/windowsAuthentication

        [xml]$AnonymousAuthenticationcurrentXML = C:\Windows\System32\inetsrv\appcmd.exe list config "$($this.WebsiteName)" -section:system.webServer/security/authentication/anonymousAuthentication

        [xml]$HandlerMappingPermissioncurrentXML = C:\Windows\System32\inetsrv\appcmd.exe list config "$($this.WebsiteName)" -section:handlers

        [xml]$SSLSettingcurrentXML = C:\Windows\System32\inetsrv\appcmd.exe list config "$($this.WebsiteName)" -section:access

        $HandlerMappingPermissionCurrentStatus = $HandlerMappingPermissioncurrentXML.GetElementsByTagName("handlers").accessPolicy

        $WindowsAuthenticationCurrentStatus = $WindowsAuthenticationcurrentXML.GetElementsByTagName("windowsAuthentication").enabled

        $AnonymousAuthenticationCurrentStatus = $AnonymousAuthenticationcurrentXML.GetElementsByTagName("anonymousAuthentication").enabled

        $SSLSettingCurrentStatus = $SSLSettingcurrentXML.GetElementsByTagName("access").sslFlags
 
        if($this.WindowsAuthentication.ToString() -ne $WindowsAuthenticationCurrentStatus.ToString())
        {
            Set-WebConfigurationProperty -filter "/system.webServer/security/authentication/windowsAuthentication" -name enabled -value "$($this.WindowsAuthentication)" -PSPath "IIS:\" -location "$($this.WebsiteName)"
            Write-Verbose("Windows authentication set as $($this.WindowsAuthentication)"); 
        }
        if($this.AnonymousAuthentication.ToString() -ne $AnonymousAuthenticationCurrentStatus.ToString())
        {

            Set-WebConfigurationProperty -filter "/system.webServer/security/authentication/anonymousAuthentication" -name enabled -value "$($this.AnonymousAuthentication)" -PSPath "IIS:\" -location "$($this.WebsiteName)"
            Write-Verbose("Anonymous authentication set as $($this.AnonymousAuthentication)"); 
        }
        if($this.HandlerMappingPermission.ToString() -ne $HandlerMappingPermissionCurrentStatus.ToString())
        {
            C:\Windows\System32\inetsrv\appcmd.exe set config "$($this.WebsiteName)" -section:handlers -accessPolicy:$this.HandlerMappingPermission
            Write-Verbose("HandlerMappingPermission set as $($this.HandlerMappingPermission)"); 
        }

        if($this.SSLSetting.ToString() -ne $SSLSettingCurrentStatus.ToString())
        {
            C:\Windows\System32\inetsrv\appcmd.exe set config "$($this.WebsiteName)"/section:access  -sslFlags:$this.SSLSetting /commit:APPHOST 
            Write-Verbose("SSLSetting set as $($this.SSLSetting)"); 
        }
    }
}