Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot Determine Cause of (null): 0x80041010 Error 1

Status
Not open for further replies.

JustScriptIt

Technical User
Oct 28, 2011
73
0
0
US
I modified code such that, it will bypass IP addresses it cannot make WMI connection to.

However, there is one IP address which causes the program to throw an error

Whether it is several IP addresses, or that one particular IP address in the list, it causes the error on line 65

Code:
free_disk_space_csv.vbs(65, 4) (null): 0x80041010

And line 65 is

Code:
   For Each objItem in colItems


Below is entire code:

Code:
Option Explicit

Dim objInputFSO, objOutputFSO, objFile, objErrorOutputFSO, objErrorFile
Dim strInputFile, strData, arrLines, strLine
Dim objWMISvc, colItems, objItem, strComputerDomain, strMember
Dim strDriveType, strDiskSize
Dim strComputer, strKeyPath, strValueName, oReg, strValue, dwValue 
Dim objLogicalDisk, strCfree, strDfree, strHDfree
Dim strName, strVersion, intComm, strGroup, strPolicy, strHardware, strVirus, strIPS, intNTP, strSEPM
Dim strArray
Dim bErrorFound

const ForAppending = 8
const strOutputFile = "output_for_free_disk_space.csv"
const strErrOutputFile = "error_output_for_free_disk_space.csv"

'Create an Input File System Object
Set objInputFSO = CreateObject("Scripting.FileSystemObject")

'Name of the input text file
strInputFile = InputBox("What is the name of file with list of computers?")

'Open the text file - strData now contains the whole file
strData = objInputFSO.OpenTextFile(strInputFile,1).ReadAll

'Split the text file into lines
arrLines = Split(strData,vbCrLf)

'Create an Output File System Object
Set objOutputFSO = CreateObject("Scripting.FileSystemObject")

'Create Output File
Set objFile = objOutputFSO.CreateTextFile(strOutputFile)

'Create an Output File System Object for Recording Error
Set objErrorOutputFSO = CreateObject("Scripting.FileSystemObject")

'Create Output File
Set objErrorFile = objOutputFSO.CreateTextFile(strErrOutputFile)

objFile.WriteLine("IP Address,Computer,Drive Letter,Drive Type, Disk Size, Free Space")

'Step through the lines
For Each strLine in arrLines

  strComputer = strLine

  bErrorFound = False

  ' Can we connect to \root\cimv2 namespace?
  On Error Resume Next

    Set objWMISvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

  If (Err.Number <> 0) Then
     objErrorFile.WriteLine(strComputer & " is unreachable")
     bErrorFound = True
  End If
  On Error Goto 0

 If bErrorFound = False Then

   Set colItems = objWMISvc.ExecQuery("Select * from Win32_LogicalDisk")

   For Each objItem in colItems
     Select Case objItem.DriveType
       Case 1 strDriveType = "Drive could not be determined."
       Case 2 strDriveType = "Removable Drive"
       Case 3 strDriveType = "Local hard disk."
       Case 4 strDriveType = "Network disk." 
       Case 5 strDriveType = "Compact disk (CD)" 
       Case 6 strDriveType = "RAM disk." 
       Case Else strDriveType = "Drive type Problem."
     End Select

     If objItem.DriveType =2 Then 
       strDiskSize = Int(objItem.Size /1048576) & " Mega Bytes" 
     Else
       strDiskSize = Int(objItem.Size /1073741824) & " GB" 
     End If 

     objFile.WriteLine(strComputer & "," & objItem.SystemName & "," & objItem.Name & "," & strDriveType & _
       "," & strDiskSize & "," & Int(objItem.FreeSpace /1073741824) & " GB")

    Next

    objFile.WriteLine("")

  End If

Next

  Wscript.echo "Output is located at output_for_free_disk_space.csv"

'Cleanup
Set objInputFSO=Nothing
Set objOutputFSO=Nothing
 
One other thing:

I ran the script with debugging debugging option

cscript //x free_disk_space_csv.vbs

