PowerShell 技能连载 - DNS 记录管理

适用于 PowerShell 5.1(Windows),需要 DnsServer 模块及管理员权限

在企业网络管理中,DNS 记录的维护是一项高频且容易出错的工作。当服务器迁移、应用上线或域名变更时,运维人员需要快速准确地添加、修改或删除 DNS 记录。手动操作 DNS 管理控制台不仅效率低下,还容易因为误操作导致服务中断。

Windows Server 自带的 DnsServer 模块提供了一整套 PowerShell cmdlet,能够以脚本化的方式管理 DNS 区域中的各类记录。结合 PowerShell 的管道和循环能力,我们可以实现批量操作、配置审计和自动化巡检,大幅提升运维效率和准确性。

本文将介绍如何使用 DnsServer 模块完成 DNS 记录的查询、创建、修改和删除操作,并提供批量管理的实用脚本模板。

查询 DNS 记录

查询是最基础的操作。使用 Get-DnsServerResourceRecord 可以列出指定区域中的所有记录,也可以按名称和类型精确筛选。下面演示了几种常见的查询方式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 导入 DnsServer 模块
Import-Module DnsServer

# 列出指定区域的所有 A 记录
Get-DnsServerResourceRecord -ZoneName "contoso.com" -RRType A |
Select-Object -First 10

# 按主机名精确查询
Get-DnsServerResourceRecord -ZoneName "contoso.com" -Name "webapp01"

# 按名称模糊查询——找出所有包含 "test" 的记录
Get-DnsServerResourceRecord -ZoneName "contoso.com" |
Where-Object { $_.HostName -like "*test*" } |
Format-Table HostName, RecordType, RecordData -AutoSize

执行结果示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
HostName  RecordType Timestamp            RecordData
-------- ---------- --------- ----------
webapp01 A 2025/9/10 上午 10:30 10.0.1.100
dbserver A 2025/9/8 上午 9:15 10.0.2.50
mail A 2025/9/1 下午 3:00 10.0.3.10

HostName RecordType Timestamp RecordData
-------- ---------- --------- ----------
webapp01 A 2025/9/10 上午 10:30 10.0.1.100

HostName RecordType Timestamp RecordData
-------- ---------- --------- ----------
test-api A 2025/9/9 上午 11:20 10.0.5.80
test-web A 2025/9/7 下午 2:45 10.0.5.81
test-db CNAME 2025/9/5 上午 8:00 dbserver.contoso.com.

创建 DNS 记录

使用 Add-DnsServerResourceRecord 系列 cmdlet 可以创建不同类型的 DNS 记录。常用的包括 A 记录、CNAME 记录、MX 记录和 TXT 记录。创建时需要指定区域名称、记录名称和对应的值。

下面展示如何创建几种常见的 DNS 记录类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 创建 A 记录——将 webapp02.contoso.com 指向 10.0.1.200
Add-DnsServerResourceRecordA -ZoneName "contoso.com" `
-Name "webapp02" `
-IPv4Address "10.0.1.200" `
-TimeToLive 01:00:00

# 创建 CNAME 记录——将 www 指向 webapp01
Add-DnsServerResourceRecordCName -ZoneName "contoso.com" `
-Name "www" `
-HostNameAlias "webapp01.contoso.com" `
-TimeToLive 01:00:00

# 创建 MX 记录——邮件交换记录
Add-DnsServerResourceRecordMX -ZoneName "contoso.com" `
-Name "." `
-MailExchange "mail.contoso.com" `
-Preference 10 `
-TimeToLive 01:00:00

