Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Just a quick note to say, "THANKS!" for these forums...The site is very well layed out and easy to use. Thanks for bringing us together - we need each other."

Geography

Where in the world do Tek-Tips members come from?

Function wont return "True" or "False" properlyHelpful Member! 

gmoney1011 (TechnicalUser)
30 Apr 12 9:49
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.
JustinEzequiel (Programmer)
30 Apr 12 10:44
try it without the quotes; with quotes, you're returning strings

 
guitarzan (Programmer)
30 Apr 12 10:56
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.
 
gmoney1011 (TechnicalUser)
30 Apr 12 14:39
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
jges (TechnicalUser)
30 Apr 12 16:37

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: http://msdn.microsoft.com/en-us/library/windows/desktop/aa394474%28v=vs.85%29.aspx
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 objItem.ChassisType = 0 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
gmoney1011 (TechnicalUser)
30 Apr 12 20:31
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?
jges (TechnicalUser)
30 Apr 12 22:31

Quote:

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.
Helpful Member!  jges (TechnicalUser)
30 Apr 12 22:37

CODE

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_SystemEnclosure", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem1 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 objItem2 In isvmItems
        If objItem2.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.  
gmoney1011 (TechnicalUser)
1 May 12 8:29

Quote (jges):

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.


Quote (jges):

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!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close