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

VBScript newbie question

Status
Not open for further replies.

irkey

MIS
Sep 23, 2002
14
0
0
CA
See the code below....

I cannot figure out why the If statement is not executing properly. I think I have the syntax correct but I do not get the wscript.echo portion of the if statement to work.

I have tried to trim the leading and trailing spaces also, but I cannot figure out what I'm doing wrong. Help!!!


Option Explicit
Dim strComputer, objWMIService, colSettings, objComputer, compModel
Dim DellGX150, DellC600

DellGX150 = "OptiPlex GX150"
DellC600 = "Latitude C600"

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings
Wscript.Echo objComputer.Model
compModel = objComputer.Model
Next

If compModel = "DellGX150" then
Wscript.Echo "Computer is " & compModel
elseif compModel = DellC600 then
Wscript.Echo "Computer is " & compModel
End If
 
when you write out objComputer.Model does the case match exactly for DellGX150??

Regards
Steve Friday
 
I have just tested your script on my machine and it works fine.

As long as you get the case right.

Regards
Steve Friday
 
I have just seen that you have DellGX150 set as a variable, but in your if statement you have "DellGX150" should this not be If compModel = DellGX150 then



Regards
Steve Friday
 
Sorry, my original script I was testing a few scenarios. But you are correct it should be a variable (no quotes). You said you got this script to work??? I still cannot get it to work as I expect. Case matches as far as I can tell....that is why I echo the results for objComputer.Model first to see what the result should be.

Is there maybe a better way to this???
 
I can convert it to all lowercase and I still cannot get it to work correctly. Help!!!!! It should be staight forward but alas....NOT!
 
Run your script with the debug option (//x) step thru and check the value of compModel, in the Locals window. It looks like .Model returns a string value that contains 31 chars (approx.) so your string is padded with spaces...

BTW, you could use
Code:
Set colSettings = GetObject("winmgmts:").InstancesOf("Win32_ComputerSystem")
in place of
Code:
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")

~ jmcallister
 
Thank you jmcallister! Once I trim-ed the spaces, the script works as expected. Thank you very much and thanks for the little debugging trick, I didn't know that was available.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top