# 创建 TXT 记录——例如 SPF 记录
Add-DnsServerResourceRecordTxt -ZoneName "contoso.com" `
-Name "@" `
-DescriptiveText "v=spf1 ip4:10.0.0.0/8 -all" `
-TimeToLive 01:00:00

# 验证刚创建的记录
Get-DnsServerResourceRecord -ZoneName "contoso.com" -Name "webapp02"

执行结果示例:

1
2
3
HostName  RecordType Timestamp            RecordData
-------- ---------- --------- ----------
webapp02 A 2025/9/12 上午 8:05 10.0.1.200

批量导入 DNS 记录

在实际运维场景中,经常需要根据 CSV 文件批量导入 DNS 记录,例如新机房上线或大批量服务迁移。以下脚本从一个结构化的 CSV 文件中读取记录信息,逐条创建对应的 DNS 记录,并在完成后输出操作摘要。

首先准备 CSV 文件 dns-records.csv,格式如下:

1
2
3
4
Name,Type,Value,TTL
app-server01,A,10.0.10.50,3600
app-server02,A,10.0.10.51,3600
api-gateway,CNAME,app-server01.contoso.com,3600

然后运行批量导入脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 批量导入 DNS 记录
$zoneName = "contoso.com"
$csvPath = "C:\Scripts\dns-records.csv"
$records = Import-Csv -Path $csvPath

$successCount = 0
$failCount = 0
$results = @()

foreach ($record in $records) {
$ttl = New-TimeSpan -Seconds $record.TTL

try {
switch ($record.Type) {
"A" {
Add-DnsServerResourceRecordA -ZoneName $zoneName `
-Name $record.Name `
-IPv4Address $record.Value `
-TimeToLive $ttl `
-ErrorAction Stop
}
"CNAME" {
Add-DnsServerResourceRecordCName -ZoneName $zoneName `
-Name $record.Name `
-HostNameAlias $record.Value `
-TimeToLive $ttl `
-ErrorAction Stop
}
default {
Write-Warning "不支持的记录类型: $($record.Type) ($($record.Name))"
continue
}
}

$successCount++
$results += [PSCustomObject]@{
Name = $record.Name
Type = $record.Type
Value = $record.Value
Status = "成功"
}
Write-Host " [OK] $($record.Type) 记录: $($record.Name) -> $($record.Value)" -ForegroundColor Green
}
catch {
$failCount++
$results += [PSCustomObject]@{
Name = $record.Name
Type = $record.Type
Value = $record.Value
Status = "失败: $($_.Exception.Message)"
}
Write-Host " [FAIL] $($record.Type) 记录: $($record.Name) -> $($_.Exception.Message)" -ForegroundColor Red
}
}

# 输出操作摘要
Write-Host "`n===== 导入摘要 =====" -ForegroundColor Cyan
Write-Host "成功: $successCount 条"
Write-Host "失败: $failCount 条"
Write-Host "总计: $($records.Count) 条"
$results | Format-Table -AutoSize

执行结果示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  [OK] A 记录: app-server01 -> 10.0.10.50
[OK] A 记录: app-server02 -> 10.0.10.51
[OK] CNAME 记录: api-gateway -> app-server01.contoso.com

===== 导入摘要 =====
成功: 3 条
失败: 0 条
总计: 3 条

Name Type Value Status
---- ---- ----- ------
app-server01 A 10.0.10.50 成功
app-server02 A 10.0.10.51 成功
api-gateway CNAME app-server01.contoso.com 成功

修改和删除 DNS 记录

DNS 记录的修改需要先获取旧记录的引用,然后用新值创建一条记录覆盖旧记录。删除操作则相对简单,直接指定名称和类型即可。以下脚本演示了如何安全地更新 IP 地址并清理过期记录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$zoneName = "contoso.com"

