tIISMail.psm1

Enum Delivery
{
    SpecifiedPickupDirectory
    Network
    PickupDirectoryFromIis
}
[DscResource()]
Class tIISMail
{
    # DSC Resource Property
    [DscProperty(key)]
    [string]$SiteName
    [DscProperty(Mandatory)]
    [Delivery]$DeliveryMethod
    [DscProperty()]
    [string]$From = [string]::Empty
    [DscProperty()]
    [bool]$DefaultCredentials
    [DscProperty()]
    [string]$MailServer = [string]::Empty
    [DscProperty()]
    [string]$Password = [string]::Empty
    [DscProperty()]
    [string]$userName = [string]::Empty
    [DscProperty()]
    [string]$PickupDirectoryLocation = [string]::Empty
    [DscProperty()]
    [int]$Port
    [DscProperty()]
    [bool]$Debug = $false

    ## private

    [string]$smtp = "system.net/mailSettings/smtp"
    [string]$NetWork = "system.net/mailSettings/smtp/network"
    [string]$specifiedPickupDirectory = "system.net/mailSettings/smtp/specifiedPickupDirectory"
    [void]Set()
    {
        if($this.Debug)
        {Wait-Debugger}
        try
        {
            $name =(Get-Website -Name $this.SiteName).Name
            $pspath = "MACHINE/WEBROOT/APPHOST/" +$name
            Write-Verbose "Setting DeliveryMethod"
            Set-WebConfigurationProperty -pspath $pspath  -filter $this.smtp -name "deliveryMethod" -value $this.DeliveryMethod.ToString()
            if($this.From -ne [string]::Empty)
            {
                 Write-Verbose "Setting From"
                 Set-WebConfigurationProperty -pspath $pspath -filter $this.smtp -name "from" -value $this.From 
            }
            switch($this.DeliveryMethod)
            {
                "Network"
                {
                    if($this.MailServer -ne [string]::Empty)
                    {
                        Write-Verbose "Setting MailServer"
                        Set-WebConfigurationProperty -pspath $pspath  -filter $this.NetWork -name "host" -value $this.MailServer
                    }
                    if($this.userName -ne [string]::Empty)
                    {
                        Write-Verbose "Setting userName"
                        Set-WebConfigurationProperty -pspath $pspath  -filter $this.NetWork -name "userName" -value $this.userName
                    }
                    if($this.Port)
                    {
                        Write-Verbose "Setting Port"
                        Set-WebConfigurationProperty -pspath $pspath  -filter $this.NetWork -name "port" -value $this.Port
                    }
                    if($this.Password -ne [string]::Empty)
                    {
                        Write-Verbose "Setting Password"
                        Set-WebConfigurationProperty -pspath $pspath  -filter $this.NetWork -name "password" -value $this.Password
                    }
                    if(($this.userName -ne [string]::Empty) -and  ($this.Password -ne [string]::Empty))
                    {
                        Write-Verbose "Setting defaultCredentials"    
                        Set-WebConfigurationProperty -pspath $pspath  -filter $this.NetWork -name "defaultCredentials" -value ($this.DefaultCredentials.ToString())
                    }
                }
                "SpecifiedPickupDirectory"
                {
                    if($this.PickupDirectoryLocation)
                    {
                        Write-Verbose "Setting SpecifiedPickupDirectory"
                        Set-WebConfigurationProperty -pspath $pspath  -filter $this.specifiedPickupDirectory -name "pickupDirectoryLocation" -value    $this.PickupDirectoryLocation
                    }
                }
            }
        }
        catch [System.Exception]
        {
            Write-Verbose $_.Exception
        }
    }    
    [bool]Test()
    {    
        if($this.Debug)
        {Wait-Debugger}
        $result = $true
        $has = $this.GetProperty($this.SiteName)
        do
        {            
            if([Delivery]$has.DeliveryMethod -ne $this.DeliveryMethod)
            {
                Write-Verbose "Testing DeliveryMethod"
                $result =$false
                Write-Verbose "DeliveryMethod Not InDesiredState"
                break
            }    
            if($this.From -ne [string]::Empty)
            {
                Write-Verbose "Testing From"
                if($has.From -ne $this.From)
                {                     
                     $result =$false
                    Write-Verbose "From Not InDesiredState"
                     break
                }
            }
            switch($this.DeliveryMethod)
            {
                "Network"
                {
                    if($this.Port)
                    {
                        Write-Verbose "Testing Port"
                        if([int]($has.Port.Value) -ne $this.Port)
                        {
                            $result = $false
                            Write-Verbose "Port Not InDesiredState"
                            break
                        }
                    }
                    if($this.userName -ne [string]::Empty)
                    {
                        Write-Verbose "Testing Username"
                        if($has.userName.Value -ne $this.userName)
                        {
                            $result = $false
                            Write-Verbose "Username Not InDesiredState"
                            break
                        }
                    }
                    if($this.Password -ne [string]::Empty)
                    {
                        Write-Verbose "Testing Password"
                        if($has.Password.Value -ne $this.Password)
                        {
                            $result = $false
                            Write-Verbose "Password Not InDesiredState"
                            break
                        }
                    }
                    if(!($this.userName -eq [string]::Empty) -and ($this.Password -eq [string]::Empty))
                    {
                        Write-Verbose "Testing DefaultCredentials"
                        if(([bool]$has.DefaultCredentials) -ne $this.DefaultCredentials)
                        {
                            $result =$false
                            Write-Verbose "DefaultCredentials Not InDesiredState"
                            break
                        }
                    }
                    if($this.MailServer -ne [string]::Empty)
                    {
                        Write-Verbose "Testing MailServer"
                        if($has.MailServer.Value -ne $this.MailServer)
                        {
                            $result = $false
                            Write-Verbose "MailServer Not InDesiredState"
                            break
                        }
                    }
                }
                "SpecifiedPickupDirectory"
                {
                    Write-Verbose "Testing PickupDirectoryLocation"
                    if($this.PickupDirectoryLocation -ne [string]::Empty)
                    {
                        if($has.pickupDirectoryLocation.Value -ne $this.PickupDirectoryLocation)
                        {
                            $result =$false
                            Write-Verbose "PickupDirectoryLocation Not InDesiredState"
                            break
                        }
                    }
                }
            }
            break
        }while($result = $false)
        return $result
    }   
    [tIISMail] Get()
    {
        if($this.Debug)
        {Wait-Debugger}
        try
        {
            $has = $this.GetProperty($this.SiteName)
            $this.DefaultCredentials = $has.DefaultCredentials
            $this.DeliveryMethod = [Delivery]$has.DeliveryMethod
            $this.From = $has.From.Value
            $this.userName = $has.userName.Value
            $this.Password = $has.Password.Value
            $this.MailServer = $has.MailServer.Value
            $this.PickupDirectoryLocation = $has.pickupDirectoryLocation.Value
            $this.Port = $has.Port.Value
            return $this
        }
        catch [System.Exception]
        {
            Write-Verbose $_.Exception
            return $null
        }
    }   
    [hashtable]GetProperty([String] $SiteName)
    {    
        [hashtable]$has = @{}
        try
        {
            $name =(Get-Website -Name $SiteName).Name
            $pspath = "MACHINE/WEBROOT/APPHOST/" +$name
            $has.Add("DeliveryMethod",
                (Get-WebConfigurationProperty -PSPath $pspath -Filter $this.smtp -Name deliveryMethod ))
            $has.Add("From",
                (Get-WebConfigurationProperty -PSPath $pspath -Filter $this.smtp -Name from ))
            $has.Add("DefaultCredentials",
                ([bool](Get-WebConfigurationProperty -PSPath $pspath -Filter $this.NetWork -Name defaultCredentials)))
            $has.Add("MailServer",
                (Get-WebConfigurationProperty -PSPath $pspath -Filter $this.NetWork -Name host))
            $has.Add("Password",
                (Get-WebConfigurationProperty -PSPath $pspath -Filter $this.NetWork -Name password))
            $has.Add("userName",
                (Get-WebConfigurationProperty -PSPath $pspath -Filter $this.NetWork -Name userName))
            $has.Add("pickupDirectoryLocation",
                (Get-WebConfigurationProperty -PSPath $pspath -Filter $this.specifiedPickupDirectory -Name pickupDirectoryLocation))
            $has.Add("Port",
                (Get-WebConfigurationProperty -PSPath $pspath -Filter $this.NetWork -Name port))
        }
        catch [System.Exception]
        {
            Write-Verbose $_.Exception
        }
        return $has
    }
}


New-Item -Path C:\Module\tIISMail -ItemType Directory -Force
New-ModuleManifest -Path C:\Module\tIISMail\tIISMail.psd1 -Guid ([guid]::NewGuid()) -Author strike -RootModule tIISMail.psm1 -ModuleVersion 0.0.0.1 -DscResourcesToExport *
#Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/A' -filter "system.net/mailSettings/smtp" -name "deliveryMethod" -value "PickupDirectoryFromIis"
#Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/A' -filter "system.net/mailSettings/smtp" -name "from" -value "GGG"
#Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/A' -filter "system.net/mailSettings/smtp/network" -name "defaultCredentials" -value "False"
#Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/A' -filter "system.net/mailSettings/smtp/network" -name "host" -value "00000"
#Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/A' -filter "system.net/mailSettings/smtp/network" -name "password" -value "123."
#Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/A' -filter "system.net/mailSettings/smtp/network" -name "userName" -value "STRI"
#Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/A' -filter "system.net/mailSettings/smtp/specifiedPickupDirectory" -name "pickupDirectoryLocation" -value "FGFFGF"
#Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/MSite' -filter "system.net/mailSettings/smtp/network" -name "port" -value 22