powershell 監控, 重啟網卡
阿新 • • 發佈:2017-11-02
lan query pan apt res power itl out int
#powershell 監控, 重啟網卡
一臺Windows服務器,由於負荷較大,偶爾會網絡中斷,就寫了個powershell腳本,監控ip是否通,不通就重啟網卡
cls $host.UI.RawUI.WindowTitle = ‘……VM監控……’ $host.UI.RawUI.ForegroundColor=‘Green‘ #version V1.3.9 #t1間隔時間s $t1=5 $log=‘C:\Router.log.txt‘ #region $nu=0 $n1="##########" $n2="監控總次數" $cq1=0 $cq2=0 $ip1=‘192.168.0.1‘ $ip2=‘172.16.11.145‘ $ip3=‘172.16.11.8‘ #endregion if(Test-Path $log){}else { "初始化log" echo $n1 >$log echo $n2 >>$log echo $nu >>$log echo $n1 >>$log } while(1 -lt 2) { sleep $t1 cls $log1=Get-Content $log $nu=$log1[2] $nu=[int]$nu + 1 #print echo $n1 echo"$($n2):$($nu)" echo "監控間隔:$($t1)s" echo "重啟網卡:$($cq1)" echo "重啟路由:$($cq2)" echo $n1 #write echo $n1 >$log echo $n2 >>$log echo $nu >>$log echo $log1[3..($log1.count-1)] >>$log #check if ((Get-WmiObject -query "SELECT * FROM Win32_PingStatus WHERE Address = ‘$ip1‘").StatusCode -eq 0) { "The router is online!" #檢測內網 if ((Get-WmiObject -query "SELECT * FROM Win32_PingStatus WHERE Address = ‘$ip2‘").StatusCode -eq 0) { "The Lan is online!" } elseif ((Get-WmiObject -query "SELECT * FROM Win32_PingStatus WHERE Address = ‘$ip3‘").StatusCode -eq 0) { "The Lan is online!" } else { "重啟內網網卡" $cq1=$cq1 + 1 echo "重啟內網網卡, $(Get-Date -Format ‘yyyy-MM-dd HH:mm:ss.ffff‘)">>$log Get-NetAdapter -Name ‘本地連接‘ | Restart-NetAdapter Get-NetAdapter -Name ‘Lan‘ | Restart-NetAdapter sleep 30 } } else { "The router is offline!" "重啟路由網卡" $cq1=$cq1 + 1 echo "重啟路由網卡, $(Get-Date -Format ‘yyyy-MM-dd HH:mm:ss.ffff‘)">>$log Get-NetAdapter -Name ‘Wan2‘ | Restart-NetAdapter Get-NetAdapter -Name ‘Lan2‘ | Restart-NetAdapter sleep 30 #再次檢測 if ((Get-WmiObject -query "SELECT * FROM Win32_PingStatus WHERE Address = ‘$ip1‘").StatusCode -eq 0) { "The route is online!" } else { "重啟 router" $cq2=$cq2 + 1 echo "重啟 router, $(Get-Date -Format ‘yyyy-MM-dd HH:mm:ss.ffff‘)">>$log $vname=‘ikuai-router‘ Restart-VM $vname -Force sleep 100 } } } ?
powershell 監控, 重啟網卡