Reputation: 3
So I have created a code that reads the following text file:
Diskspace C:
Diskspace D:
Diskspace E:
Memory
CPU
And check usage for each type of "hardware" listed.
I only have a disk C: on my computer, so how can I put an error message for any disk that doesn't work?
What happens in my script now is since I have "On Error Resume Next", my code checks whatever disk it can analyze, and inputs that answer (aka disk C:) for each disk. For example, in my wscript.echo box, it says
Disk C: is 93.4% free.
Disk D: is 93.4% free.
Disk E: is 93.4% free.
I know that this is wrong.
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
EDIT: I have tried the following but it doesn't seem to work:
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
It displays that E: is an error, but disk D still copies disk C.
Upvotes: 0
Views: 74
Reputation:
You can get help on WMI by using the command line utility wmic /?
and wmic logicaldisk get /?
wmic logicaldisk get caption,freespace
outputs the freespace on ALL drives. In VBScript the above becomes
Set colSettings = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
For Each objdisk in colSettings
wscript.echo objdisk.caption & " " & objdisk.freespace
Next
You can use a where clause in both VBScript or WMIC.
wmic logicaldisk where (caption="C:" or caption="D:" or caption="E:") get caption,freespace
Upvotes: 0
Reputation: 38755
Replace the EVIL global OERN with a strictly local one (a call of a Sub/Function containing the risky operation(s)). As in:
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
output:
cscript 41173138.vbs
C Info: 41929616
D Diag: 71 Disk not ready
E Info: 227784528
Q Diag: 68 Device unavailable
Upvotes: 0