VBScript 2-part conundrum

Status
Not open for further replies.

LTParis

Ars Legatus Legionis
23,426
Subscriptor
Part of our routine here is to run this pstools batch script to determine disk space. It's quite limited because you have to define evey server and drive explicitly, and it has problems when finding disk sizes under 100GB. So I decided to plunder and pillage a couple scripts and came up with something that mostly works.:

Code:
Set iFSO = CreateObject("Scripting.FilesyStemObject") 
Set oFSO = CreateObject("Scripting.FileSystemObject") 
InputFile="computers.txt" 
Outputfile="Diskspacelist.csv" 
SET ofile = oFSO.CreateTextFile(OutputFile) 
SET ifile = iFSO.openTextFile(inputfile) 
Const GBCONVERSION= 1048576000 
ofile.writeline "Server,Drive,Disk Size,FreeSpace,DiskPercent" 

Do until ifile.AtEndOfLine 
Server = ifile.ReadLine 
SET objWMIService = GetObject("winmgmts://" & Server) 
SET colLogicalDisk = objWMIService.InstancesOf("Win32_LogicalDisk") 
FOR EACH objLogicalDisk In colLogicalDisk
Dim iThreshold
Dim DiskSpaceSize
Dim DiskPercent

IF objLogicalDisk.drivetype=3 then 
Disksize = FormatNumber(objLogicalDisk.size/GBCONVERSION,2) & "GB" 
DiskSpaceSize = objLogicalDisk.size
Freespace = FormatNumber(objLogicalDisk.freespace/GBCONVERSION,2) & "GB" 
DiskPercent = FormatPercent (objLogicalDisk.freespace/objLogicalDisk.size)
DeviceID = objLogicalDisk.DeviceID 


'If DiskPercent < 16 Then
	ofile.writeline Server & "," & DeviceID & "," & Disksize & "," & Freespace & "," & DiskPercent


END IF 
Next 

SET colLogicalDisk = NOTHING 
SET objWMIService = NOTHING 
Loop
wscript.Echo "Check complete"

Issue #1. Disksize and Freespace, if it goes beyond 1TB, it adds another comma when it's writing and throws my colums off. I have been trying to find how to strip that comma but have not found a proper solution.

Issue #2. As you see by a commended line out, I would desire only to write to the file if it's reached below a threshold (in this case 15%) but every time I try to insert the line it's looking for a next condition, and every time I place one in it does not work.
 

technophile

Ars Legatus Legionis
23,768
You can deal with commas by quoting the values, or you can do a replace on them; either will work.

Your IF needs a corresponding END IF.

i.e.:

Code:
...
DeviceID = objLogicalDisk.DeviceID 

If DiskPercent < 16 Then
   ofile.writeline Server & "," & DeviceID & "," & Replace(Disksize, ",", "") & "," & _
                   Replace(Freespace, ",", "") & "," & DiskPercent
End If

END IF
Next
...
(Includes both fixes.)
 

technophile

Ars Legatus Legionis
23,768
Ah, right. FormatPercent returns a string; you can't do a numeric comparison on a string. You want to replace this:
Code:
DiskPercent = FormatPercent (objLogicalDisk.freespace/objLogicalDisk.size)
...
If DiskPercent < 16 Then
   ofile.writeline Server & "," & DeviceID & "," & Replace(Disksize, ",", "") & "," & _
                   Replace(Freespace, ",", "") & "," & DiskPercent

with this:
Code:
DiskPercent = objLogicalDisk.freespace/objLogicalDisk.size
...
If DiskPercent < 0.16 Then
   ofile.writeline Server & "," & DeviceID & "," & Replace(Disksize, ",", "") & "," & _
                   Replace(Freespace, ",", "") & "," & FormatPercent(DiskPercent)
 
Status
Not open for further replies.