Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Help! VBScript-WMI - Instr() Runtime Error: Type mismatch: '[string: "Microsoft Windows XP"]'

Posted on 2006-05-30
11
Medium Priority
?
1,946 Views
Last Modified: 2008-01-09
I am trying to write a small module to set a value to 1 if the OS is XP.  I keep getting the following error:
delMSjvm.vbs(32, 1) Microsoft VBScript Runtime Error: Type mismatch: '[string: "Microsoft Windows XP"]'


' Begin Get OS Type Module
Dim strComputer, objWMIService, colSettings, objOperatingSystem, strItsType
strComputer = "."

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

Set colSettings = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

Dim strOSname
For Each objOperatingSystem in colSettings
    strOSname = objOperatingSystem.Name
    Wscript.Echo "Operating System Version: " & objOperatingSystem.Name
Next

Wscript.Echo name

Dim strXP, OSTypeResult
strXP = "Microsoft Windows XP Professional"

If InStr(strOSname, strXP, 1) => 1 Then  'Error is produced here
XPOS = 1
End If


0
Comment
Question by:TristinColby
  • 7
  • 3
11 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 16788517
Have u checked to see if strOSname is null/empty?

also, in the code, the instr only checks the last strOSname set from your loop
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16788542
alternative methods to obtain os name
http://www.experts-exchange.com/Programming/Q_21765271.html

0
 
LVL 65

Accepted Solution

by:
rockiroads earned 1000 total points
ID: 16788573
sample code, this is working
note in my instr, I converted case to lowercase, then checked it that way


   Dim OsName, N1, Ver
   strComputer = "."

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set Os = objWMIService.ExecQuery( "SELECT * FROM Win32_OperatingSystem",,48)

  Set OsInfo = objWMIService.ExecQuery( "SELECT * FROM Win32_OperatingSystem",,48)
   For Each objOs in OsInfo
    Ver = "-Build-" & objOs.BuildNumber : N1 = Split(objOs.Name,"|")
    OsName  = "Operating System Name : " & vbCrLf &  N1(0) & Ver
   Next

   if instr(1,lcase(OsName),"windows xp")>0 then
         msgbox "You are running Windows XP"
   else
      MsgBox OsName, 0 + 32, "Computer Information"
   end if
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Expert Comment

by:lojk
ID: 16788626
as you can see in rockiroads comment, his instr command has the correct opening parameters!

InStr(   ByVal Start As Integer,    ByVal String1 As String,    ByVal String2 As String,    Optional ByVal Compare As CompareMethod ) As Integer

You had simply omitted to include the '1' at the start, telling the Instr *where* to start looking. This is a required parameter in VBS and VB before .NET - Hence the type mismatch, its looking for an integer and you are supplying a String...
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16788658
lojk, its really weird I know, I found this out in MS Access VBA, that first parameter is actually optional. I just wonder whether the help pages forgot to omit optional.
You will find that it works without specifying. It certainly threw me when I first found out.

 if instr(lcase(OsName),"windows xp")>0 then

is the same as

 if instr(1, lcase(OsName),"windows xp")>0 then

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16788676
Have u checked to see if strOSname is null/empty?

thats a silly question, your code is displaying it, doh!


anyways, I ran your code and I get data type mismatch, it may have something to do with strOSName, containg characters returned by the call. What I did in my example is split    objOperatingSystem.Name  . I used the pipe as the seperator
0
 
LVL 9

Assisted Solution

by:lojk
lojk earned 1000 total points
ID: 16788768
Rocki... as far as i am aware in pre.NET languages parameter lists cannot be overloaded...

If a parameter is specifed as optional, certainly with VB6, you usually need to add its placeholder to access parameters after it... i.e.

If InStr( , strOSname, strXP, 1) => 1

may have actually worked

VBA is as different to VBS as VB6 is to VBA, just because they start with the same letters, be under no illusion that they are the same language. My speciality is VB6, i have done some VBA and more VBS and had many simliar problems to that above.

If in doubt consult the Lanaguage Documentation for the Langauge in Question, heres the VBscript one

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/ddfa5183-d458-41bc-a489-070296ced968.asp

Rather interstingly, if you check the instr function from here it says....

Arguments
start
Optional. Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. If start contains Null, an error occurs. *The start argument is required if compare is specified.*

string1
Required. String expression being searched.

string2
Required. String expression searched for.

compare
Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values. If omitted, a binary comparison is performed.



So you see, theres the Problem and Answer, because the '1' is on the end it infers that the Compare Pararameter is specified and therefore the Start parameter is required (even if it is omitted)

I usually find its better to always allow all optional Parameters to be specified, you should never make assumptions...
0
 
LVL 1

Author Comment

by:TristinColby
ID: 16788983
Lojk,

Excellent advice. Once I added the 1 to my instr() statement my code work. Wow. 1 number threw my who script off. Thanks again guys.

TristinColby
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16789266
Lojk, I know what you mean and do agree with you in your last post
By habit I have always been used the start pos (1) in my instr


I do wonder though, why this code worked for me without any issues, note I did not place a 1 in my instr


   Dim OsName, N1, Ver
   strComputer = "."

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set Os = objWMIService.ExecQuery( "SELECT * FROM Win32_OperatingSystem",,48)

  Set OsInfo = objWMIService.ExecQuery( "SELECT * FROM Win32_OperatingSystem",,48)
   For Each objOs in OsInfo
    Ver = "-Build-" & objOs.BuildNumber : N1 = Split(objOs.Name,"|")
    OsName  = "Operating System Name : " & vbCrLf &  N1(0) & Ver
   Next

   if instr(lcase(OsName),"windows xp")=>1 then
         msgbox "You are running Windows XP"
   else
      MsgBox OsName, 0 + 32, "Computer Information"
   end if

0
 
LVL 9

Expert Comment

by:lojk
ID: 16789729
Rockiroads,

Your line works because you do it the *other* correct way.
The instr functions requires eithher 'neither', 'first only' or 'both'.  By using lcase (quite correctly) you are standardising the input to the sub and because you omit both first and last values a binary comparison is done on two values that are lower case (i.e. the textcomparison method is not case senstive)

Of course it would be better to have something like this below in a shared code area.

public function ContainsText(TexttoCheck as string, TextToFind as string ) as boolean
on error resume next
if instr(1,TexttoCheck,TextToFind,1)>0 then
ContainsText=true
end if
end function

and do

if ContainsText(OSName,"Windows XP") then
Msgbox "Running Windows XP")
end if

much tidier, i reckons...
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16789776
ok, thanks.
Ive never come across this problem as yet cos I always specify the 1, but nice to know the reason if it ever happens to me or someone else.
Thanks again
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Six Sigma Control Plans

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question