自己写了个脚本用来监视指定IP地址,如果PING不通就关闭计算机,但放入SERVER 2008开机脚本中却只执行了一部分(objLOG.WriteLine(FormatLogTEXT(0, "脚本开始工作"))),以后不管指定IP通与不通后面的语句都没能正确执行,请求各位帮忙看一下问题出在哪儿,是否是权限?在本机测试和登录SERVER 2008 后测试正常。
- '脚本定时PING指定IP,如果PING不能则关闭计算机
- '保护文件服务器硬盘不受停电损害,在非在线式UPS维持下可以关机
- '脚本将生成日志
-
- Dim strIP '检测的IP
- Dim objWSH '执行PING的对象
- Dim objFSO '日志写入的对象
- Dim objLOG '日志
- Dim strLOGFILE '日志文件名
- Dim strOUT '接收PING执行结果的对象
- Dim oRE '正则对象
- Dim strLine '从结果中读取的行
- Dim lFLAG '关机标志
- Dim nSleep 'PING间隔 单位:秒
- Dim nTimeOut '超时次数, 防止偶尔PING不能
- Dim nCount '计数器
- Dim nLineNum '行号计数器
- Dim strTEXT '显示或写入日志的内容变量, 使脚本简洁些
- Dim strErrTEXT '错误时的提示
-
- nLineNum = 1
- nTimeOut = 20
- nCount = 0
- nSleep = 20 * 1000
- strIP = "192.168.180.110"
- strLOGFILE = "D:\doc\ScanLog.txt"
- strErrTEXT = "目标地址PING不通!可能停电, 可能交换机故障!稍后将关闭服务器!"
-
- Set objFSO = CreateObject("Scripting.FileSystemObject")
- Set objLOG = objFSO.OpenTextFile(strLOGFILE, 8, True)
- objLOG.WriteLine("==========================================================")
- objLOG.WriteLine(FormatLogTEXT(0, "脚本开始工作"))
- ' <只执到这儿,日志文件里只留了什么时候开始工作,以后不管通与不通都不执行>
- Set objWSH = CreateObject("WScript.shell")
- Set oRE = New RegExp
-
- While True
- Set strOUT = objWSH.Exec("ping " & strIP)
- While Not strOUT.StdOut.AtEndOfStream
- strLine = strOUT.StdOut.ReadLine
- oRE.Pattern = "^请求超时|^Reply|^Request|目标主机无法访问"
- lFLAG = oRE.Test(strLine)
- If lFLAG Then
- nCount = nCount + 1
- strTEXT = FormatLogTEXT(nLineNum, "目标地址PING不通!")
- WScript.Echo strTEXT
- objLOG.WriteLine(strErrTEXT)
- If nCount >= nTimeOut Then
- nLineNum = nLineNum + 1
- strTEXT = FormatLogTEXT(nLineNum, "准备关机!")
- objLOG.WriteLine(strTEXT)
- WScript.Echo strErrTEXT
- WScript.Sleep nSleep
- '关机代码
- objWSH.Exec("Shutdown -s")
- Set objFSO = Nothing
- Set objWSH = Nothing
- Set objLOG = Nothing
- Set oRE = Nothing
- WScript.Quit
- End If
- nLineNum = nLineNum + 1
- Else
- '如果PING通计数器复位
- oRE.Pattern = "TTL=64"
- lFLAG = oRE.Test(strLine)
- If lFLAG Then
- nCount = 0
- WScript.Echo "Ping OK!"
- End If
- End If
- Wend
- WScript.Sleep nSleep
- Wend
-
- Function CNWeekday(sn)
- Dim strR
- Select Case Weekday(sn)
- Case 1
- strR = "星期天"
- Case 2
- strR = "星期一"
- Case 3
- strR = "星期二"
- Case 4
- strR = "星期三"
- Case 5
- strR = "星期四"
- Case 6
- strR = "星期五"
- Case 7
- strR = "星期六"
- End Select
- CNWeekday = strR
- End Function
-
- Function FormatLogTEXT(nLN, strTxt)
- FormatLogTEXT = nLN & Chr(9) & Date & CNWeekday(Date) & " " & time & " " & chr(9) & strTxt
- End Function
复制代码
|