and I "Step Into" the code, line by line.

1. There is no error when program attempts to connect to WMI of IP address
2. Code progresses normally till it reaches line 65, i.e.

Code:
For Each objItem in colItems

Before it reaches line 65, it executes on the line 64, i.e.

Code:
Set colItems = objWMISvc.ExecQuery("Select * from Win32_LogicalDisk")

Are there any reasons why this line would cause a failure? This computer is Windows XP, SP3.

I'm really stumped!
 
Suppose nothing comes back from the query... then there would be no objItems in colItems and so the object couldn't be set as there would be no items in colItems - this may cause that error?

^^^Guesswork by the way...

Cheers,
Dave

"Yes, I'll stop finding bugs in the software - as soon as you stop writing bugs into the software." <-- Me

For all your testing needs: Forum1393
 
I am re-doing the code so it checks for errors, i.e if colItems is null.


How does the Err.Number get reset? It seems that once it is a non-zero number, it doesn't get reset with On Error Goto 0.


Error Checking Code

Code:
 If (Err.Number <> 0) Then
     On Error Goto 0
     objErrorFile.WriteLine("Cannot connect to WMI of " & strComputer)
     bErrorFound = True
  Else
    Set colItems = objWMISvc.ExecQuery("Select * from Win32_LogicalDisk")
    If (Err.Number <> 0) Then
      On Error Goto 0
      objErrorFile.WriteLine(strComputer & "does not support Win32_OperatingSystem")
      bErrorFound = True
    Else
       intDrive = colItems.strDriveType
       If (Err.Number <> 0) Then
         On Error Goto 0
         objErrorFile.WriteLine(strComputer & " may have damaged WMI")
         bErrorFound = True
       End If
    End If
  End If

Any advice?
 
I left out the important parts, re-posting

Code:
strComputer = strLine

bErrorFound = False

' Can we connect to \root\cimv2 namespace?
On Error Resume Next