# 修改 A 记录——更新 webapp01 的 IP 地址
$oldRecord = Get-DnsServerResourceRecord -ZoneName $zoneName `
-Name "webapp01" -RRType A |
Select-Object -First 1

$newRecord = $oldRecord.Clone()
$newRecord.RecordData.IPv4Address = [System.Net.IPAddress]::Parse("10.0.1.150")

Set-DnsServerResourceRecord -ZoneName $zoneName `
-OldInputObject $oldRecord `
-NewInputObject $newRecord

Write-Host "已将 webapp01 的 IP 从 $($oldRecord.RecordData.IPv4Address) 更新为 $($newRecord.RecordData.IPv4Address)"

# 删除指定记录
Remove-DnsServerResourceRecord -ZoneName $zoneName `
-Name "webapp02" `
-RRType A `
-Force `
-Confirm:$false

Write-Host "已删除 webapp02 的 A 记录"

# 验证修改结果
Get-DnsServerResourceRecord -ZoneName $zoneName -Name "webapp01"

执行结果示例:

1
2
3
4
5
6
已将 webapp01 的 IP 从 10.0.1.100 更新为 10.0.1.150
已删除 webapp02 的 A 记录

HostName RecordType Timestamp RecordData
-------- ---------- --------- ----------
webapp01 A 2025/9/12 上午 8:10 10.0.1.150

DNS 记录审计报告

定期审计 DNS 记录有助于发现残留的过期记录、重复指向和异常配置。以下脚本生成一份简洁的审计报告,包含各类型记录的统计信息和即将过期的静态记录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
$zoneName = "contoso.com"

# 获取所有记录
$allRecords = Get-DnsServerResourceRecord -ZoneName $zoneName

# 按类型统计
$summary = $allRecords |
Group-Object RecordType |
Sort-Object Count -Descending

Write-Host "===== DNS 区域审计报告: $zoneName =====" -ForegroundColor Cyan
Write-Host "生成时间: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')`n"

foreach ($group in $summary) {
$pct = [math]::Round(($group.Count / $allRecords.Count) * 100, 1)
Write-Host ("{0,-10} {1,5} 条 ({2,5}%)" -f $group.Name, $group.Count, $pct)
}

Write-Host "`n总计: $($allRecords.Count) 条记录"

# 检查可能的过期 A 记录(超过 30 天未更新的静态记录)
$cutoff = (Get-Date).AddDays(-30)
$staleRecords = $allRecords | Where-Object {
$_.RecordType -eq "A" -and
$_.Timestamp -lt $cutoff -and
$_.Timestamp -gt [datetime]::MinValue
}

