代码检查一个驱动器的三个不同的驱动器号

时间:2016-12-15 20:51:58

标签: vbscript

所以我创建了一个读取以下文本文件的代码:

Diskspace   C:
Diskspace   D:
Diskspace   E:

Memory
CPU

并检查每种类型的"硬件的使用情况"列出。

我的计算机上只有一个磁盘C:所以如何为任何不起作用的磁盘发出错误信息?

我的脚本现在发生了什么,因为我有" On Error Resume Next",我的代码检查它可以分析的任何磁盘,并为每个磁盘输入答案(也就是磁盘C :)。例如,在我的wscript.echo框中,它说

Disk C: is 93.4% free.  
Disk D: is 93.4% free.  
Disk E: is 93.4% free.

我知道这是错的。

On Error Resume Next 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objReadFile = objFSO.OpenTextFile("M:\vbscripts\ServerHealthCheck_Control.txt")


message = ""

Do While objReadFile.AtEndOfStream <> True
    strLine = objReadFile.ReadLine

    If Left(strLine, 1) = "D" then
        letter = Mid(strLine, 11, 2)
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set drive = fso.GetDrive(letter)
        totalSpace = drive.TotalSize / 1024
        freeSpace = drive.AvailableSpace / 1024
        percentFree = freeSpace / totalSpace

        number = FormatNumber(percentFree*100, 2)
        'WARNING_MESSAGE = Mid(strLine, 14, 2)
        'ERROR_MESSAGE = Mid(strLine, 17, 2)

        message = message & "Drive " & letter & " is " & number & "% free." & vbCrlf

    Elseif Left(strLine, 1) = "M" then
        Const CONVERT = 1048576 ' This is total bytes in 1 MB.
        Const strComputer = "."
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
        Set colSettings = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
        For Each objOperatingSystem in colSettings
            intFreeMem = objOperatingSystem.FreePhysicalMemory / CONVERT
            intTotalMem = objOperatingSystem.TotalVisibleMemorySize / CONVERT
            Memory = intFreeMem / intTotalMem * 100

            message = message & "Memory is " & FormatNumber(Memory,2) & "% free." & vbCrlf
        Next

    Elseif Left(strLine, 1) = "C" then
        Set objWMIService = GetObject("winmgmts:\\localhost\root\CIMV2") 
        Set CPUInfo = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor",,48) 
        For Each Item in CPUInfo 
            If Item.Name = "_Total" Then
                    message = message & "Total CPU usage is " & Item.PercentProcessorTime & "%."
            End If
        Next
    End If

Loop

Wscript.Echo message

编辑:我尝试了以下但是它似乎不起作用:

If fso.GetDrive(letter) = error then
    message = message & "The drive " & letter & " cannot be read."
Elseif fso.GetDrive(letter) <> error then
    totalSpace = drive.TotalSize / 1024
    freeSpace = drive.AvailableSpace / 1024
    percentFree = freeSpace / totalSpace

    number = FormatNumber(percentFree*100, 2)
    message = message & "Drive " & letter & " is " & number & "% free." & vbCrlf
End If

显示E:是错误,但磁盘D仍然复制磁盘C.

2 个答案:

答案 0 :(得分:0)

您可以使用命令行实用程序wmic /?wmic logicaldisk get /?

获取有关WMI的帮助
wmic logicaldisk get caption,freespace

输出所有驱动器上的自由空间。在VBScript中,上面变成了

Set colSettings = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
For Each objdisk in colSettings
    wscript.echo objdisk.caption & " " & objdisk.freespace
Next

您可以在VBScript或WMIC中使用where子句。

wmic logicaldisk where (caption="C:" or caption="D:" or caption="E:") get caption,freespace

答案 1 :(得分:0)

将EVIL全局OERN替换为严格本地的OERN(调用包含风险操作的Sub / Function)。如:

Option Explicit

Dim goFS : Set goFS= CreateObject("Scripting.FileSystemObject")

Function RiskyGetDrive(sDL)
' maybe lots of lines here, immediate exit on first error
  Set RiskyGetDrive = goFS.GetDrive(sDL) ' could be unavailable
  Dim dummy : dummy = RiskyGetDrive.TotalSize ' could be not ready
End Function

Dim sDL, oDrv, aErr
For Each sDL in Split("C D E Q")
  ' error hiding for just one risky operation and copy of (possible) error data
  On Error Resume Next
    Set oDrv = RiskyGetDrive(sDL)
    aErr = Array(Err.Number, Err.Description)
  On Error GoTo 0
  If aErr(0) Then
     WScript.Echo sDL, "Diag:", aErr(0), aErr(1)
  Else
     'not needed, if "not ready" is provoked in function
     'If Not oDrv.IsReady Then
     '   WScript.Echo sDL, "Diag:", "not ready"
     'Else
     '   WScript.Echo sDL, "Info:", oDrv.TotalSize / 1024
     'End If
     WScript.Echo sDL, "Info:", oDrv.TotalSize / 1024
  End If
Next

输出:

cscript 41173138.vbs
C Info: 41929616
D Diag: 71 Disk not ready
E Info: 227784528
Q Diag: 68 Device unavailable