← Back to Home

PowerShellのNetwork

Views: 904

You can try the commands on our interactive shell.

PowerShellはWindows環境におけるネットワーク管理を効率化する強力なツールです。主要な操作と実践的な例を以下のカテゴリに分けて解説します。


1. ネットワークアダプタの管理

Get-NetAdapterで接続状態やMACアドレスを確認:

# すべてのアダプタを表示
Get-NetAdapter

# 特定アダプタの詳細(例:Ethernet)
Get-NetAdapter -Name "Ethernet" | Format-List *

→ 接続速度「Speed」やステータス「Status」を確認可能[6][8]

Restart-NetAdapterで再起動:

Restart-NetAdapter -Name "Wi-Fi" -Confirm:$false

→ 無線アダプタを再起動[7]


2. IPアドレス設定

静的IPの設定(例:192.168.1.100/24):

New-NetIPAddress -InterfaceAlias "Ethernet" `
  -IPAddress 192.168.1.100 `
  -PrefixLength 24 `
  -DefaultGateway 192.168.1.1

→ ゲートウェイも同時設定可能[3][6]

DNSサーバーの変更

Set-DnsClientServerAddress -InterfaceAlias "Ethernet" `
  -ServerAddresses ("8.8.8.8", "8.8.4.4")

→ Google Public DNSを設定[3]


3. ネットワーク診断ツール

高度なpingテスト(ポート指定可能):

Test-NetConnection -ComputerName "google.com" -Port 443

→ 接続時間「ResponseTime」と結果「TcpTestSucceeded」を表示[2][6]

サブネット全体のスキャン

1..254 | ForEach-Object {
  Test-Connection -ComputerName "192.168.1.$_" -Count

Citations:

[1] https://learn.microsoft.com/zh-cn/powershell/scripting/samples/performing-networking-tasks?view=powershell-7.5

[2] https://juejin.cn/post/6844904105236758542

[3] https://blog.51cto.com/u_14308022/10321593

[4] https://searchdatacenter.techtarget.com.cn/9-23610/

[5] https://www.cnblogs.com/suv789/p/18287292

[6] https://blog.csdn.net/qq_44632427/article/details/143777356

[7] https://www.cnblogs.com/cqpanda/p/16589979.html

[8] https://blog.csdn.net/myxsL/article/details/140033710

[9] https://www.cnblogs.com/suv789/p/18211719

[10] https://learn.microsoft.com/zh-cn/training/modules/manage-network-service-settings-for-windows-devices-use-powershell-cmdlets/

[11] https://wenku.csdn.net/column/1o2gngw6dg

[12] https://docs.azure.cn/zh-cn/azure-local/manage/use-datacenter-firewall-powershell

[13] https://learn.microsoft.com/zh-cn/powershell/scripting/samples/sample-scripts-for-administration?view=powershell-7.5

[14] https://blog.csdn.net/weixin_28840811/article/details/147236493


## Example

PowerShellで「google.com」に5秒ごとにリクエスト(ping)を送るには、無限ループと`Start-Sleep`コマンドレットを組み合わせて実現できます。以下はその具体的な例です。

```powershell
while ($true) {
    Test-Connection -ComputerName "google.com" -Count 1
    Start-Sleep -Seconds 5
}

このスクリプトは以下の動作をします:

ポイント - Test-ConnectionはPowerShell標準のpingコマンドで、-Count 1で1回だけ送信します - Start-Sleepで待機時間を調整できます[3]


応用例:タイムスタンプ付きで結果を表示

while ($true) {
    $result = Test-Connection -ComputerName "google.com" -Count 1
    Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") $($result.Status)"
    Start-Sleep -Seconds 5
}

これにより、各pingの結果と時刻が一緒に表示されます。


バッチファイルやpingコマンドを使う方法

PowerShellでなくコマンドプロンプト流のやり方もありますが、pingコマンドの-wオプションは「応答待ち時間(タイムアウト)」であり、「ping間隔」ではありません[1][2]。したがって、確実に5秒ごとにpingしたい場合は上記のPowerShellループが最もシンプルです。


この方法で、PowerShellから5秒ごとにgoogle.comへpingを自動送信できます[3]。

Citations:

[1] https://superuser.com/questions/345214/how-can-i-perform-a-ping-every-x-minutes-and-check-the-response-time

[2] https://stackoverflow.com/questions/67090996/windows-cmd-powershell-ping-every-second-even-during-timing-out

[3] https://techexpert.tips/powershell/powershell-repeat-command-every-5-seconds/

[4] https://stackoverflow.com/questions/52273215/powershell-ping-continous-for-specific-duration-and-save-log

[5] https://powershell.one/tricks/network/ping

[6] https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-connection?view=powershell-7.5

[7] https://www.cbtnuggets.com/blog/certifications/microsoft/how-to-write-a-simple-ping-test-with-powershell

[8] https://www.reddit.com/r/PowerShell/comments/6eyhpv/whats_the_quickest_way_to_ping_a_computer/

[9] https://www.reddit.com/r/PowerShell/comments/17949iu/how_to_do_continuous_ping_to_multiple_ips_and/

[10] https://gist.github.com/jamesfreeman959/231b068c3d1ed6557675f21c0e346a9c

PowerShellでgoogle.comに対してGETリクエストを送るには、主にInvoke-WebRequestコマンドレットを使います。以下に具体的な例を示します。


基本的なGETリクエスト

$response = Invoke-WebRequest -Uri "https://www.google.com"
Write-Output $response.StatusCode
Write-Output $response.Content

クエリパラメータ付きのGETリクエスト(Google検索)

$response = Invoke-WebRequest -Uri "https://www.google.com/search?q=PowerShell"
Write-Output $response.Content

エラー処理付きの例

try {
    $response = Invoke-WebRequest -Uri "https://www.google.com"
    Write-Output $response.StatusCode
} catch {
    Write-Output $_.Exception.Message
}

ポイント


このように、PowerShellではシンプルなコマンドでgoogle.comへのGETリクエストが可能です。

Citations:

[1] https://qiita.com/kurukurupapa@github/items/c77b7be7f3c05453e75e

[2] https://learn.microsoft.com/ja-jp/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.5

[3] https://beagle-dog.com/powershell-rest-api-guide/

[4] https://note.com/mahalo_/n/n6ed8dd9a6514

[5] https://brightdata.jp/blog/%E5%90%84%E7%A8%AE%E3%81%94%E5%88%A9%E7%94%A8%E6%96%B9%E6%B3%95/powershell-invoke-webrequest-with-proxy

[6] https://qiita.com/nownaka/items/29c1c7211a775c774fb8

[7] https://zenn.dev/kzrashi/articles/e4c3f3d39de326

Try it Now!