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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function wont return "True" or "False" properly 1

Status
Not open for further replies.

gmoney1011

Technical User
Apr 30, 2012
4
US
I have a simple vbscript where I use WMI to pull various server information. I wrote a function to determine if the server is a virtual machine or not. When I call the function I receive "runtime error: Object required", it appears that my function is not exiting properly but I cant figure it out. Im fairly new to vbscript, so any help is appreciated. Ive wrote the script to run on servers so Im not sure how it would behave on a workstation.



Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_SystemEnclosure", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
If IsVM Then
WScript.Echo "Server Type: Virtual"
WScript.Echo "Service Tag: N/A"
Else
WScript.Echo "Server Type: Physical"
WScript.Echo "SerialNumber: " & objItem.SerialNumber *****I receive the error on this line*****
End If
Next



'**********Function to determine Virtual Machine**********
Function IsVM ()
Dim isvmItems
Set isvmItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In isvmItems
If objItem.Manufacturer = "VMware, Inc." Then
IsVM = "True"
Else
IsVM = "False"
End If
Next
End Function

If I need to post the enitre code or give more information please let me know.
 
I don't see any problem in the code that would cause an "Object required" error, though I can't see where you are defining objWMIService, nor what values the wbemFlagReturnImmediately and wbemFlagForwardOnly flags have. You may need to post more of the code.
 
Here is the entire code

JustinEzequiel, Ive been running it without the quotes as well. I was trying different things in an attempt to find out what the issue is.

Option Explicit
'On Error Resume Next

Dim wmiRoot
Dim WshShell
Dim objWMIService
Dim colItems
Dim objItem
Dim strComputerName
Dim dellAssestTag
Dim Suffix
Dim vmCores 'Used to count number of cores for virtual


Const wbemFlagReturnImmediately = &h10 'Constant for using Win32_OperatingSystem class
Const wbemFlagForwardOnly = &h20 'Constant for using Win32_OperatingSystem, class
Const IEVersion = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Version"


wmiRoot = "winmgmts:\\.\root\cimv2" 'Assigns WMI root to a variable

Set objWMIService = GetObject(wmiRoot)
Set WshShell = WScript.CreateObject("WScript.Shell")





'Pulls Computer Name

Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
For Each objItem in colItems
strComputerName = objItem.Name
WScript.Echo "Computer Name: " & strComputerName
Next

'Ends Pull Computer Name



'Pulls Operating System and Service Pack Version

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
WScript.Echo "Operating System: " & objItem.Caption
WScript.Echo "ServicePack: SP " & objItem.ServicePackMajorVersion

Next

'End Pulls Operating System and Service Pack Version





'Pulls Dell Assest Tag and Server Type


Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_SystemEnclosure", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
If IsVM Then
WScript.Echo "Server Type: Virtual"
WScript.Echo "Service Tag: N/A"
Else
WScript.Echo "Server Type: Physical"
WScript.Echo "SerialNumber: " & objItem.SerialNumber
End If
Next

'End Pull Dell Assest Tag and Server Type



'Pull CPU Speed and Number of cores

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
WScript.Echo "NumberOfCores: " & objItem.NumberOfCores
WScript.Echo "CPU Speed: " & Round(objItem.CurrentClockSpeed / 1000, 2) & " GHz"

Next


'End Pull CPU Speed and Number of cores


'Allocated RAM
Set colItems = objWMIService.ExecQuery( "Select * FROM Win32_ComputerSystem")
For Each objItem in colItems
WScript.Echo "RAM: " & ConvertBytes(objItem.TotalPhysicalMemory)
Next


'Network Adapters
Dim NumNetworkPorts


Set colItems = objWMIService.ExecQuery("Select * FROM Win32_NetworkAdapter WHERE PhysicalAdapter=True")
For Each objItem In colItems
NumNetworkPorts=NumNetworkPorts + 1
Next


WScript.Echo "Number of NetworksPorts: " & NumNetworkPorts




'IE Version
WScript.Echo "IE Version: Version " & Left(((WshShell.RegRead (IEVersion))),3)




'Number of Hard Drives
Dim HardDrives

Set colItems = objWMIService.ExecQuery("Select * FROM Win32_LogicalDisk WHERE DriveType=3")
For Each objItem In colItems
HardDrives=HardDrives + 1
Next

WScript.Echo "Number of Hard Drives: " & HardDrives



'C Drive usage
Set colItems = objWMIService.ExecQuery("Select * FROM Win32_LogicalDisk WHERE DeviceID='C:'")
For Each objItem in colItems
WScript.Echo "C Drive: " & ConvertBytes(objItem.Size - objItem.FreeSpace) & " free out of " & ConvertBytes(objItem.Size) &_
" total"
Next


'Socket Type
'Set colItems = objWMIService.ExecQuery("Select * FROM Win32_Processor")
'For Each objItem In colItems
' WScript.Echo"Socket Type: " & objItem.SocketDesignation
'Next




