• powershell 监控, 重启网卡


    #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
             }
        }
    }
    ​
  • 相关阅读:
    GetArxPath
    动态链接库
    获取文件名称 消除前面的绝对地址路径
    arx 插入图片
    cstring to utf8
    map 用法
    异常处理
    面向对象 "一"
    configparser模块
    装饰器
  • 原文地址:https://www.cnblogs.com/elvi/p/7718434.html
Copyright © 2020-2023  润新知