在DC的正向和反向上的Powershell NSlookup

时间:2015-05-21 14:07:50

标签: powershell dns nslookup

$topDC1="10.254.90.17"
$topDC2="10.225.224.17"
$topDC3="10.110.33.32"
$topDC4="10.88.100.10"
$DomainName="office.adroot.company.net"
TRY{    
$hostname = [System.Net.DNS]::GetHostByName($topDC1).HostName.toupper()
$ipaddress = [System.Net.Dns]::GetHostAddresses($DomainName) | select IPAddressToString -ExpandProperty IPAddressToString
# I want the below to loop foreach ip in the object, ns it against all 4 topDC's, then output each result :( 
$NS1 = nslookup $ipaddress[0] $topDC1
Write-host $NS1
}
Catch{
write-host "error"
}
Here is my dirty code so far (just to keep it simple)

我正在尝试自动执行此操作: NSLOOKUP office.adroot.company.net 将结果放入对象中 对于结果中的每个ip,对我们的顶级DC执行NSLOOKUP。 找到退役后哪个DC的天堂已经清理过(仍在dns中)

2 个答案:

答案 0 :(得分:0)

试试这个:

$topDomainControllers = @("10.254.90.17", "10.225.224.17", "10.110.33.32", "10.88.100.10")

$DomainName="office.adroot.company.net"

try {
  $hostname = [System.Net.Dns]::GetHostByName($topDC1).HostName.ToUpper()
  $ipAddresses = [System.Net.Dns]::GetHostAddresses($DomainName) |
                 select -ExpandProperty IPAddressToString

  foreach ($ipAddress in $ipAddresses) {
    $nslookupResult = nslookup $ipAddress
    $foundIp = $nslookupResult[1] -match "^\D*(\d+\.\d+\.\d+\.\d+)$"

    if ($foundIp -eq $false) {
      continue
    }

    $domainController = $Matches[1]
    if ($topDomainControllers.Contains($domainController)) {
      Write-Output -Verbose "Found domain controller match for $domainController"
      break
    } else {
      Write-Output -Verbose "No match found for domain controller $domainController"
    }
  }
} catch {
  Write-Output "An error has occured: $_"
}

答案 1 :(得分:0)

</head>

知道了!这将节省我确定是否需要进行DNS清理的大量工作。谢谢大家,我正在学习Powershell会有多棒:)