Solved

Turnkey VB Script Template Ideas and Suggestions

Posted on 2008-10-31
8
223 Views
Last Modified: 2013-12-24
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?
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
  • 4
  • 3
8 Comments
 
LVL 38

Assisted Solution

by:Shift-3
Shift-3 earned 150 total points
ID: 22853569
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
 
LVL 1

Author Comment

by:JB4375
ID: 22853658
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 22854848
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 1

Author Comment

by:JB4375
ID: 22855330
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
 
LVL 65

Accepted Solution

by:
RobSampson earned 350 total points
ID: 22855934
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
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 350 total points
ID: 22855966
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
 
LVL 1

Author Closing Comment

by:JB4375
ID: 31512168
Great information. Thanks Guys!
0
 
LVL 1

Author Comment

by:JB4375
ID: 22856860
Thanks Rob. it's more clear to me now. Thanks for the info and links guys!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

This article outlines the process to identify and resolve account lockout in an Active Directory environment.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 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