Vbscript 监视多台服务器磁盘空间的VB脚本
我正在寻找一个VBScript,以便每天使用一个计划任务来读取服务器列表并获取以下信息:Vbscript 监视多台服务器磁盘空间的VB脚本,vbscript,Vbscript,我正在寻找一个VBScript,以便每天使用一个计划任务来读取服务器列表并获取以下信息: 服务器名 总容量(GB) 已用容量(GB) 自由空间 可用空间百分比 然后,我希望这些信息用这些信息填充excel电子表格 然后,我想将每天收集的新数据附加到电子表格中,从而实现趋势分析,而不是将获得的信息写在上面 有人能帮我满足以上要求吗 strInputFile = "D:\Disk_Space\servers.txt" strOutputFile = "D:\Disk_S
strInputFile = "D:\Disk_Space\servers.txt"
strOutputFile = "D:\Disk_Space\disk_space_report.csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const adVarChar = 200
Const MaxCharacters = 255
Dim DataList
Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "Server", adVarChar, MaxCharacters
DataList.Fields.Append "C Free Space", adVarChar, MaxCharacters
DataList.Fields.Append "C Percent Free", adVarChar, MaxCharacters
DataList.Fields.Append "D Free Space", adVarChar, MaxCharacters
DataList.Fields.Append "D Percent Free", adVarChar, MaxCharacters
DataList.Fields.Append "E Free Space", adVarChar, MaxCharacters
DataList.Fields.Append "E Percent Free", adVarChar, MaxCharacters
DataList.Fields.Append "F Free Space", adVarChar, MaxCharacters
DataList.Fields.Append "F Percent Free", adVarChar, MaxCharacters
DataList.Open
Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading)
While Not objInputFile.AtEndOfStream
strComputer = objInputFile.ReadLine
Get_Free_Space_Details(strComputer)
Wend
Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
objOutputFile.WriteLine """SERVER"",""C FREE SPACE"",""C PERCENT FREE"",""D FREE SPACE"",""D PERCENT FREE"",""E FREE SPACE"",""E PERCENT FREE"",""F FREE SPACE"",""F PERCENT FREE"""
DataList.MoveFirst
While Not DataList.EOF
objOutputFile.WriteLine """" & DataList("Server") & """,""" & DataList("C Free Space") & """,""" & DataList("C Percent Free") & """,""" & DataList("D Free Space") & """,""" & DataList("D Percent Free") & """,""" & DataList("E Free Space") & """,""" & DataList("E Percent Free") & """,""" & DataList("F Free Space") & """,""" & DataList("F Percent Free") & """"
DataList.MoveNext
Wend
DataList.Close
objOutputFile.Close
MsgBox "Done. Please see " & strOutputFile
'==============
Sub Get_Free_Space_Details(strComputer)
DataList.AddNew
DataList("Server") = strComputer
If Ping(strComputer) = True Then
On Error Resume Next
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
If Err.Number = 0 Then
Err.Clear
On Error GoTo 0
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = 3")
For Each objDisk In colDisks
strDrive = objDisk.DeviceID
intFreeSpace = objDisk.FreeSpace
intTotalSpace = objDisk.Size
pctFreeSpace = intFreeSpace / intTotalSpace
Select Case UCase(strDrive)
Case "C:"
DataList("C Free Space") = Round(intFreeSpace / 1024 / 1024 / 1024, 2) & " GB"
DataList("C Percent Free") = FormatPercent(pctFreeSpace)
Case "D:"
DataList("D Free Space") = Round(intFreeSpace / 1024 / 1024 / 1024, 2) & " GB"
DataList("D Percent Free") = FormatPercent(pctFreeSpace)
Case "E:"
DataList("E Free Space") = Round(intFreeSpace / 1024 / 1024 / 1024, 2) & " GB"
DataList("E Percent Free") = FormatPercent(pctFreeSpace)
Case "F:"
DataList("F Free Space") = Round(intFreeSpace / 1024 / 1024 / 1024, 2) & " GB"
DataList("F Percent Free") = FormatPercent(pctFreeSpace)
End Select
Next
Set objDisk = Nothing
Set colDisks = Nothing
Set objWMIService = Nothing
Else
DataList("C Free Space") = "WMI ERROR"
DataList("C Percent Free") = "WMI ERROR"
DataList("D Free Space") = "WMI ERROR"
DataList("D Percent Free") = "WMI ERROR"
DataList("E Free Space") = "WMI ERROR"
DataList("E Percent Free") = "WMI ERROR"
DataList("F Free Space") = "WMI ERROR"
DataList("F Percent Free") = "WMI ERROR"
End If
Else
DataList("C Free Space") = "OFFLINE"
DataList("C Percent Free") = "OFFLINE"
DataList("D Free Space") = "OFFLINE"
DataList("D Percent Free") = "OFFLINE"
DataList("E Free Space") = "OFFLINE"
DataList("E Percent Free") = "OFFLINE"
DataList("F Free Space") = "OFFLINE"
DataList("F Percent Free") = "OFFLINE"
End If
DataList.Update
End Sub
Function Ping(strComputer)
Dim objShell, boolCode
Set objShell = CreateObject("WScript.Shell")
boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
If boolCode = 0 Then
Ping = True
Else
Ping = False
End If
End Function
嗨,Alex,我已经更新了我的答案,这段代码对我来说绝对有效,只是它会提示我一个msgBox-msgBox“完成。请参阅”&strOutputFile adn,直到我按ok键,它才将数据附加到输出文件中。你能帮我做同样的事情吗。如果你删除第39行MsgBox“完成。请看”&strOutputFile,它不工作了?嘿,亚历克斯,这工作了。。但是,当我通过批处理文件运行vbs脚本时,它会给我一个消息“按任意键继续…”,直到我按OK,它才将数据附加到输出文件中。这是另一个问题。检查批处理文件是否有回声或暂停。
' Check drive c free space
'
On Error Resume Next
Const MIN_FREE = 38.2 ' Gb
Const ForAppending = 8
Const HARD_DISK = 3
Const ForReading = 1
CONST ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set SrvList = objFSO.OpenTextFile("C:\Users\user\Desktop\Tests\servers.txt", ForReading)
Set oShell = CreateObject("WScript.Shell")
Set ReportFile = objFSO.OpenTextFile ("C:\Users\user\Desktop\Tests\disk_space_report.csv", ForAppending, True)
strComputer = "."
ReportFile.writeline "Computer" & vbTAB & "Drive C Free (Gb)" & vbTAB & "Status" & vbTAB & "Drive D Free (Gb)"
Do Until SrvList.AtEndOfStream
StrComputer = SrvList.Readline
'wscript.echo now & vbTAB & StrComputer
If Not IsConnectible(strComputer, "", "") Then
ReportFile.writeline(strComputer & vbTAB & " no available")
Else
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & " AND DeviceID = 'C:' ")
Set colDisks1 = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & " AND DeviceID = 'D:' ")
For Each objDisk in colDisks
FreeGB = objDisk.FreeSpace / (1024 * 1024 * 1024)
Size = objDisk.Size / 1073741824
UsedSpace = (objDisk.Size-objDisk.FreeSpace) / 1073741824
FreeGBPercent = FormatPercent(objDisk.FreeSpace/objDisk.Size)
strStatus = "ok"
If FreeGB < MIN_FREE Then strStatus = "Low disk"
ReportFile.writeline(strComputer & vbTAB & Round(FreeGB,2) & vbTAB & strStatus & vbTAB & Round(Size,2) & vbTAB & Round(UsedSpace,2) & vbTAB & FreeGBPercent)
Next
For Each objDisk in colDisks1
FreeGB = objDisk.FreeSpace / (1024 * 1024 * 1024)
Size = objDisk.Size / 1073741824
UsedSpace = (objDisk.Size-objDisk.FreeSpace) / 1073741824
FreeGBPercent = FormatPercent(objDisk.FreeSpace/objDisk.Size)
strStatus = "ok"
If FreeGB < MIN_FREE Then strStatus = "Low disk"
ReportFile.writeline(strComputer & vbTAB & Round(FreeGB,2) & vbTAB & strStatus & vbTAB & Round(Size,2) & vbTAB & Round(UsedSpace,2) & vbTAB & FreeGBPercent)
Next
End If
Loop
'
Wscript.Quit
Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 7, 2) & "/" & _
Mid(dtmBootup, 5, 2) & "/" & Left(dtmBootup, 4) _
& " " & Mid (dtmBootup, 9, 2) & ":" & _
Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
13, 2))
End Function
Function IsConnectible(sHost, iPings, iTO)
Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
Dim sTempFile, fFile
If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750
sTempFile = objFSO.GetSpecialFolder(2).ShortPath & "\" & objFSO.GetTempName
oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO & " " & sHost & ">" & sTempFile, 0 , True
Set fFile = objFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsASCII)
Select Case InStr(fFile.ReadAll, "TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select
fFile.Close
objFSO.DeleteFile(sTempFile)
End Function
"Computer Drive C Free (Gb) Status Drive D Free (Gb)"
"127.0.0.1 67.72 ok 237.87 170.15 28.47%"
"127.0.0.1 102.16 ok 443.21 341.06 23.05%"