Solved

VBScript If INSTR Using A List of Values to Check

Posted on 2010-11-09
7
1,090 Views
Last Modified: 2012-05-10
I'm running a report that lists all Active Directory Accounts and lists the following: User ID, Full Name, Description, Email Address, and Employee Type. The goal being to list all but service type accounts.

I've noticed some discrepancies where some accounts are listed as Generic, but they are actual user accounts. I can clean these up easily enough with another script, but my main concern is giving someone this report with everything already removed regardless of whether these discrepancies exist or not. The easiest way is skip files that have certain strings in the Description or User ID.

So if I use the following code:
If InStr(objUser.Description, "Workstation") Then objRecordSet.MoveNext

Is there a way to check for a list of strings, and most importantly, to see if a numeric value exists without a ridiculously long If, Then, Else If statement?

Thanks!!
0
Comment
Question by:JB4375
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 2

Expert Comment

by:jeffmowens
ID: 34095292
This will look for the Description in the comma-delimited list of objects you want to skip. Note that both portions of the comparision need to be wrapped in commas as exampled below:

If InStr(1, ","&objUser.Description&","   ,  ",Workstation,Computer,Group," ) > 0 Then
  ' MoveNext
  ...
0
 
LVL 1

Author Comment

by:JB4375
ID: 34096230
OK...
Remarkably simple. Is there any limit to the number of items in the comma-delimited list?
Thanks again!!
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 300 total points
ID: 34097847
Hi,

To my knowledge, that doesn't work.  InStr will not treat ",Workstation,Computer,Group," as a comma separated list to look for in objUser.Description.  That will match only when the full string of ",Workstation,Computer,Group," is actually in the description.

What you might need to do is create a dictionary of excluded words or phrases and check if any of them exist in any attribute you're after.

Regards,

Rob.
Set objExcludes = CreateObject("Scripting.Dictionary")
objExcludes.Add "Workstation", 0
objExcludes.Add "Computer Model", 0
objExcludes.Add "Group", 0

While Not objRS.EOF
	bSkip = False
	For Each strExclude In objExcludes
		If InStr(LCase(objUser.Description), LCase(strExclude)) > 0 Then
			bSkip = True
			Exit For
		End If
	Next
	If bSkip = False Then
		'....add the record to the file, otherwise if bSkip is true, this block will not be executed
	End If
	objRS.MoveNext
Wend

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Assisted Solution

by:jeffmowens
jeffmowens earned 200 total points
ID: 34098455
Yes, it will work; and I don't think there is a string length limit.  

However, after looking at my example again, I see I have the order of the string parameters reversed. It should be:

InStr([Start Position] , [String To Search] , [String To Find])

thus;

If InStr( 1, ",Workstation,Computer,Group," , ","&objUser.Description&"," ) > 0 Then

...would look for the Description inside of the list.


0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34098726
Oh right.  That makes more sense.  So if the Description value was either of the following:
Workstation
Computer
Group

it would work.  This does match the *entire* description though, so if the description were My Workstation, it would not find a match.  This would probably work for the author.

Thanks for clarifying.

Rob.
0
 
LVL 1

Author Closing Comment

by:JB4375
ID: 34101781
Thanks guys!!

Rob I liked you approach because it seems easier to included a list of items, without dealing with all the syntaxt, AND it also deals with handling case. Something I hadn't even considered.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34106192
Sure, no problem. Thanks for the grade.

Regards,

Rob.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …

749 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