Solved

VBScript If INSTR Using A List of Values to Check

Posted on 2010-11-09
7
1,072 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

775 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