DSCResources/myAdSubnet/myAdSubnet.psm1
Import-Module $PSScriptRoot\..\myAdHelper.psm1 -Verbose:$false function Get-TargetResource { param ( [parameter(Mandatory = $true)][System.String]$Name, [System.String]$Site, [System.String]$Location, [System.String]$Description, [System.Boolean]$Protected, [System.Management.Automation.PSCredential]$Credential ) $SitesDN = ReplacePartitionTokens -Identity "CN=Sites,%%configuration%%" -Credential $Credential Write-Verbose "SitesDN: $SitesDN" $SubnetsDN = "CN=Subnets,$SitesDN" myGetAdObject -Filter "objectClass -eq 'subnet' -and Name -eq '$Name'" -SearchBase $SubnetsDN -Properties @('siteObject', 'description', 'location') -Credential $Credential } 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]$Credential ) $SitesDN = ReplacePartitionTokens -Identity "CN=Sites,%%configuration%%" -Credential $Credential $SubnetsDN = "CN=Subnets,$SitesDN" $SiteDN = "CN=$Site,$SitesDN" $Subnet = Get-TargetResource @PSBoundParameters if ($Subnet) { Set-ADObject -Identity $($Subnet.DistinguishedName) -Replace @{ siteObject = "$SiteDN"; location = "$Location" } -Credential $Credential -Description $Description } else { New-ADObject -Path $SubnetsDN -Description $Description -Name $Name -protectedFromAccidentalDeletion $Protected -Type 'subnet' -OtherAttributes @{ siteObject = "$SiteDN"; location = "$Location" } -Credential $Credential } } 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]$Credential ) $SiteDN = ReplacePartitionTokens -Identity "CN=$Site,CN=Sites,%%configuration%%" -Credential $Credential $result = $true $Subnet = Get-TargetResource @PSBoundParameters if ($Subnet) { if ($SiteDN -ne $($Subnet.siteObject)) { $result = $false } } else { $result = $false } $result } Export-ModuleMember -Function *-TargetResource |