Solved

Turnkey VB Script Template Ideas and Suggestions

Posted on 2008-10-31
8
220 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:JB4375
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 65

Accepted Solution

by:
RobSampson earned 350 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Great information. Thanks Guys!
0
 
LVL 1

Author Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Resolve DNS query failed errors for Exchange
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
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…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

728 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

11 Experts available now in Live!

Get 1:1 Help Now