DSCResources/myAdSite/myAdSite.psm1

$SitesDN = "CN=Sites,$($(Get-ADRootDSE).configurationNamingContext)"
$SubnetsDN = "CN=Subnets,$SitesDN"

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

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

    #Write-Debug "Use this cmdlet to write debug information while troubleshooting."
    
    $Site = Get-ADObject -Filter "objectClass -eq 'site' -and Name -eq '$Name'" -SearchBase $SitesDN -Properties 'location', 'description' -Credential $DomainAdminCreds
    if ($Site -eq $null)
    {
        $returnValue = @{
            Name = ''
            Location = ''
            Description = ''
        }
    }
    else
    {
        $returnValue = @{
            Name = $Site.Name
            Location = $Site.location
            Description = $Site.Description
        }
    }

    $returnValue
}


function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]$Name,
        [System.String]$Location,
        [System.String]$Description,
        [System.Boolean]$Protected,
        [System.Management.Automation.PSCredential]$DomainAdminCreds
    )
    
    $Site = Get-ADObject -Filter "objectClass -eq 'site' -and name -eq '$Name'" -SearchBase $SitesDN -Credential $DomainAdminCreds
    if ($Site -eq $null)
    {
        New-ADObject -Path $SitesDN -Description $Description -Name $Name -Type 'site' -protectedFromAccidentalDeletion $Protected -OtherAttributes @{ location = "$Location" } -Credential $DomainAdminCreds
    }
    else
    {
        Set-ADObject -Identity $($Site.DistinguishedName) -Replace @{ location = "$Location" } -Credential $DomainAdminCreds -Description $Description
    }
    
    #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]$Name,
        [System.String]$Location,
        [System.String]$Description,
        [System.Boolean]$Protected,
        [System.Management.Automation.PSCredential]$DomainAdminCreds
    )

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

    #Write-Debug "Use this cmdlet to write debug information while troubleshooting."
    
    $Site = Get-ADObject -Filter "objectClass -eq 'site' -and name -eq '$Name'" -SearchBase $SitesDN -Credential $DomainAdminCreds
    if ($Site -eq $null)
    {
        $result = $false
    }
    else
    {
        $result = $true
    }    
    $result
}


Export-ModuleMember -Function *-TargetResource