使用PowerShell 测试DNS

运行环境:Windows Server 2012 R2

 

获取服务器DNS命令,下面的仅获取一个dns

 (nslookup sql.ciras.com)[1].split(':')[1].trim() 

 

此脚本用来验证DNS是否正常,实际原理就是ping域名的ip

先ping域名

再解析域名的ip地址

判断域名是否解析域名是否为多个ip

若为多个ip,则再次ping文件中给的ip地址

最后输出表格

 

# 验证DNS是否能ping通
# author:lttr <www.cnblogs.com/GoCircle>
# date:2019-08-09
# eg. # C:\Users\0\Desktop\TestDNS.ps1 # 获取当前脚本绝对路径 $path = Split-Path -Parent $MyInvocation.MyCommand.Definition; $file = "\域名及ip列表.txt"; # 读取txt文件获取列表信息 [array]$dnslist = (get-content -Path ($path + $file)) $sourcecount = $dnslist.Length-1 for($i=1;$i -le $sourcecount;$i++){ Write-Progress -Activity "正在检测DNS列表" -PercentComplete ($i/$sourceCount*100) -Status "Step $i of $sourcecount" $dnsip = ($dnslist[$i] -split ",")[1] $flage = Test-NetConnection ($dnslist[$i] -split ",")[0] -Hops 1 -InformationLevel Quiet -ErrorAction SilentlyContinue -WarningAction silentlyContinue $dnsresult = Resolve-DnsName -Name ($dnslist[$i] -split ",")[0] -ErrorAction SilentlyContinue if($dnsresult.IPAddress.count -gt 1){ $flage = Test-NetConnection $dnsip -Hops 1 -InformationLevel Quiet -ErrorAction SilentlyContinue -WarningAction silentlyContinue } [PSCustomObject]@{ 域名 = ($dnslist[$i] -split ",")[0].ToUpper() + " " DNS状态 = Switch($DNSLookupObject){ {!$flage}{"Ping失败 "} {$flage -and !($dnsresult.IPAddress -Match $dnsip)}{"IP不一致 "} {$flage -and $dnsresult.IPAddress -Match $dnsip}{"成功 "} } txt中IP地址 = $dnsip + " " Connection_IP地址 = $(if($flage){$dnsresult.IPAddress}) } }

附带的txt文件格式

输入格式【域名,IP】(中间使用英文逗号分隔),请保留本行且不能包含空行。
www.baidu.com,127.0.0.1
www.baidu.com,127.0.0.2
www.baidu.com,127.0.0.3
www.baidu.com,127.0.0.4

 

 

 

 

posted @ 2019-08-18 20:26  听雨的人  阅读(1115)  评论(0编辑  收藏  举报