DSCResources/myAdExchPrepareAD/myAdExchPrepareAD.psm1

Import-Module $PSScriptRoot\..\myAdHelper.psm1 -Verbose:$false

function GetOrgNameFromArguments
{
    [CmdletBinding()]
    [OutputType([System.String])]
    param
    (
        [System.String]$Arguments
    )
    $returnValue = ''
    $aArgs = $Arguments -split ' '
    foreach ($aArg in $aArgs)
    {
        if ($aArg.contains('/OrganizationName:'))
        {
            $returnValue = [string]$aArg.Substring(18)
        }
    }
    $returnValue
}

function Get-TargetResource
{
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]$Path,
        [parameter(Mandatory = $true)]
        [System.String]$Arguments,
        [System.Management.Automation.PSCredential]$Credential
    )
    
    $Searchbase = ReplacePartitionTokens -Identity "CN=Services,%%configuration%%" -Credential $Credential
    myGetAdObject -Filter "objectClass -eq 'msExchOrganizationContainer'" -SearchBase $Searchbase -Properties @('name', 'msExchProductID') -Credential $Credential
    
}

function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)][System.String]$Path,
        [parameter(Mandatory = $true)][System.String]$Arguments,
        [System.Management.Automation.PSCredential]$Credential
    )
    
    StartAndWaitWaitForProcessEnd -Path $Path -Arguments $Arguments -Credential $Credential -Delay 7200 -Verbose -TaskName 'EXCHANGE - PrepareAD' -ProcessToWaitFor 'ExSetup*'

}

function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]$Path,
        [parameter(Mandatory = $true)]
        [System.String]$Arguments,
        [System.Management.Automation.PSCredential]$Credential
    )
    
    $myExchOrganization = Get-TargetResource @PSBoundParameters
    $result = $true
    if ($myExchOrganization)
    {
        $OrgInPlace = $myExchOrganization.Name
        $OrgDN = $myExchOrganization.DistinguishedName
        $OrgInArguments = GetOrgNameFromArguments -Arguments $Arguments
        if ($OrgInArguments -eq '' -or $OrgInPlace -eq '')
        {
            $Result = $false
        }
        elseif ($OrgInArguments -ne $OrgInPlace)
        {
            $Result = $false
        }
        else
        {
            if ((myGetAdObject -Identity "CN=ServiceEndpoints,$OrgDN" -Credential $Credential) -eq $null) { $result = $false }
            if ((myGetAdObject -Identity "CN=All Global Address Lists,CN=Address Lists Container,$OrgDN" -Credential $Credential) -eq $null) { $result = $false }
            if ((myGetAdObject -Identity "CN=Exchange Administrative Group (FYDIBOHF23SPDLT),CN=Administrative Groups,$OrgDN" -Credential $Credential) -eq $null) { $result = $false }

            $Domain = ReplacePartitionTokens -Identity "%%domain%%" -Credential $Credential
            if ((myGetAdObject -Filter "objectClass -eq 'group'" -SearchBase "OU=Microsoft Exchange Security Groups,$Domain" -Credential $Credential).Count -lt 17) { $result = $false }
            if ((myGetAdObject -Filter "objectClass -eq 'msExchSystemObjectsContainer'" -SearchBase "CN=Microsoft Exchange System Objects,$Domain" -Credential $Credential).Count -eq $null) { $result = $false }
            
        }
    }
    else
    {
        $Result = $false
    }
    
    $result
}


Export-ModuleMember -Function *-TargetResource