'IP Address
Set colItems = objWMIService.ExecQuery("Select * FROM Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objItem in colItems
WScript.Echo"IP Address: " & objItem.IPAddress(0)
Next




'PageFile Settings
Set colItems = objWMIService.ExecQuery("Select * FROM Win32_PageFileUsage")
For Each objItem in colItems
WScript.Echo "PageFile: Allocated - " & ConvertMB(objItem.AllocatedBaseSize) & " Usage - " &_
ConvertMB(objItem.CurrentUsage)
Next



'**********Function that convert bytes up to Terabytes**********
Function ConvertBytes(Size)
Do While InStr(Size,",") 'Remove commas from size
CommaLocate = InStr(Size,",")
Size = Mid(Size,1,CommaLocate - 1) & _
Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate)
Loop

Suffix = " Bytes"
If Size >= 1024 Then suffix = " KB"
If Size >= 1048576 Then suffix = " MB"
If Size >= 1073741824 Then suffix = " GB"
If Size >= 1099511627776 Then suffix = " TB"

Select Case Suffix
Case " KB" Size = Round(Size / 1024, 2)
Case " MB" Size = Round(Size / 1048576, 2)
Case " GB" Size = Round(Size / 1073741824, 2)
Case " TB" Size = Round(Size / 1099511627776, 2)
End Select

ConvertBytes = Size & Suffix
End Function


'**********Function that convert Megabytes up to Exabytes**********
Function ConvertMB(Size)
Size = CSng(Replace(Size,",",""))

If Not VarType(Size) = vbSingle Then
ConvertMB = "SIZE INPUT ERROR"
Exit Function
End If

Suffix = " MB"
If Size >= 1024 Then suffix = " GB"
If Size >= 1048576 Then suffix = " TB"
If Size >= 1073741824 Then suffix = " PB"
If Size >= 1099511627776 Then suffix = " EB"

Select Case Suffix
Case " GB" Size = Round(Size / 1024, 2)
Case " TB" Size = Round(Size / 1048576, 2)
Case " PB" Size = Round(Size / 1073741824, 2)
Case " EB" Size = Round(Size / 1099511627776, 2)
End Select

ConvertMB = Size & Suffix
End Function



'**********Function to determine Virtual Machine**********
Function IsVM ()
Dim isvmItems
Set isvmItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In isvmItems
If objItem.Manufacturer = "VMware, Inc." Then
IsVM = True
Else
IsVM = False
End If
Next
End Function
 
Code:
For Each objItem In colItems
    If IsVM Then
From this bit of code, it appears that you want to know if objItem is virtual. However, your IsVM function is completely independent of objItem. IsVM grabs a collection and iterates through the entire collection; the return value depends only on the last item in the collection.

The community additions on this page: indicate that a chassis type of 0 indicates it is virtual. Perhaps the following (untested) code would work:

Code:
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_SystemEnclosure", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
    If [highlight]objItem.ChassisType = 0[/highlight] Then
        WScript.Echo "Server Type: Virtual"
        WScript.Echo "Service Tag: N/A"
    Else
        WScript.Echo "Server Type: Physical"
        WScript.Echo "SerialNumber: " & objItem.SerialNumber
    End If
Next
 
When you say the IsVM function is completely independent of objItem, isn't objItem a global variable as it defined at the beginning of the script?
 
isn't objItem a global variable as it defined at the beginning of the script?
Yes, and I believe that is the root cause of your problem.

Code:
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_SystemEnclosure", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
    If IsVM Then...
objItem starts out as an item from Win32_SystemEnclosure, but the function IsVM overwrites it with an item from Win32_ComputerSystem. You then ask the serial number of an object that doesn't have one.
 
Code:
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_SystemEnclosure", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each [COLOR=blue]objItem1[/color] In colItems
    If IsVM Then
        WScript.Echo "Server Type: Virtual"
        WScript.Echo "Service Tag: N/A"
    Else
        WScript.Echo "Server Type: Physical"
        WScript.Echo "SerialNumber: " & objItem.SerialNumber *****I receive the error on this line*****
    End If
Next



'**********Function to determine Virtual Machine**********
Function IsVM () 
Dim isvmItems    
Set isvmItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", _
                                        wbemFlagReturnImmediately + wbemFlagForwardOnly)
             
    For Each [COLOR=orange]objItem2[/color] In isvmItems
        If [COLOR=orange]objItem2[/color].Manufacturer = "VMware, Inc." Then
            IsVM = "True"
        Else
            IsVM = "False"
        End If
    Next
End Function

When I said it is independent, I meant the value that IsVM returns has nothing to do with the current value of objItem1.
 
jges said:
objItem starts out as an item from Win32_SystemEnclosure, but the function IsVM overwrites it with an item from Win32_ComputerSystem. You then ask the serial number of an object that doesn't have one.


jges said:
When I said it is independent, I meant the value that IsVM returns has nothing to do with the current value of objItem1.

Ok, I understand now! Ive been learning on the fly and you helped me learn something else. Thanks alot, I really appreciate it! The script is working now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top