DSCResources/myAdSubnet/myAdSubnet.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."
    
    $Subnet = Get-ADObject -Filter "objectClass -eq 'subnet' -and Name -eq '$Name'" -SearchBase $SubnetsDN -Properties 'siteObject', 'description', 'location' -Credential $DomainAdminCreds
    
    if ($Subnet -eq $null)
    {
        $returnValue = @{
            Name = ''
            Site = ''
            Location = ''
            Description = ''
        }
    }
    else
    {
        $returnValue = @{
            Name = $Subnet.Name
            Site = $Subnet.siteObject
            Location = $Subnet.location
            Description = $Subnet.Description
        }
    }
    $returnValue
}


function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]$Name,
        [System.String]$Site,
        [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."
    
    $SiteDN = "CN=$Site,$SitesDN"
    
    $Subnet = Get-ADObject -Filter "objectClass -eq 'subnet' -and Name -eq '$Name'" -SearchBase $SubnetsDN -Credential $DomainAdminCreds
    if ($Subnet -eq $null)
    {
        New-ADObject -Path $SubnetsDN -Description $Description -Name $Name -protectedFromAccidentalDeletion $Protected -Type 'subnet' -OtherAttributes @{ siteObject = "$SiteDN"; location = "$Location" } -Credential $DomainAdminCreds
    }
    else
    {
        Set-ADObject -Identity $($Subnet.DistinguishedName) -Replace @{ siteObject = "$SiteDN"; location = "$Location" } -Credential $DomainAdminCreds -Description $Description
    }
    
    
    #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]$Site,
        [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."
    $result = $true
    $SiteDN = "CN=$Site,$SitesDN"
    
    $Subnet = Get-ADObject -Filter "objectClass -eq 'subnet' -and Name -eq '$Name'" -SearchBase $SubnetsDN -Properties 'siteObject', 'description', 'location' -Credential $DomainAdminCreds
    if ($Subnet -eq $null)
    {
        $result = $false
    }
    else
    {
        if ($SiteDN -ne $($Subnet.siteObject))
        {
            $result = $false
        }
    }
    
    $result

}


Export-ModuleMember -Function *-TargetResource