modules/deploy/dsc/ext/PsGallery/xDnsServer.1.5.0.0/DSCResources/MSFT_xDnsARecord/MSFT_xDnsARecord.psm1

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

        [parameter(Mandatory = $true)]
        [System.String]
        $Zone,

        [parameter(Mandatory = $true)]
        [System.String]
        $Target,

        [ValidateSet('Present','Absent')]
        [System.String]
        $Ensure = 'Present'
    )
    Write-Warning -Message "DSC Resource xDnsARecord has been replaced by xDNSRecord, and will be removed in a future version"
    Write-Verbose "Looking up DNS record for $Name in $Zone"
    $record = Get-DnsServerResourceRecord -ZoneName $Zone -Name $Name -ErrorAction SilentlyContinue
    if ($record -eq $null) {
        return @{
            Name = $Name;
            Zone = $Zone;
            Target = $Target;
            Ensure = 'Absent';
        }
    }
    else {
        return @{
            Name = $record.HostName;
            Zone = $Zone;
            Target = $record.RecordData.IPv4Address.ToString();
            Ensure = 'Present';
        }
    }
}


function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $Name,

        [parameter(Mandatory = $true)]
        [System.String]
        $Zone,

        [parameter(Mandatory = $true)]
        [System.String]
        $Target,

        [ValidateSet('Present','Absent')]
        [System.String]
        $Ensure = 'Present'
    )
    if ($Ensure -eq 'Present') {
        Write-Verbose "Creating for DNS $Target in $Zone"
        Add-DnsServerResourceRecordA -IPv4Address $Target -Name $Name -ZoneName $Zone -ComputerName "localhost" 
    }
    elseif ($Ensure -eq 'Absent') {
        Write-Verbose "Removing DNS $Target in $Zone"
        Remove-DnsServerResourceRecord -Name $Name -ZoneName $Zone -RRType A -ComputerName "localhost" -Force
    }
}


function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $Name,

        [parameter(Mandatory = $true)]
        [System.String]
        $Zone,

        [parameter(Mandatory = $true)]
        [System.String]
        $Target,

        [ValidateSet('Present','Absent')]
        [System.String]
        $Ensure = 'Present'
    )

    Write-Verbose "Testing for DNS $Name in $Zone"
    $result = @(Get-TargetResource @PSBoundParameters)
    if ($Ensure -ne $result.Ensure) { return $false }
    elseif ($Ensure -eq 'Present' -and ($result.Target -ne $Target)) { return $false }
    return $true
}


Export-ModuleMember -Function *-TargetResource