vbs获取本机信息以及ping的监控检测

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/caixingyunzi/article/details/81669853

脚本背景

有时候跟非技术人员沟通的时候,你让人家提供主机信息或者让别人ping什么域名检测健康情况先行判断的时候,一般的人员可能无法理解,这对于他们可能是灾难性的操作,所以这边提供了一个vbs脚本直接发给非技术人员,让别人双击之后截图即可

程序本体

Function GetIPMAC(ComputerName) 
Dim objWMIService,colItems,objItem,objAddress
Set objWMIService = GetObject("winmgmts://" & ComputerName & "/root/cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objItem in colItems
 For Each objAddress in objItem.IPAddress
  If objAddress <> "" then
  GetIPMAC = "【本机信息】 "& vbCrLf & " IP: " & objAddress & vbCrLf & " MAC: " & objItem.MACAddress & vbCrLf & " DNS1: " & objItem.DNSServerSearchOrder(0) & vbCrLf & " DNS2: " & objItem.DNSServerSearchOrder(1)
  Exit For
 End If  
 Next
 Exit For
Next
End Function

Private Function PingHost(strHostOrIP, count)

    On Error Resume Next ' Start Error Handling

    'Set Variables
    Dim objPing, objRetStatus, i, strResult: strResult = ""

    For i = 1 to count
        Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address = '" & strHostOrIP & "'")
        For Each objRetStatus in objPing
            If IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 Then
                strResult = "无法连接"
                'WScript.Echo "Status code is " & objRetStatus.StatusCode
            Else
                strResult = "正常"
                'Wscript.Echo "Bytes = " & vbTab & objRetStatus.BufferSize
                'Wscript.Echo "Time (ms) = " & vbTab & objRetStatus.ResponseTime
                'Wscript.Echo "TTL (s) = " & vbTab & objRetStatus.ResponseTimeToLive
            End If
        Next

        ' Error Check
        If Err.Number <> 0 Then
            strResult = "无法连接"
        End If

        'Check For Success
        If strResult = "正常" Then
            Exit For
        End If
        count = count -1
    Next

    'Return Result
    PingHost = strResult

    On Error GoTo 0 ' End Error Handling

End Function

' ------ SCRIPT CONFIGURATION ------
Dim strHost: strHost = "www.baidu.com"
Dim intCount: intCount = 2
Dim pingHostResult: pingHostResult = PingHost(strHost, intCount)
Dim tip: tip = "【检测结果】" & vbCrLf & " 请求域名:" & strHost & vbCrLf & " 连接状态:" & pingHostResult & vbCrLf & vbCrLf & "【提示】" & vbCrLf & " 当前连接状态正常,如果无法访问系统,请使用IE9+浏览器并尝试清除浏览器缓存。如果还不行,请截图当前窗口,并在仓库或客服运营群@技术值班同学进行反馈"
' ------ END CONFIGURATION ------

If pingHostResult = "无法连接" Then
    tip = "【检测结果】" & vbCrLf & " 请求域名:" & strHost & vbCrLf & " 连接状态:" & pingHostResult & vbCrLf & vbCrLf & "【提示】" & vbCrLf & " 提示信息总体" & vbCrLf &" 1、提示信息1" & vbCrLf & " 2、提示信息2"
End If

Wscript.Echo GetIPMAC(".") & vbCrLf & vbCrLf & tip

把上述代码保存为.vbs文件发给非技术人员执行即可,效果如下:
这里写图片描述

展开阅读全文

没有更多推荐了,返回首页