Link to home
Start Free TrialLog in
Avatar of JB4375
JB4375Flag for United States of America

asked on

Turnkey VB Script Template Ideas and Suggestions

I recently asked how to query an Active Directory Group and an OU, and then list the common members: https://www.experts-exchange.com/questions/23863991/VB-Script-lists-members-of-OU-Need-it-to-query-Group-as-well-List-common-members-of-both-the-group-and-OU.html

I got a great solution, and what I found really intriguing was that fact that the solution was turn key. I didn't have to edit the query to put in my domain or deal with the sytax. While reading the Scripting Guy articles they mentioned that some of the code, such as Creating a connection to Active Directory using the Active Directory ADO (ActiveX Data Objects) provider is pure boilerplate and could be used anywhere as well:

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

That got me to thinking about the methods expert scripters use to save time such as a template that would have the most common lines of code already present. From there they could pull out what they didn't need, and add in everything else.

I've seen it in web page design with HTML, Body, Content Here, /Body, /HTML, etc. but I was really interested in something a little more extensive than just comment outlines. Suggestions?
SOLUTION
Avatar of Shift-3
Shift-3
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JB4375

ASKER

I agree.. right now I do have a fairly sizeable collection, from the previous scripter, that I've used and modified.
I was just wondering how much, such as what I've shown in the above examples, could be universal? As opposed to: use this for group query, use this for printer query, Use this for .... etc.
Of course if someone has gotten really creative, I would really enjoy seeing it.
JB
Avatar of RobSampson
Hi, if you're after something like an #include statement used on PHP or other technologies, you can try the ExecuteGlobal method:
http://msdn.microsoft.com/en-us/library/342311f1(VS.85).aspx

which you'd use by basically reading in a text file that has your common function(s), using the FileSystemObject, then executing that code.  It's still not the best solution, particularly for portability, as you'd need to port your functions script with it all the time.

In all the scripts I write, I just use good old Copy and Paste for the common tasks.  Every requirement is always slightly different, so it has to be modified anyway......

When you've got a situation where you've got one script that does multiple tasks, such as
query a group, query users, etc
then the code you've posted in the question is universal in a sense, and only needs to be included once.

See the attached code for another example.
You can see the Common Section, where there's "re-usable" code, and then two Custom Sections, that have different requirements, meaning cut-and-paste doesn't really fit, but for the common section, it certainly does.

Hope that helps.

Regards,

Rob.


'====== COMMON SECTION ==========
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
'================================
 
'====== CUSTOM SECTION 1 ========
Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where Local = TRUE")
 
For Each objPrinter In colPrinters
    WScript.Echo objPrinter.Caption
End If
'================================
 
'====== CUSTOM SECTION 2 ========
Set colDrives = objWMIService.ExecQuery _
    ("Select * From Win32_LogicalDisk Where DriveType = 3")
 
For Each objPrinter In colPrinters
    WScript.Echo objPrinter.Caption
End If
'================================

Open in new window

Avatar of JB4375

ASKER

Shift-3: Great Link. I'd seen one or two of these, but I hadn't seen all of them.
Rob: Good link as well, and I get the concept of the include statement, but you lost me on the included code. Could you explain it by throwing in some comments?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JB4375

ASKER

Great information. Thanks Guys!
Avatar of JB4375

ASKER

Thanks Rob. it's more clear to me now. Thanks for the info and links guys!!