Solved

Turnkey VB Script Template Ideas and Suggestions

Posted on 2008-10-31
8
222 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

816 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now