ActiveX component can't create object: 'GetObject'

ActiveX component can't create object: 'GetObject'

Post by Jason Benw » Thu, 14 Oct 2004 01:53:28


This seems to fail when the machine is not online. I have a function to ping
the machine and return true or false. But it doesn't seem to be working.
Will someone please take a look:
thank you

'
============================================================================
===============
'
' Script Information
'
' Title: Bios Information
' Author: Jason Benway
' Description: list computer name,model,and bios version. Must pass txt file
containing list
' computer names. Output is written to c:\biosversions.txt
'
'
'
============================================================================
===============

Set objargs = WScript.Arguments
Const forreading = 1
Set acomputers = CreateObject("Scripting.Dictionary")
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objtextfile = objfso.OpenTextFile(objargs(0), forreading)
Set outfso = CreateObject("Scripting.FileSystemObject")
Set outtextfile = outfso.CreateTextFile("c:\biosversions.txt", True)

' Read the file which contains a list of machines, on per line
i = 0
Do While objtextfile.AtEndOfStream <> True
strnextline = objtextfile.ReadLine
acomputers.add i, strnextline
i = i + 1
Loop


For Each sComputer In aComputers
dcomputer = acomputers.Item(scomputer)

' check if computer is online
w = 0

' suppress errors
'On Error Resume Next
dcomputer = Replace(dcomputer, vbtab, "")
If IsConnectible(dcomputer,2,300) = True Then

Set oWMI = GetObject("winmgmts:\\" & dComputer & "\root\cimv2")
If err = 0 Then
For Each wmiobj In oWMI.InstancesOf("Win32_ComputerSystem")
soutput = wmiObj.Caption & vbtab & wmiobj.model
Next
End If
Set aoWMI = GetObject("winmgmts:\\" & dComputer & "\root\cimv2")
If err = 0 Then
For Each awmiobj In aoWMI.InstancesOf("Win32_BIOS")
soutput = soutput & vbtab & awmiObj.SMBIOSBIOSVersion
Next
End If

If err = 0 Then
WScript.Echo soutput
outtextfile.WriteLine soutput
End If
End If
Next


Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' Works an "all" WSH versions
' sHost is a hostname or IP

' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used

If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750

Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "\runresult.tmp"

oShell.Run "%comspec% /c ping -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True

Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsDefault)

sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)

Select Case InStr(sResults,"TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select
End Function
 
 
 

ActiveX component can't create object: 'GetObject'

Post by MV » Thu, 14 Oct 2004 02:32:03


Hi

I tested your code (removing the WMI part only), and only online
computers that responded on ping fulfilled your If test:

If IsConnectible(dcomputer,2,300) = True Then
...
End If

For offline computers I did not get inside that If test.

Note that even if you can ping a computer, the WMI connect can fail
for various reasons, so you should enable your "On Error Resume Next"
line again (it is commented out in your code).

I also see that you recreate a couple of objects, this is not
necessary, and for the WMI object you create twice, it will give
you an execution time penalty.

Instead of

Set objfso = CreateObject("Scripting.FileSystemObject")
Set objtextfile = objfso.OpenTextFile("c:\2\fff.txt", forreading)
Set outfso = CreateObject("Scripting.FileSystemObject")
Set outtextfile = outfso.CreateTextFile("c:\biosversions.txt", True)

I suggest you use

Set objfso = CreateObject("Scripting.FileSystemObject")
Set objtextfile = objfso.OpenTextFile("c:\2\fff.txt", forreading)
Set outtextfile = objfso.CreateTextFile("c:\biosversions.txt", True)



and instead of

Set oWMI = GetObject("winmgmts:\\" & dComputer & "\root\cimv2")
If err = 0 Then
For Each wmiobj In oWMI.InstancesOf("Win32_ComputerSystem")
soutput = wmiObj.Caption & vbtab & wmiobj.model
Next
End If
Set aoWMI = GetObject("winmgmts:\\" & dComputer & "\root\cimv2")
If err = 0 Then
For Each awmiobj In aoWMI.InstancesOf("Win32_BIOS")
soutput = soutput & vbtab & awmiObj.SMBIOSBIOSVersion
Next
End If

I suggest you use

Set oWMI = GetObject("winmgmts:\\" & dComputer & "\root\cimv2")
If err = 0 Then
For Each wmiobj In oWMI.InstancesOf("Win32_ComputerSystem")
soutput = wmiObj.Caption & vbtab & wmiobj.model
Next
For Each awmiobj In oWMI.InstancesOf("Win32_BIOS")
soutput = soutput & vbtab & awmiObj.SMBIOSBIOSVersion
Next
End If






--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.yqcomputer.com/

 
 
 

ActiveX component can't create object: 'GetObject'

Post by Jason Benw » Thu, 14 Oct 2004 03:31:26

Thank you.
I uncommented the "on Error Resume Next"

and changed the code to make less calls.

Its working now. Thank you for your help, I'm just starting to gett into VBS
script for small admin tasks.

Could I some how capture the error and handle it cleanly without using "on
Error Resume Next" ?

thanks,jb
 
 
 

ActiveX component can't create object: 'GetObject'

Post by MV » Thu, 14 Oct 2004 04:00:25


Hi

Nope.

Note that you should never use "On Error Resume Next" unless you
*really* need it (and then you should turn it off again at first
chance with "On Error Goto 0").



--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.yqcomputer.com/