ZeroCool898
ZeroCool898

Reputation: 1

Script to check remote windows hard drives for free space

I am attempting to check the hard drive space on some remote servers here at the office. The file below debugs fine but then there is no output in the text file. I've tried PS and other VB code and they appear to work but I need or at least hope to get the data in a text file so that it can be saved.

Thoughts?

arrServers = Array("server.domain.net", "server2.domain.net", "server3.domain.net")
strFilePath = "freespace.txt"

On Error Resume Next
Set objFso = CreateObject("Scripting.FileSystemObject")
Set oFile = objFso.OpenTextFile(strFilePath, 2, vbTrue)

If Not IsNothing(oFile) Then
For Each strComputer In arrServers
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    If objWMIService Then
        Set colDiskDrives = objWMIService.ExecQuery _
            ("Select * from Win32_PerfFormattedData_PerfDisk_LogicalDisk Where " _
                & "Name <> '_Total'")

        For Each objDiskDrive In colDiskDrives
            oFile.WriteLine "Drive", objDiskDrive.Name, "on", strComputer, "has", _
            objDiskDrive.FreeMegabytes & "MB (" & objDiskDrive.PercentFreeSpace & "%) Free"
        Next

    Else
        oFile.WriteLine "Could not connect to " & strComputer
    End If

Next

Else
    WScript.Echo "Could not open text file."

End If

Upvotes: 0

Views: 4283

Answers (2)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38765

While in Tim's answer the EVIL Global OERN is correctly identified, the "should" wrt to checking for "oFile Is Nothing" is misleading.

This code

  Dim goFS      : Set goFS  = CreateObject( "Scripting.FileSystemObject" )
  Dim sBadFSpec : sBadFSpec = ".\nix\nix.txt"
  Dim tsOut, bIsNothing
 On Error Resume Next
  Set tsOut = goFS.OpenTextFile(sBadFSpec, ForWriting, True)
  If 0 <> Err.Number Then WScript.Echo "Bingo!", Err.Description 
 On Error GoTo 0
  WScript.Echo "tsOut:", VarType(tsOut), TypeName(tsOut)
 On Error Resume Next
  bIsNothing = tsOut Is Nothing
  If 0 <> Err.Number Then WScript.Echo "Bingo!", Err.Description 
 On Error GoTo 0

and its output:

Bingo! Path not found
tsOut: 0 Empty
Bingo! Object required

shows:

  1. If the .OpenTextFile() fails, the variable you are trying to assign to (tsOut) is unchanged, i.e. Empty (if we rule out re-using of a variable)
  2. Applying Is Nothing to a non-object (e.g. Empty) variable throws an error
  3. Check for Is Nothing is not the correct way to check the result of .Open/CreateTextFile().

UPDATE

Tim's comment made me realize that the above holds for VBScript only (here uninitialized variable are plain Variants of subtype Empty). Knowing nothing about VBA, I trust Tim that a test for oFile Is Nothing is a valid strategy in that language.

Upvotes: 0

Tim Williams
Tim Williams

Reputation: 166790

Remove the on error resume next and run it. What do you see?

I think this may be your problem:

If Not IsNothing(oFile) Then 

Should probably be

If Not oFile Is Nothing Then 

Upvotes: 1

Related Questions