if ($staleRecords) {
Write-Host "`n--- 可能过期的 A 记录 (超过 30 天未更新) ---" -ForegroundColor Yellow
foreach ($rec in $staleRecords) {
Write-Host (" {0,-25} -> {1,-15} (最后更新: {2})" -f `
$rec.HostName, `
$rec.RecordData.IPv4Address, `
($rec.Timestamp.ToString("yyyy-MM-dd")))
}
Write-Host "`n共 $($staleRecords.Count) 条可能过期的记录,建议人工确认后清理。"
}
else {
Write-Host "`n未发现超过 30 天未更新的动态 A 记录。" -ForegroundColor Green
}

执行结果示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
===== DNS 区域审计报告: contoso.com =====
生成时间: 2025-09-12 08:15:00

A 42 条 ( 52.5%)
CNAME 18 条 ( 22.5%)
MX 3 条 ( 3.8%)
TXT 12 条 ( 15.0%)
SRV 5 条 ( 6.3%)

总计: 80 条记录

--- 可能过期的 A 记录 (超过 30 天未更新) ---
legacy-app -> 10.0.99.10 (最后更新: 2025-07-15)
old-printer -> 10.0.99.20 (最后更新: 2025-06-28)

共 2 条可能过期的记录,建议人工确认后清理。

注意事项

  1. 管理员权限要求:DnsServer 模块的所有写操作(添加、修改、删除)都需要以管理员身份运行 PowerShell。可以在脚本开头添加 #Requires -RunAsAdministrator 确保权限正确。

  2. 区域名称区分大小写:虽然 DNS 协议本身不区分大小写,但在 PowerShell cmdlet 中传入区域名称时应保持与 DNS 服务器上的一致,避免因大小写差异导致查询失败。

  3. TTL 设置要合理:为即将变更的记录设置较短的 TTL(如 300 秒),可以缩短切换期间的 DNS 缓存影响;长期稳定的记录则可使用较长的 TTL(如 3600 秒)以减轻 DNS 服务器负载。

  4. 修改记录必须使用 Clone 方法Set-DnsServerResourceRecord 要求同时提供旧记录和新记录对象。务必使用 Clone() 方法创建副本再修改,直接修改原始对象会导致比较失败。

  5. 批量操作建议使用事务日志:在批量导入或大规模变更前,先将现有记录导出为 CSV 备份。如果脚本中途出错,可以快速回滚到原始状态。

  6. CNAME 与 A 记录的冲突:同一个主机名不能同时存在 CNAME 记录和其他类型的记录。如果需要将 CNAME 改为 A 记录,必须先删除 CNAME 再创建 A 记录,顺序不能颠倒。

PowerShell 技能连载 - 网络诊断与排查

适用于 PowerShell 5.1 及以上版本

网络故障是运维工作中最常见的排障场景——“连不上数据库”、”网站打不开”、”文件共享超时”。PowerShell 内置了丰富的网络诊断命令,从基本的 ping、端口检测到 DNS 解析、路由追踪和 TCP 连接测试,可以快速定位网络问题的层级(物理层、链路层、网络层、传输层、应用层)。

本文将讲解 PowerShell 网络诊断的系统化方法,以及如何构建一键式排障脚本。

基础连通性测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Test-Connection(PowerShell 的 ping)
Test-Connection -ComputerName google.com -Count 4 |
Select-Object Address, @{N='延迟ms'; E={$_.ResponseTime}}, Status |
Format-Table -AutoSize

# Test-NetConnection(更强大的网络测试)
Test-NetConnection -ComputerName blog.vichamp.com -Port 443 |
Select-Object ComputerName, RemoteAddress, RemotePort, TcpTestSucceeded, PingSucceeded |
Format-List

# 批量测试多台服务器的连通性
$servers = @('web-01.internal', 'db-01.internal', 'api-01.internal', 'redis-01.internal')

$results = foreach ($server in $servers) {
$test = Test-NetConnection -ComputerName $server -WarningAction SilentlyContinue
[PSCustomObject]@{
Server = $server
IP = $test.RemoteAddress
Ping = $test.PingSucceeded
Port = if ($test.TcpTestSucceeded) { 'Open' } else { 'Closed/Filtered' }
}
}

$results | Format-Table -AutoSize

执行结果示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Address   延迟ms Status
------- ------ ------
google.com 12 True
google.com 15 True
google.com 11 True
google.com 14 True

ComputerName : blog.vichamp.com
RemoteAddress : 185.199.108.153
RemotePort : 443
TcpTestSucceeded : True
PingSucceeded : True

Server IP Ping Port
------ -- ---- ----
web-01.internal 192.168.1.101 True Open
db-01.internal 192.168.1.201 True Open
api-01.internal 192.168.1.102 True Open
redis-01.internal 192.168.1.202 False Closed/Filtered

DNS 诊断

DNS 解析问题是网络故障的高发区:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# DNS 解析
Resolve-DnsName -Name blog.vichamp.com | Format-Table -AutoSize

# 查看所有 DNS 记录类型
Resolve-DnsName -Name contoso.com -Type ANY |
Select-Object Name, Type, Section, Data |
Format-Table -AutoSize

# 查看当前 DNS 服务器配置
Get-DnsClientServerAddress -AddressFamily IPv4 |
Where-Object { $_.InterfaceAlias -notmatch 'Loopback' } |
Select-Object InterfaceAlias, ServerAddresses |
Format-Table -AutoSize

# 刷新 DNS 缓存
Clear-DnsClientCache
Write-Host "DNS 缓存已清除" -ForegroundColor Green

# DNS 解析对比(用不同 DNS 服务器)
function Compare-DnsResolution {
param([string]$Domain)

$servers = @{
'默认' = $null
'Google' = '8.8.8.8'
'Cloudflare' = '1.1.1.1'
'AliDNS' = '223.5.5.5'
}

foreach ($name in $servers.Keys) {
try {
$params = @{ Name = $Domain }
if ($servers[$name]) { $params.Server = $servers[$name] }

$result = Resolve-DnsName @params -ErrorAction Stop |
Select-Object -First 1

[PSCustomObject]@{
DNS服务器 = $name
IP地址 = $result.IPAddress
类型 = $result.Type
TTL = $result.TTL
}
} catch {
[PSCustomObject]@{
DNS服务器 = $name
IP地址 = "解析失败: $($_.Exception.Message)"
}
}
}
}

Compare-DnsResolution -Domain "blog.vichamp.com" | Format-Table -AutoSize

执行结果示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Name             Type  Section Data
---- ---- ------- ----
blog.vichamp.com CNAME Name victorwoo.github.io

InterfaceAlias ServerAddresses
-------------- ---------------
以太网 {192.168.1.1}

DNS服务器 IP地址 类型 TTL
--------- ------ ---- ---
默认 185.199.108.153 CNAME 3600
Google 185.199.108.153 CNAME 3600
Cloudflare 185.199.108.153 CNAME 3600
AliDNS 185.199.108.153 CNAME 3600

TCP 端口扫描

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function Test-TcpPort {
<#
.SYNOPSIS
测试目标主机的 TCP 端口连通性
#>
param(
[Parameter(Mandatory)]
[string]$ComputerName,

[int[]]$Ports = @(22, 80, 443, 3389, 5985, 5986, 8080, 1433, 3306, 6379),

[int]$TimeoutMs = 3000
)

$results = foreach ($port in $Ports) {
$tcpClient = New-Object System.Net.Sockets.TcpClient
$asyncResult = $tcpClient.BeginConnect($ComputerName, $port, $null, $null)
$waited = $asyncResult.AsyncWaitHandle.WaitOne($TimeoutMs, $false)

$isOpen = $false
if ($waited) {
try {
$tcpClient.EndConnect($asyncResult)
$isOpen = $true
} catch {
$isOpen = $false
}
}

$tcpClient.Close()

$serviceName = switch ($port) {
22 { 'SSH' }
80 { 'HTTP' }
443 { 'HTTPS' }
3389 { 'RDP' }
5985 { 'WinRM-HTTP' }
5986 { 'WinRM-HTTPS' }
8080 { 'HTTP-Alt' }
1433 { 'MSSQL' }
3306 { 'MySQL' }
6379 { 'Redis' }
default { 'Unknown' }
}

[PSCustomObject]@{
Port = $port
Service = $serviceName
Status = if ($isOpen) { 'Open' } else { 'Closed' }
}
}

$results | Format-Table -AutoSize
}

# 扫描目标服务器的常用端口
Test-TcpPort -ComputerName "192.168.1.100" -Ports @(22, 80, 443, 3389, 5985, 1433, 6379)

执行结果示例:

1
2
3
4
5
6
7
8
9
Port Service     Status
---- ------- ------
22 SSH Closed
80 HTTP Open
443 HTTPS Open
3389 RDP Open
5985 WinRM-HTTP Open
1433 MSSQL Open
6379 Redis Closed

构建一键式排障脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function Invoke-NetworkTroubleshooter {
<#
.SYNOPSIS
一键式网络排障脚本
#>
param(
[Parameter(Mandatory)]
[string]$Target,

[int]$Port = 443
)

Write-Host "========== 网络排障:$Target ==========" -ForegroundColor Cyan

# Step 1: DNS 解析
Write-Host "`n[1/5] DNS 解析" -ForegroundColor Yellow
try {
$dns = Resolve-DnsName -Name $Target -ErrorAction Stop
$ip = ($dns | Where-Object { $_.Type -eq 'A' } | Select-Object -First 1).IPAddress
if (-not $ip) {
$ip = ($dns | Select-Object -First 1).IPAddress
}
Write-Host " 解析结果:$Target => $ip" -ForegroundColor Green
} catch {
Write-Host " DNS 解析失败:$($_.Exception.Message)" -ForegroundColor Red
Write-Host " 建议:检查 DNS 配置或尝试使用其他 DNS 服务器" -ForegroundColor Yellow
return
}

# Step 2: ICMP Ping
Write-Host "`n[2/5] Ping 测试" -ForegroundColor Yellow
$ping = Test-Connection -ComputerName $ip -Count 3 -Quiet
if ($ping) {
$latency = (Test-Connection -ComputerName $ip -Count 1).ResponseTime
Write-Host " Ping 成功,延迟:${latency}ms" -ForegroundColor Green
} else {
Write-Host " Ping 失败(可能被防火墙阻止)" -ForegroundColor Yellow
}

# Step 3: TCP 端口测试
Write-Host "`n[3/5] TCP 端口测试 ($Port)" -ForegroundColor Yellow
$tcp = Test-NetConnection -ComputerName $ip -Port $Port -WarningAction SilentlyContinue
if ($tcp.TcpTestSucceeded) {
Write-Host " 端口 $Port 连接成功" -ForegroundColor Green
} else {
Write-Host " 端口 $Port 连接失败" -ForegroundColor Red
Write-Host " 建议:检查防火墙规则或目标服务是否运行" -ForegroundColor Yellow
}

# Step 4: 路由追踪
Write-Host "`n[4/5] 路由追踪 (tracert)" -ForegroundColor Yellow
$trace = tracert -d -h 15 -w 1000 $ip 2>$null
$trace | Select-Object -First 15 | ForEach-Object { Write-Host " $_" }

# Step 5: HTTP 测试(如果是 Web 服务)
if ($Port -in @(80, 443, 8080, 8443)) {
Write-Host "`n[5/5] HTTP 测试" -ForegroundColor Yellow
$protocol = if ($Port -in @(443, 8443)) { 'https' } else { 'http' }
try {
$response = Invoke-WebRequest -Uri "${protocol}://${Target}" `
-TimeoutSec 10 -UseBasicParsing -MaximumRedirection 0 -ErrorAction Stop
Write-Host " HTTP $($response.StatusCode) - $($response.StatusDescription)" -ForegroundColor Green
} catch {
$statusCode = $_.Exception.Response.StatusCode
if ($statusCode) {
Write-Host " HTTP $([int]$statusCode) - $($statusCode)" -ForegroundColor Yellow
} else {
Write-Host " HTTP 请求失败:$($_.Exception.Message)" -ForegroundColor Red
}
}
}

Write-Host "`n========== 排障完成 ==========" -ForegroundColor Cyan
}

# 一键排查网络问题
Invoke-NetworkTroubleshooter -Target "blog.vichamp.com" -Port 443

执行结果示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
========== 网络排障:blog.vichamp.com ==========

[1/5] DNS 解析
解析结果:blog.vichamp.com => 185.199.108.153

[2/5] Ping 测试
Ping 成功,延迟:45ms

[3/5] TCP 端口测试 (443)
端口 443 连接成功

[4/5] 路由追踪 (tracert)
1 <1 ms <1 ms <1 ms 192.168.1.1
2 5 ms 3 ms 3 ms 10.0.0.1
...

[5/5] HTTP 测试
HTTP 200 - OK

========== 排障完成 ==========

注意事项

  1. 防火墙影响:ICMP ping 被阻止不代表服务不可用,始终结合 TCP 端口测试判断
  2. DNS 缓存:排查 DNS 问题时先执行 Clear-DnsClientCache 清除缓存
  3. 超时设置:给网络测试设置合理的超时,避免脚本无限等待
  4. IPv4/IPv6:某些环境可能有 IPv6 相关问题,使用 -AddressFamily IPv4 强制使用 IPv4
  5. 权限要求:部分网络诊断命令(如路由追踪)需要管理员权限
  6. 安全合规:端口扫描功能仅用于授权的网络诊断和安全审计,不得用于未授权的扫描