PowerShell 技能连载 - 网络操作技巧

在 PowerShell 中处理网络操作是一项常见任务,特别是在系统管理和自动化过程中。本文将介绍一些实用的网络操作技巧。

首先,让我们看看基本的网络连接测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 测试网络连接
$hosts = @(
"www.baidu.com",
"www.qq.com",
"www.taobao.com"
)

foreach ($host in $hosts) {
$result = Test-NetConnection -ComputerName $host -Port 80
Write-Host "`n测试 $host 的连接:"
Write-Host "是否可达:$($result.TcpTestSucceeded)"
Write-Host "响应时间:$($result.PingReplyDetails.RoundtripTime)ms"
}

获取网络配置信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 获取网络适配器信息
$adapters = Get-NetAdapter | Where-Object { $_.Status -eq "Up" }

foreach ($adapter in $adapters) {
Write-Host "`n网卡名称:$($adapter.Name)"
Write-Host "连接状态:$($adapter.Status)"
Write-Host "MAC地址:$($adapter.MacAddress)"

# 获取IP配置
$ipConfig = Get-NetIPConfiguration -InterfaceIndex $adapter.ifIndex
Write-Host "IP地址:$($ipConfig.IPv4Address.IPAddress)"
Write-Host "子网掩码:$($ipConfig.IPv4Address.PrefixLength)"
Write-Host "默认网关:$($ipConfig.IPv4DefaultGateway.NextHop)"
}

配置网络设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 配置静态IP地址
$adapterName = "以太网"
$ipAddress = "192.168.1.100"
$prefixLength = 24
$defaultGateway = "192.168.1.1"

# 获取网卡
$adapter = Get-NetAdapter -Name $adapterName

# 配置IP地址
New-NetIPAddress -InterfaceIndex $adapter.ifIndex -IPAddress $ipAddress -PrefixLength $prefixLength

# 配置默认网关
New-NetRoute -InterfaceIndex $adapter.ifIndex -NextHop $defaultGateway -DestinationPrefix "0.0.0.0/0"

网络流量监控:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 创建网络流量监控函数
function Monitor-NetworkTraffic {
param(
[string]$InterfaceName,
[int]$Duration = 60
)

$endTime = (Get-Date).AddSeconds($Duration)
$adapter = Get-NetAdapter -Name $InterfaceName

Write-Host "开始监控 $InterfaceName 的网络流量..."
Write-Host "监控时长:$Duration 秒"

while ((Get-Date) -lt $endTime) {
$stats = Get-NetAdapterStatistics -Name $InterfaceName
Write-Host "`n当前时间:$(Get-Date -Format 'HH:mm:ss')"
Write-Host "接收字节:$($stats.ReceivedBytes)"
Write-Host "发送字节:$($stats.SentBytes)"
Start-Sleep -Seconds 1
}
}

一些实用的网络操作技巧:

  1. DNS 解析:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # DNS 解析和反向解析
    $hostname = "www.baidu.com"
    $ip = "8.8.8.8"

    # 正向解析
    $dnsResult = Resolve-DnsName -Name $hostname
    Write-Host "`n$hostname 的IP地址:"
    $dnsResult | ForEach-Object { $_.IPAddress }

    # 反向解析
    $reverseResult = Resolve-DnsName -Name $ip -Type PTR
    Write-Host "`n$ip 的主机名:"
    $reverseResult.NameHost
  2. 端口扫描:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # 简单的端口扫描函数
    function Test-Port {
    param(
    [string]$ComputerName,
    [int[]]$Ports = @(80,443,3389,22)
    )

    foreach ($port in $Ports) {
    $result = Test-NetConnection -ComputerName $ComputerName -Port $port -WarningAction SilentlyContinue
    Write-Host "端口 $port$($result.TcpTestSucceeded)"
    }
    }
  3. 网络共享管理:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # 创建网络共享
    $shareName = "DataShare"
    $path = "C:\SharedData"
    $description = "数据共享文件夹"

    # 创建文件夹
    New-Item -ItemType Directory -Path $path -Force

    # 创建共享
    New-SmbShare -Name $shareName -Path $path -Description $description -FullAccess "Everyone"

    # 设置共享权限
    Grant-SmbShareAccess -Name $shareName -AccountName "Domain\Users" -AccessRight Read

这些技巧将帮助您更有效地处理网络操作。记住,在进行网络配置时,始终要注意网络安全性和性能影响。同时,建议在测试环境中先验证网络配置的正确性。

PowerShell 技能连载 - 网络操作技巧

http://blog.vichamp.com/2025/03/20/powershell-network-operations/

作者

吴波

发布于

2025-03-20

更新于

2025-03-25

许可协议

评论