Skip to content

Instantly share code, notes, and snippets.

@TechByTom
Last active March 14, 2024 16:59
Show Gist options
  • Save TechByTom/1f28d28e51a5fb9ad0fc14e17216e2db to your computer and use it in GitHub Desktop.
Save TechByTom/1f28d28e51a5fb9ad0fc14e17216e2db to your computer and use it in GitHub Desktop.

Revisions

  1. TechByTom renamed this gist Mar 14, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. TechByTom created this gist Mar 14, 2024.
    77 changes: 77 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    # Import the Active Directory module
    Write-Host "Importing the Active Directory module..."
    Import-Module ActiveDirectory
    Write-Host "Active Directory module imported."

    # Automatically derive the domain's distinguished name
    Write-Host "Retrieving the current user's domain context..."
    $domainDN = (Get-ADDomain).DistinguishedName
    Write-Host "The current user's domain context has been retrieved: $domainDN"

    # Construct the DN for the MicrosoftDNS zone within the DomainDnsZones partition
    Write-Host "Constructing the Distinguished Name (DN) for the MicrosoftDNS zone within the DomainDnsZones partition..."
    $dnsZoneDN = "CN=MicrosoftDNS,DC=DomainDnsZones,$domainDN"
    Write-Host "The DN for the MicrosoftDNS zone has been constructed: $dnsZoneDN"

    # Display the query that will be executed
    Write-Host "Preparing to execute the query to fetch all objects under the MicrosoftDNS zone..."
    Write-Host "The query will be executed with the following search base: $dnsZoneDN"

    # Append the current date and time to the file name
    $dateTime = Get-Date -Format "yyyyMMddHHmmss"
    $outputPath = "MicrosoftDNSZoneObjects_$dateTime.csv"

    # Attempt to retrieve and export all objects under the MicrosoftDNS zone to a CSV file
    try {
    Write-Host "Executing the query..."
    $objects = Get-ADObject -SearchBase $dnsZoneDN -Filter * -Properties *

    if ($objects) {
    Write-Host "Query executed successfully. Preparing data for export..."

    $internalHosts = New-Object System.Collections.ArrayList
    $notInternalHosts = New-Object System.Collections.ArrayList

    foreach ($object in $objects) {
    $hostname = "none"
    # Check for conditions to set the hostname or keep it as "none"
    if ($object.DistinguishedName -match '^DC=([^,]+),' -and $matches[1] -notmatch '^\@' -and $matches[1] -notmatch '\.') {
    $hostname = $matches[1]
    }

    # Create a custom object to maintain the order and include the new hostname column
    $customObject = New-Object PSObject -Property @{
    Hostname = $hostname
    DistinguishedName = $object.DistinguishedName
    }

    # Add the remaining properties from the original object
    $object.psobject.properties | Where-Object { $_.Name -notin 'DistinguishedName', 'Hostname' } | ForEach-Object {
    Add-Member -InputObject $customObject -MemberType NoteProperty -Name $_.Name -Value $_.Value -Force
    }

    # Distribute objects into two arrays based on the Hostname value
    if ($hostname -eq "none") {
    [void]$notInternalHosts.Add($customObject)
    } else {
    [void]$internalHosts.Add($customObject)
    }
    }

    # Define file paths for internal and non-internal hosts
    $internalHostsPath = "MicrosoftDNSZoneObjects_$dateTime.csv"
    $notInternalHostsPath = "MicrosoftDNSZoneObjects_NotInternalHosts_$dateTime.csv"

    Write-Host "Exporting internal hosts to $internalHostsPath..."
    $internalHosts | Export-Csv -Path $internalHostsPath -NoTypeInformation

    Write-Host "Exporting non-internal hosts to $notInternalHostsPath..."
    $notInternalHosts | Export-Csv -Path $notInternalHostsPath -NoTypeInformation

    Write-Host "Results exported successfully."
    } else {
    Write-Host "No objects found in the MicrosoftDNS zone."
    }
    } catch {
    Write-Error "An error occurred while executing the query or exporting the results: $_"
    }