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: http://www.experts-exchange.com/Software/Server_Software/File_Servers/Active_Directory/Q_23863991.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?
LVL 1
JB4375Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shift-3Commented:
Having a single document containing code for every possible task would probably be too cumbersome.  I find that it's easier to have a library of separate scripts that you can refer to and adapt as needed.

Starting out you can use the Scripting Guy archive this way.  It contains a large number of well-written scripts which accomplish a variety of common tasks.  You can generally do a keyword search and find something close to what you want to do.  Once you've adapted a number of their scripts you can refer back to your own adaptations and add refinements as needed.

 Microsoft has created automated tools for writing certain kinds of scripts.  These can be very helpful in some scenarios.
http://www.microsoft.com/technet/scriptcenter/createit.mspx
0
JB4375Author Commented:
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
0
RobSampsonCommented:
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

0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

JB4375Author Commented:
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?
0
RobSampsonCommented:
OK, I'll try again.  Basically, I'm just illustrating the "common code" within a script, that is the part that can be copied into new scripts, and also the "custom code" within a script, that is that part that makes use of the common code, but needs to be modified for each different task.

Regards,

Rob.
'============= COMMON CODE =================
' This code is transferrable from one script
' to another when you want to perform LDAP
' queries against your Active Directory
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
'===========================================
 
' These two sections of code have differing
' custom parameters that are usually changed
' with each new script. The Common Code Is
' required for these snippets to work
 
'========== CUSTOM CODE 1 ==================
objCommand.CommandText = "SELECT Name FROM '" & strDNSDomain & "' WHERE objectCategory='user'"
Set objRecordSet = objCommand.Execute
Do Until objRecordSet.EOF
    Wscript.Echo objRecordSet.Fields("Name").Value
    objRecordSet.MoveNext
Loop
'===========================================
 
'========== CUSTOM CODE 2 ==================
objCommand.CommandText = "SELECT Name FROM '" & strDNSDomain & "' WHERE objectCategory='group'"
Set objRecordSet = objCommand.Execute
Do Until objRecordSet.EOF
    Wscript.Echo objRecordSet.Fields("Name").Value
    objRecordSet.MoveNext
Loop
'===========================================

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobSampsonCommented:
Oh, and I just remembered.....if you want something of a menu where you can select some common tasks (and a whole lot more not so common tasks), check out VBSEdit, which you can use as your VBScript editor, but it allows you to select tasks from its menu.  Once you select a task, code very similar to the Scripting Guy articles is automatically inserted into your script.

Regards,

Rob.
0
JB4375Author Commented:
Great information. Thanks Guys!
0
JB4375Author Commented:
Thanks Rob. it's more clear to me now. Thanks for the info and links guys!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.