码迷,mamicode.com
首页 > 系统相关 > 详细

powershell 监控, 重启网卡

时间:2017-11-02 16:03:52      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:lin   span   pow   int   服务   dap   ping   UI   raw   

#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 监控, 重启网卡

标签:lin   span   pow   int   服务   dap   ping   UI   raw   

原文地址:http://www.cnblogs.com/elvi/p/7718434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!