' Windows主机巡检脚本
' 获取主机名称
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
' 获取磁盘信息
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk")
' 获取CPU信息
Set colProcessors = objWMIService.ExecQuery("Select * from Win32_Processor")
' 获取内存信息
Set colMemory = objWMIService.ExecQuery("Select * from Win32_PhysicalMemory")
' 获取网络信息
Set colNetworkAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter")
' 创建文件系统对象
Set objFSO = CreateObject("Scripting.FileSystemObject")
' 检查文件是否存在,如果存在则删除
strFilePath = "C:\logs\check.txt"
If objFSO.FileExists(strFilePath) Then
objFSO.DeleteFile strFilePath
End If
' 创建文件
Set objFile = objFSO.CreateTextFile(strFilePath)
' 写入主机信息到文件
objFile.WriteLine "主机名称: " & strComputer & vbCrLf
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration")
For Each objItem In colItems
If Not IsNull(objItem.IPAddress) Then
For Each ipAddress In objItem.IPAddress
objFile.WriteLine "IP Address: " & ipAddress
Next
End If
Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfFormattedData_PerfOS_Processor")
totalUsage = 0
For Each objItem in colItems
totalUsage = totalUsage + objItem.PercentProcessorTime
Next
objFile.WriteLine "CPU使用率: " & totalUsage & "%"
' 获取内存使用率的VBScript示例
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfFormattedData_PerfOS_Memory")
For Each objItem in colItems
memoryUsage = objItem.PercentCommittedBytesInUse
Next
objFile.WriteLine "内存使用率:" & memoryUsage & "%"
' 获取磁盘
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfFormattedData_PerfDisk_LogicalDisk")
For Each objItem In colItems
diskName = objItem.Name
diskUsage = objItem.PercentFreeSpace
objFile.WriteLine "磁盘 " & diskName & " 使用率: " & 100 - diskUsage & "%"
Next
' 检查某个进程是否在运行
strProcessName = "fileserver.exe"
Set colProcesses = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process Where Name = '" & strProcessName & "'")
If colProcesses.Count > 0 Then
objFile.WriteLine "进程状态: " & " 正在运行"
Else
objFile.WriteLine " 进程状态: " & " 未找到"
End If
' 检查端口 23000 的VBScript示例
Const PORT_NUMBER = 28000
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("netstat -an")
bPortFound = False
Do While Not objExec.StdOut.AtEndOfStream
strLine = objExec.StdOut.ReadLine
If InStr(strLine, ":" & PORT_NUMBER) > 0 Then
bPortFound = True
Exit Do
End If
Loop
If bPortFound Then
objFile.WriteLine "端口状态: " & " 正常"
Else
objFile.WriteLine "端口状态: " & " 异常"
End If
' 巡检URL
strURL = "http://127.0.0.1/" ' 将URL替换为实际的URL
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.open "GET", strURL, False
objHTTP.send ""
' 检查HTTP状态码
If objHTTP.Status = 200 Or objHTTP.Status = 304 Then
objFile.WriteLine "URL巡检结果: " & " 正常"
Else
objFile.WriteLine "URL巡检结果: " & " 异常, HTTP状态码 " & objHTTP.Status
End If
' 关闭文件
objFile.Close
Windows主机巡检vbs脚本
于 2024-03-05 16:37:54 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)