Set objWMISvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

  If (Err.Number <> 0) Then
     On Error Goto 0
     objErrorFile.WriteLine("Cannot connect to WMI of " &      
     strComputer)
     bErrorFound = True
  Else
    Set colItems = objWMISvc.ExecQuery("Select * from    
    Win32_LogicalDisk")
    If (Err.Number <> 0) Then
      On Error Goto 0
      objErrorFile.WriteLine(strComputer & "does not support 
      Win32_OperatingSystem")
      bErrorFound = True
    Else
       intDrive = colItems.strDriveType
       If (Err.Number <> 0) Then
         On Error Goto 0
         objErrorFile.WriteLine(strComputer & " may have damaged 
          WMI")
         bErrorFound = True
       End If
    End If
  End If
 
How does the Err.Number get reset?
Err.Clear

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Code works, but why would colItems be blank? Does this mean the computer doesn't have any local or external hard drives? It doesn't make sense.

On the other hand, I could read the registry values of this particular IP address just fine.
 
FYI, Below is working code that checks free disk space, in case anyone wants to use it:

Code:
Option Explicit


''''''''''Declare Variables''''''''''

Dim objInputFSO, objOutputFSO, objFile, objErrorOutputFSO, objErrorFile
Dim strInputFile, strData, arrLines, strLine
Dim objWMISvc, colItems, objItem, strComputerDomain, strMember
Dim strDriveType, strDiskSize
Dim strComputer, strKeyPath, strValueName, oReg, strValue, dwValue 
Dim objLogicalDisk, strCfree, strDfree, strHDfree
Dim strName, strVersion, intComm, strGroup, strPolicy, strHardware, strVirus, strIPS, intNTP, strSEPM
Dim strArray
Dim bErrorFound, intDrive
Dim Shell, strCommand, ReturnCode, bUnreachable


''''''''''Declare Constants''''''''''

const ForAppending = 8
const strOutputFile = "output_for_free_disk_space.csv"
const strErrOutputFile = "error_output_for_free_disk_space.txt"


''''''''''Declare Sub Routines''''''''''

Sub HandleError()
     On Error Goto 0
     Err.Clear 
     bErrorFound = True
End Sub


''''''''''Declare Functions''''''''''

Function Ping(strComputer)   
  bUnreachable = False  
  Set Shell = wscript.createObject("wscript.shell")
  strCommand = "ping -n 1 " & strComputer
  ReturnCode = Shell.Run(strCommand, 0, True)

  If ReturnCode <> 0 Then
   objErrorFile.WriteLine(strComputer & " is not reachable.")
   bUnreachable = True
 End If

End Function


''''''''''Begin Execution''''''''''

'Create an Input File System Object
Set objInputFSO = CreateObject("Scripting.FileSystemObject")

'Name of the input text file
strInputFile = InputBox("What is the name of file with list of computers?")

'Open the text file - strData now contains the whole file
strData = objInputFSO.OpenTextFile(strInputFile,1).ReadAll

'Split the text file into lines
arrLines = Split(strData,vbCrLf)

'Create an Output File System Object
Set objOutputFSO = CreateObject("Scripting.FileSystemObject")

'Create Output File
Set objFile = objOutputFSO.CreateTextFile(strOutputFile)

'Create an Output File System Object for Recording Error
Set objErrorOutputFSO = CreateObject("Scripting.FileSystemObject")

'Create Output File
Set objErrorFile = objOutputFSO.CreateTextFile(strErrOutputFile)

objFile.WriteLine("IP Address,Computer,Drive Letter,Drive Type, Disk Size, Free Space")

'Step through the lines
For Each strLine in arrLines

  strComputer = strLine


  Ping(strComputer)

  '''''''''Error Handling Only if Computer Was Reachable By 
   Pinging''''''''''

  If bUnreachable = False Then

    bErrorFound = False
    On Error Resume Next

    Set objWMISvc = GetObject("winmgmts:
    {impersonationLevel=impersonate}!\\" & strComputer 
    & "\root\cimv2")

    If (Err.Number <> 0) Then
      HandleError()
      objErrorFile.WriteLine("Cannot connect to WMI of " & 
      strComputer)
    Else
      Set colItems = objWMISvc.ExecQuery("Select * from 
      Win32_LogicalDisk")
      If (Err.Number <> 0) Then
        HandleError()
        objErrorFile.WriteLine(strComputer & "does not support 
        Win32_LogicalDisk")
      Else
        intDrive = colItems.Count
        If (Err.Number <> 0) Then
          HandleError()
          objErrorFile.WriteLine("Query for " & strComputer & " 
          turned up blank.")
        End If
     End If
   End If
  

   ''''''''''If there were no errors''''''''''
   If bErrorFound = False Then

     'Set colItems = objWMISvc.ExecQuery("Select * from 
     Win32_LogicalDisk")

     For Each objItem in colItems
       Select Case objItem.DriveType
         Case 1 strDriveType = "Drive could not be determined."
         Case 2 strDriveType = "Removable Drive"
         Case 3 strDriveType = "Local hard disk."
         Case 4 strDriveType = "Network disk." 
         Case 5 strDriveType = "Compact disk (CD)" 
         Case 6 strDriveType = "RAM disk." 
         Case Else strDriveType = "Drive type Problem."
       End Select

       If objItem.DriveType =2 Then 
         strDiskSize = Int(objItem.Size /1048576) & " Mega Bytes" 
       Else
         strDiskSize = Int(objItem.Size /1073741824) & " GB" 
       End If 

       objFile.WriteLine(strComputer & "," & objItem.SystemName 
       & "," & objItem.Name & "," & strDriveType & _
         "," & strDiskSize & "," & Int
       (objItem.FreeSpace /1073741824) & " GB")

      Next

      objFile.WriteLine("")

    End If
  End If

Next

  Wscript.echo "Output is located at    
    output_for_free_disk_space.csv"

  Wscript.echo "Error log is located at   
    error_output_for_free_disk_space.csv"

'Cleanup
Set objInputFSO=Nothing
Set objOutputFSO=Nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top