We are creating MSAs for a given computer that is used as SQL Server server, and we want to absolutely sure that the computer exists by a given name in Active Directory (AD)and DNS. That also includes that the Fully Qualified Domain Name (FQDN) is correct.
To do this check I have created a function that checks both AD and DNS. The function is constructed to a specific script, and you should probably alter something to make it suit your needs.
function Test-Computer {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, HelpMessage='Enter name of server. Use Fully Qualified Name (FQN), e.g. "SANDBOX.sqladmin.lan"')]
[String]$ServerName
)
[String]$ComputerName = $ServerName.Split('.')[0]
"{0:s}Z Testing if the computer '$ComputerName' exists in DNS..." -f $([System.DateTime]::UtcNow) | Write-Verbose
try {
[System.Net.IPHostEntry]$IpHost = [System.Net.Dns]::GetHostByName($ComputerName)
}
catch [System.Management.Automation.MethodInvocationException] {
"'$Computername' does not exist in DNS as FQDN!"
return $false
}
"{0:s}Z Testing if the FQDN of '$ServerName'..." -f $([System.DateTime]::UtcNow) | Write-Verbose
if ($IpHost.HostName -ieq $ServerName) {
"{0:s}Z FQDN '$ServerName' is OK." -f $([System.DateTime]::UtcNow) | Write-Verbose
}
else {
"The computer name '$ServerName' does not match the FQDN '$($IpHost.HostName)'." | Write-Error
return $false
}
"{0:s}Z Testing if the computer '$ComputerName' exists in Active Directory..." -f $([System.DateTime]::UtcNow) | Write-Verbose
try {
[Microsoft.ActiveDirectory.Management.ADComputer]$Computer = $null
$Computer = Get-ADComputer -Identity $ComputerName
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
$ComputerError = $Error[0]
}
if ($Computer) {
"{0:s}Z The computer '$ServerName' exists in Active Directory." -f $([System.DateTime]::UtcNow) | Write-Verbose
return $true
}
else {
"The computer '$ServerName' does not exist in Active Directory." | Write-Error
return $false
}
} # Test-Computer()
No comments:
Post a Comment