疎通確認する PowerShell スクリプトを作ったのでメモする。
機能
・host名出力
・ipconfig出力
・WinHttpプロキシ設定確認
・以下の宛先に対する疎通確認(tnc 443 /tnc 80 / curl)出力
URL_1=yahoo.co.jp Yahoo! JAPAN
URL_2=abehiroshi.la.coocan.jp 阿部寛のホームページ (coocan.jp)
URL_3=google.com Google
*PowerShell において tnc は Test-NetConnection ,curl は Invoke-webRequest のエイリアスなので、それぞれ同じです。
・ファイル出力機能(実行フォルダに AzureBackup_Check_NW_YYYYMMMM_HHMMSS.log が生成されます)
実行方法
右クリック → PowerShellで実行
実行画面
下記のような感じ。阿部寛さんの公式HPがhttpsに対応していないっぽいことが分かる。
zip
Check_NW_Windows+ResultExample.zip
中身
Check_NW_Windows.ps1
CheckNWResult_20220320_204046.log
コード
Check_NW_Windows.ps1
####yamapan.tokyo
# Name directory
$static_logName = "CheckNWResult"
#Get Script Located Path
$logDirPath = $PSScriptRoot
# Set log location and filename
$logFilePath = "${logDirPath}\${static_logname}_$($(Get-Date).ToString("yyyyMMdd_HHmmss")).log"
###Target URL setting
$URLs=@()
$URLs+= "yahoo.co.jp"
$URLs+= "abehiroshi.la.coocan.jp"
$URLs+= "google.com"
#Start Transcript
Start-Transcript -path "${logFilePath}" -Append
Write-Output "-----ScriptStart----- $(Get-Date)"
###################################
#getInfos
function getInfos {
Write-Output ""
Write-Output "----------"
Write-Output "#####getInfo######"
Write-Output "###hostname"
$hostname=HOSTNAME.EXE
$hostname
Write-Output "###ipconfig"
$ipconfig=ipconfig.exe
$ipconfig
Write-Output "----------"
Write-Output ""
}
##tnc func
function Check_connect443 ($Test_URLs){
Write-Output "#TRY!! Test-NetConnection $Test_URLs port 443 "
$PortNo= "443"
$resulttnc=tnc $Test_URLs -Port $PortNo
$resultComputername=$resulttnc.computername
$resultRemoteaddress=$resulttnc.remoteaddress.IPAddressToString
$resultTcpTestSucceeded=$resulttnc.TcpTestSucceeded
$resultSourceIP=$resulttnc.SourceAddress.IPAddress
Write-Output "Result of tnc - $portNo / from $resultSourceIP /To URL $resultComputername IPaddress $resultRemoteaddress / TcpTestSucceeded: $resultTcpTestSucceeded "
Start-Sleep -Seconds 1
}
function Check_connect80 ($Test_URLs){
Write-Output "#TRY!! Test-NetConnection $Test_URLs port 80 "
$PortNo= "80"
$resulttnc=tnc $Test_URLs -Port $PortNo
$resultComputername=$resulttnc.computername
$resultRemoteaddress=$resulttnc.remoteaddress.IPAddressToString
$resultTcpTestSucceeded=$resulttnc.TcpTestSucceeded
$resultSourceIP=$resulttnc.SourceAddress.IPAddress
Write-Output "Result of tnc - $portNo / from $resultSourceIP /To URL $resultComputername IPaddress $resultRemoteaddress / TcpTestSucceeded: $resultTcpTestSucceeded "
Start-Sleep -Seconds 1
}
function Check_curl($Test_URLs) {
Write-Output "#TRY!! Invoke-webRequest $Test_URLs "
$Invoke=Invoke-webRequest $Test_URLs
$InvokeStatusCode=$Invoke.StatusCode
$InvokeStatusDescription=$Invoke.StatusDescription
Write-Output "Result of Invoke-webRequest / StatusCode: $InvokeStatusCode/ StatusDescription: $InvokeStatusDescription"
}
#winhttp_proxyCheck
function http_proxy(){
Write-Output ""
Write-Output "--------------------------------------------"
Write-Output "##Check WinHttpProxy"
netsh winhttp show proxy
Write-Output "--------------------------------------------"
}
#########################################################################################################
###GetInfos
getInfos;
###WInhttpProxy
http_proxy;
#######疎通確認
Write-Output ""
Write-Output "--------------------------------------------"
Write-Output "### Check connection to Yahoo! , abehiroshi ,google ### "
foreach ($URL in $URLs) {
Write-Output "## Check connection to $URL ## "
Check_connect443($URL)
Check_connect80 ($URL)
Check_curl($URL)
}
Write-Output "-----ScriptStart Completed----- $(Get-Date)"
##Complete Transcript
Stop-Transcript
実行結果例
CheckNWResult_20220320_201207.log
********************** Windows PowerShell transcript start Start time: 20220320204046 Username: VM-WIN-2019\test-admin RunAs User: VM-WIN-2019\test-admin Configuration Name: Machine: VM-WIN-2019 (Microsoft Windows NT 10.0.17763.0) Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'C:\Users\test-admin\Desktop\Check_NW_Windows.ps1' Process ID: 7480 PSVersion: 5.1.17763.2268 PSEdition: Desktop PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.17763.2268 BuildVersion: 10.0.17763.2268 CLRVersion: 4.0.30319.42000 WSManStackVersion: 3.0 PSRemotingProtocolVersion: 2.3 SerializationVersion: 1.1.0.1 ********************** Transcript started, output file is C:\Users\test-admin\Desktop\CheckNWResult_20220320_204046.log -----ScriptStart----- 03/20/2022 20:40:46 ---------- #####getInfo###### ###hostname VM-Win-2019 ###ipconfig Windows IP Configuration Ethernet adapter ?????? 2: Connection-specific DNS Suffix . : w3omyutrbuhebdknhpx3okxd5g.lx.internal.cloudapp.net Link-local IPv6 Address . . . . . : fe80::7855:b178:f783:c5c8%22 IPv4 Address. . . . . . . . . . . : 10.2.0.4 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 10.2.0.1 ---------- -------------------------------------------- ##Check WinHttpProxy Current WinHTTP proxy settings: Direct access (no proxy server). -------------------------------------------- -------------------------------------------- ### Check connection to Yahoo! , abehiroshi ,google ### ## Check connection to yahoo.co.jp ## #TRY!! Test-NetConnection yahoo.co.jp port 443 Result of tnc - 443 / from 10.2.0.4 /To URL yahoo.co.jp IPaddress 183.79.219.252 / TcpTestSucceeded: True #TRY!! Test-NetConnection yahoo.co.jp port 80 Result of tnc - 80 / from 10.2.0.4 /To URL yahoo.co.jp IPaddress 183.79.219.252 / TcpTestSucceeded: True #TRY!! Invoke-webRequest yahoo.co.jp Result of Invoke-webRequest / StatusCode: 200/ StatusDescription: OK ## Check connection to abehiroshi.la.coocan.jp ## #TRY!! Test-NetConnection abehiroshi.la.coocan.jp port 443 WARNING: TCP connect to (222.158.205.72 : 443) failed Result of tnc - 443 / from 10.2.0.4 /To URL abehiroshi.la.coocan.jp IPaddress 222.158.205.72 / TcpTestSucceeded: False #TRY!! Test-NetConnection abehiroshi.la.coocan.jp port 80 Result of tnc - 80 / from 10.2.0.4 /To URL abehiroshi.la.coocan.jp IPaddress 222.158.205.72 / TcpTestSucceeded: True #TRY!! Invoke-webRequest abehiroshi.la.coocan.jp Result of Invoke-webRequest / StatusCode: 200/ StatusDescription: OK ## Check connection to google.com ## #TRY!! Test-NetConnection google.com port 443 Result of tnc - 443 / from 10.2.0.4 /To URL google.com IPaddress 172.217.175.78 / TcpTestSucceeded: True #TRY!! Test-NetConnection google.com port 80 Result of tnc - 80 / from 10.2.0.4 /To URL google.com IPaddress 172.217.175.78 / TcpTestSucceeded: True #TRY!! Invoke-webRequest google.com Result of Invoke-webRequest / StatusCode: 200/ StatusDescription: OK -----ScriptStart Completed----- 03/20/2022 20:41:20 ********************** Windows PowerShell transcript end End time: 20220320204120 **********************
PowerShell ファイル出力部分
まず、ファイル出力する先を設定する。
下記の例だとスクリプトがあるフォルダに CheckNWResult_YYYYMMDD_HHMMSS.log という命名規則で出力する
# Name directory
$static_logName = "CheckNWResult"
#Get Script Located Path
$logDirPath = $PSScriptRoot
# Set log location and filename
$logFilePath = "${logDirPath}\${static_logname}_$($(Get-Date).ToString("yyyyMMdd_HHmmss")).log"
次にファイル出力する記述を下記のように各
#Start
Transcript Start-Transcript -path "${logFilePath}" -Append
Write-Output "-----ScriptStart----- $(Get-Date)"
#この間に出力したい処理を書く#
#この間に出力したい処理を書く#
Write-Output "-----ScriptStart Completed----- $(Get-Date)"
課題
おそらくもう少しシンプルに書けると思う。