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コマンドレットで「google.com」へ1回だけpingを送信- 5秒間待機(
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:
[3] https://techexpert.tips/powershell/powershell-repeat-command-every-5-seconds/
[5] https://powershell.one/tricks/network/ping
[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
-Uriでリクエスト先URLを指定します。$response.StatusCodeでHTTPステータスコード(例: 200)を確認できます。$response.Contentで取得したHTMLコンテンツを表示できます[2][3][6]。
クエリパラメータ付きのGETリクエスト(Google検索)
$response = Invoke-WebRequest -Uri "https://www.google.com/search?q=PowerShell"
Write-Output $response.Content
- この例では「PowerShell」でGoogle検索した結果のHTMLを取得します[1]。
エラー処理付きの例
try {
$response = Invoke-WebRequest -Uri "https://www.google.com"
Write-Output $response.StatusCode
} catch {
Write-Output $_.Exception.Message
}
- 例外が発生した場合はエラーメッセージを表示します[1]。
ポイント
Invoke-WebRequestはHTMLやJSONの生データ取得に適しています[3][5]。- Googleのトップページや検索結果は自動アクセスに対する制限があるため、頻繁なリクエストやスクレイピングは控えてください。
このように、PowerShellではシンプルなコマンドでgoogle.comへのGETリクエストが可能です。
Citations:
[1] https://qiita.com/kurukurupapa@github/items/c77b7be7f3c05453e75e
[3] https://beagle-dog.com/powershell-rest-api-guide/
[4] https://note.com/mahalo_/n/n6ed8dd9a6514