SiteNameConsistencyReport.ps1


Description

@GitHub Copilot - Welcome @BanterBoy, I’m your Copilot and I’m here to help you get things done faster. I can identify issues, explain and even improve code.

You can ask generic questions, but what I’m really good at is helping you with your code. For example:

Generate unit tests for my code Explain the selected code Propose a fix for the bugs in my code If you want to learn more about my capabilities and limitations, check out the Copilot documentation.

I’m powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve.

@BanterBoy: - Explain what the script does and why it is useful.

@GitHub Copilot -


Script

<#
 This script will compare the

 Shay Levy posted the Get the AD site name of a computer function here:
 http://www.powershellmagazine.com/2013/04/23/pstip-get-the-ad-site-name-of-a-computer/

 Microsoft posted a method for retrieving the DynamicSiteName here:
 Problem retrieving Value for DynamicSiteName from Registry using PS: http://support.microsoft.com/kb/2801452

  https://github.com/joethemongoose/PowerCLI/blob/master/Export-VMs-With-SiteCode.ps1

#>

#-------------------------------------------------------------
# Get the script path
$ScriptPath = { Split-Path $MyInvocation.ScriptName }
$OnlineFile = $(&$ScriptPath) + "\SiteNameConsistency-online.csv"
$OfflineFile = $(&$ScriptPath) + "\SiteNameConsistency-offline.csv"

#-------------------------------------------------------------

Import-Module ActiveDirectory

# Get Domain Controllers
$Computers = Get-ADDomainController -Filter * | Sort-Object Name

# Get All Servers
#$Computers = Get-ADComputer -Filter * -Properties Name,Operatingsystem | Where-Object {$_.Operatingsystem -like "*server*"} | Sort-Object Name

# Get All Authorized DHCP Servers
$defaultNC = ([ADSI]"LDAP://RootDSE").defaultNamingContext.Value
$configurationNC = "cn=configuration," + $defaultNC
$AuthorizedDHCPServers = Get-ADObject -SearchBase $configurationNC -Filter "objectclass -eq 'dhcpclass' -AND Name -ne 'dhcproot'"

#-------------------------------------------------------------

function Get-ComputerSite($ComputerName) {
    $site = nltest /server:$ComputerName /dsgetsite 2>$null
    if ($LASTEXITCODE -eq 0) { $site[0] }
}

function Get-ComputerSiteValue($ComputerName, $Value) {
    $ValueData = ""
    $key = "System\CurrentControlSet\Services\NetLogon\Parameters"
    $type = [Microsoft.Win32.RegistryHive]::LocalMachine
    Try {
        $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($type, $ComputerName)
        $regKey = $regKey.OpenSubKey($key)
        $ValueData = $regKey.GetValue($($Value)).Split([char]0)[0]
    }
    Catch {
        Write-Host -ForegroundColor Red "- $($Value): Could Not Connect to Remote Registry!"
    }
    $ValueData
}

$onlinearray = @()
$offlinearray = @()

ForEach ($Computer in $Computers) {
    $ComputerError = "$false"
    $ComputerName = $Computer.Name
    if (Test-Connection -Cn $Computer.Name -BufferSize 16 -Count 1 -ea 0 -quiet) {
        Write-Host -ForegroundColor Green "Checking Site for $ComputerName"
        $ComputerSite = Get-ComputerSite $ComputerName
        $ComputerDynamicSiteName = Get-ComputerSiteValue $ComputerName DynamicSiteName
        $ComputerSiteName = Get-ComputerSiteValue $ComputerName SiteName
        $DynamicSiteName = $False
        $SiteName = $False

        If ($ComputerDynamicSiteName -ne "") { $DynamicSiteName = $True }
        If ($ComputerSiteName -ne "") { $SiteName = $True }
        If ($SiteName -eq $True) {
            If ($ComputerSite -eq $ComputerSiteName) {
                Write-Host -ForegroundColor Green "- Sites match"
            }
            Else {
                Write-Host -ForegroundColor Red "- Sites do not match"
            }
        }
        ElseIf ($DynamicSiteName -eq $True) {
            If ($ComputerSite -eq $ComputerDynamicSiteName) {
                Write-Host -ForegroundColor Green "- Sites match"
            }
            Else {
                Write-Host -ForegroundColor Red "- Sites do not match"
            }
        }
        Else {
            Write-Host -ForegroundColor Red "- Both the DynamicSiteName and SiteName registry values are missing."
        }
    }
    Else {
        $ComputerError = "$true"
        $ErrorDescription = "Unable to ping server"
        Write-Host -ForegroundColor Red "$ComputerName is offline"
    }
    if ($ComputerError -eq $true) {
        $outputbad = New-Object PSObject
        $outputbad | Add-Member NoteProperty ComputerName $ComputerName
        $outputbad | Add-Member NoteProperty ErrorDescription $ErrorDescription
        $offlinearray += $outputbad
    }
}
$onlinearray | export-csv -notype "$OnlineFile"
$offlinearray | export-CSV -notype "$OfflineFile"

# Remove the quotes
(Get-Content "$OnlineFile") | ForEach-Object { $_ -replace '"', "" } | out-file "$OnlineFile" -Force -Encoding ascii
(Get-Content "$OfflineFile") | ForEach-Object { $_ -replace '"', "" } | out-file "$OfflineFile" -Force -Encoding ascii

Back to Top


Download

Please feel free to copy parts of the script or if you would like to download the entire script, simple click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top