• 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
             }
        }
    }
    ​
  • 相关阅读:
    结构体怎么组包发送
    开源语音代码eSpeak1.06 的移植到单片机的过程(二)之分析下speak.c 文件
    看看深圳的房价
    开源语音代码eSpeak1.06 的移植到单片机的过程(一)0之分析下espeak.c 文件
    开源语音代码eSpeak1.06 的学习入门
    利尔达模组CAT1 UIS8910指令的 TCP相关中文解释
    将博客搬至CSDN
    【原创】大叔问题定位分享(39)azkaban定期出现fullgc
    【原创】大叔经验分享(129)mac下启动MAT报错
    【原创】大数据基础之Doris(1)编译安装和启动
  • 原文地址:https://www.cnblogs.com/elvi/p/7718434.html
Copyright © 2020-2023  润新知