Scripting Central--How to Get Started Writing Scripts--June 2, 2006

If you're new to scripting, check out the list of resources recommended by the scripting experts. If you're not new to scripting, check out the GetSIDFromUser function.

ITPro Today

June 4, 2006

11 Min Read
ITPro Today logo in a gray background | ITPro Today

1. Perspectives

  • How to Get Started Writing Scripts

2. This & That

  • Calling All Windows IT Pro Innovators!

3. Script Watch

  • Let a Service Do All the Running for You

4. Scripter's Toolkit

  • The GetSIDFromUser Function

5. Featured White Paper

  • Managing Mobility in the Enterprise

6. Announcements

  • June Exclusive: Save $80 off Windows Scripting Solutions

  • Memorial Day Special--Save 58% off Windows IT Pro

7. Events and Resources

  • Leveraging Centralized Event Log Monitoring

  • Win a New iPod (for Mac or PC)

  • Does Your Antivirus Software REALLY Protect You Against Spyware?

  • Implement Real-Time Processes

  • Disaster Recovery Lessons Learned from Katrina and Rita

1. Perspectives
by Karen Bemowski, [email protected]

How to Get Started Writing Scripts
A few weeks ago, I received an email from Randy Culbertson, who noted that he has the desire and time to learn scripting but didn't know where to start. That prompted me to contact several scripting experts so that I could compile a list of resources that Randy and others could use to learn scripting in the Windows environment.

To begin, it helps to know the basic concepts behind scripting. The article "An Introduction to Scripting Technologies and Object Models" (InstantDoc ID 8599) provides an easy-to-understand overview of scripting.

Before I go any further, let me mention two important notes:

  1. I'll be referencing Windows IT Pro, Microsoft, and third-party resources. All the Windows IT Pro articles will have an InstantDoc ID after them. To access these articles, simply go to http://www.windowsitpro.com, enter the specified number in the InstantDoc ID box, and click Go. Normally, most of these articles are available only to subscribers. However, for about a month (from June 2 to July 6), the Windows IT Pro articles I mention in this column will be available for public viewing. Come July 7, they'll be locked down again, so don't put off looking at them.

  2. Some of the articles and books I reference are older. However, even though they may be slightly outdated, they're still good references for learning scripting. To borrow an old cliche, they're "oldies but goodies."

One point that the article "An Introduction to Scripting Technologies and Object Models" brings up is that there are different scripting languages. Because many scripting languages exist, I decided to narrow the list. In this column, I'll cover the languages of Windows shell scripting and VBScript. For those of you who aren't new to scripting but maybe new to the Windows PowerShell (formerly code-named Monad), I'm including some resources for learning this new command-line shell's scripting language, which is called Powerscript. Next month, I'll discuss the resources for learning JScript, Perl, and T-SQL.

Which languages you should learn depends on the systems you work with. If you work with Windows, I suggest you learn shell scripting and VBScript. If you work with Microsoft SQL Server, you should probably learn shell scripting and T-SQL. Notice that I specified two languages. The more you learn, the better.

Shell Scripting
The scripting language that the Windows command processor (cmd.exe) uses is often referred to as Windows shell scripting. The 10-part series "Shell Scripting 101" is an excellent tutorial for people who want to learn to write .bat and .cmd scripts. (If you're wondering about the difference between .bat and .cmd scripts, read Lesson 2.) This series walks you the basics and even includes practical exercises you can try on your own. Here are the links to this series:

  • "Shell Scripting 101, Lesson 1" (InstantDoc ID 16355)

  • "Shell Scripting 101, Lesson 2" (InstantDoc ID 19840)

  • "Shell Scripting 101, Lesson 3" (InstantDoc ID 20142)

  • "Shell Scripting 101, Lesson 4" (InstantDoc ID 20530)

  • "Shell Scripting 101, Lesson 5" (InstantDoc ID 20781)

  • "Shell Scripting 101, Lesson 6" (InstantDoc ID 21310)

  • "Shell Scripting 101, Lesson 7" (InstantDoc ID 21630)

  • "Shell Scripting 101, Lesson 8" (InstantDoc ID 21984)

  • "Shell Scripting 101, Lesson 9" (InstantDoc ID 22289)

  • "Shell Scripting 101, Lesson 10" (InstantDoc ID 22582)

After you go through that series, you can check out the following articles:

  • "Getting Started in NT Shell Scripting, Part 1" (InstantDoc ID 8182)

  • "Getting Started in NT Shell Scripting, Part 2" (InstantDoc ID 8384)

  • "The FAQs About Setting Up Your Shell Scripting Environment" (InstantDoc ID 49676)

One book that several scripting experts recommended is Tim Hill's book Windows NT Shell Scripting (Macmillan Technical Publishing, 1998).

VBScript
VBScript is a scripting language associated with Windows Script Host. WSH is an environment for executing scripts. I like to think of WSH as a house. A house contains people and objects (e.g., furniture, appliances). In the WSH house, the scripting languages are the people. Two people permanently reside in that house: VBScript and JScript. And just like any house, there can be visitors. The WSH house visitors include Perl and Python. Whether the people reside in the house or are visitors, they can use the objects in that house. In the WSH house, the people can use objects such as the FileSystemObject object or the WScript object.

Good resources to learn about WSH include

  • "WSH, Part 1: File Types" (InstantDoc ID 48498)

  • "WSH, Part 2: .wsf Files" (InstantDoc ID 48692)

  • "WSH, Part 3: Windows Script Components" (InstantDoc ID 49092)

  • "Scripting Solutions: Scripting 101" (InstantDoc ID 5410)

  • "Scripting Solutions: Scripting 101, Part 2" (InstantDoc ID 5505)

  • "Scripting Solutions: Scripting 101, Part 3" (InstantDoc ID 5683)

  • "Scripting Solutions: Scripting 101, Part 4" (InstantDoc ID 7112)

  • "An Introduction to WSH" (InstantDoc ID 4608)

  • Tim Hill's Windows Script Host, third edition (New Riders Publishing, 2003)

  • Windows 2000 Scripting Guide (Microsoft Press, 2003). You can find parts of the guide available on the Microsoft Developer Network (MSDN) at http://www.microsoft.com/technet/scriptcenter/guide.

Like the "Shell Scripting 101" series, the "Understanding VBScript" series is a tutorial that you can use to learn VBScript. The following articles cover the basics:

  • "Understanding VBScript" (InstantDoc ID 5455)

  • "Understanding VBScript: Arrays" (InstantDoc ID 5627)

  • "Understanding VBScript: Operators" (InstantDoc ID 5881)

  • "Understanding VBScript: Statements" (InstantDoc ID 6125)

  • "Understanding VBScript: Built-In and User-Defined Functions" (InstantDoc ID 6185)

  • "Understanding VBScript: Functions to Manipulate Strings" (InstantDoc ID 7460)

  • "Understanding VBScript: Object Management" (InstantDoc ID 7667)

  • "Understanding VBScript: Manipulating Files with FileSystemObject" (InstantDoc ID 7825)

  • "Understanding VBScript: The Drive Object" (InstantDoc ID 7992)

  • "Understanding VBScript: The Folder Object" (InstantDoc ID 8184)

  • "Understanding VBScript: Working with the File Object" (InstantDoc ID 8389)

  • "Understanding VBScript: The TextStream Object" (InstantDoc ID 8603)

  • "Understanding VBScript: The Dictionary Object--An Alternative to Array" (InstantDoc ID 8797)

  • "Understanding VBScript: Runtime Code Evaluation" (InstantDoc ID 8979)

  • "Understanding VBScript: Using Regular Expressions" (InstantDoc ID 9170)

  • "Understanding VBScript: Real-World Uses of Regular Expressions" (InstantDoc ID 9800)

Powerscript
Although Windows PowerShell is still a release candidate, you can start learning about this new command-line shell and its scripting language. There's already been at least one book published: Andy Oakley's Monad Jumpstart (O'Reilly, 2005). Another book Windows PowerShell in Action is tentatively scheduled for publication in November by Manning Publications. Bruce Payette, a member of the Windows PowerShell team, is writing this book. You can read an unedited chapter at http://www.manning.com/payette.

Besides these books, there are some other types of resources available. Microsoft's Channel9 has some good basic information about PowerShell. The Windows Power Shell Wiki (http://channel9.msdn.com/wiki/default.aspx/Channel9.WindowsPowerShellWiki) includes download information and a link to an FAQ Web page. The Microsoft TechNet Script Center offers the "Scripting with Windows PowerShell" Web site (http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx), which includes links to articles, Web casts, and scripts.

Other useful resources are newsgroups and blogs. You can find the microsoft.public.windows.powershell newsgroup on news.microsoft.com. If you don't use a standard newsreader, you can use the Web interface at http://support.microsoft.com/newsgroups/newsReader.aspx?dg=microsoft.public.windows.powershell. PowerShell blogs include the Windows PowerShell blog at http://blogs.msdn.com/powershell and the PowerShelled blog at http://mow001.blogspot.com.

This list of shell scripting, VBScript, and Powerscript resources isn't meant to be all-inclusive. It just contains resources that are known to be helpful. If you have come across any other resources that are helpful, please let me know and I'll add them to the list.

2. This & That

Calling All Windows IT Pro Innovators!
Have you developed a solution that uses Windows technology to solve a business problem in an innovative way? Enter your solution in the 2006 Windows IT Pro Innovators Contest! Grand-prize winners will receive airfare and a conference pass to Windows and Exchange Connections in Las Vegas, November 6-9, 2006, plus more great prizes and a feature article about the winning solutions in the December 2006 issue of Windows IT Pro. Contest runs through August 1, 2006. To enter, click here:
http://www.windowsitpro.com/AWARDS/innovators_2006.cfm

3. Script Watch

Let a Service Do All the Running for You
If you have a script that needs to run continuously or if you need to know immediately when a script fails, consider running your script as a service. Dave Gest has created an HTML Application (HTA) that makes it easy to install a script as a service. Learn about this HTA in the article "Run Your Scripts as Services," which will be featured in the June issue of Windows Scripting Solutions.

4. Scripter's Toolkit

The GetSIDFromUser Function

Sometimes it's useful to be able to obtain a user's SID from that person's username. The GetSIDFromUser function lets you do just that. It uses Windows Management Instrumentation's (WMI's) Win32_UserAccount class to retrieve an Account's SID. Here's the code for this user-defined VBScript function:

   Function GetSIDFromUser(UserName)      Dim DomainName, Result, WMIUser      If InStr(UserName, "") > 0 Then         DomainName = Mid(UserName, 1, InStr(UserName, "") - 1)         UserName = Mid(UserName, InStr(UserName, "") + 1)      Else         DomainName = CreateObject("WScript.Network").UserDomain      End If      On Error Resume Next      Set WMIUser = GetObject("winmgmts:{impersonationlevel=impersonate}!" _         & "/root/cimv2:Win32_UserAccount.Domain='" & DomainName & "'" _         & ",Name='" & UserName & "'")      If Err = 0 Then Result = WMIUser.SID Else Result = ""      On Error GoTo 0      GetSIDFromUser = Result   End Function

You call the function with the code

   GetSIDFromUser(username)

where username is the name of user for which you want the SID. The username parameter can include a domain name (e.g., domainusername). If you don't specify a domain name, the current logon domain is assumed. Thanks to Bill Stewart for writing this function!

5. Featured White Paper

Managing Mobility in the Enterprise
Identify the appropriate tools to help you manage your mobile workforce effectively, avoid increases in TCO, and more.
http://www.windowsitpro.com/go/whitepapers/ianywhere/enterprisemgmt?code=0531featwp

6. Announcements

June Exclusive: Save $80 off Windows Scripting Solutions
For a limited time, order the Windows Scripting Solutions newsletter and SAVE up to $80 off the cover price! You'll get 12 helpful issues loaded with expert-reviewed code and scripting techniques, as well as hundreds of tips on automating repetitive tasks. You'll also get FREE, unlimited access to the full online scripting article library (more than 500 articles). Subscribe now:
https://store.pentontech.com/index.cfm?s=1&promocode=eu2665us

Memorial Day Special--Save 58% off Windows IT Pro
Subscribe to Windows IT Pro today and SAVE 58%! Along with your 12 issues, you'll get FREE access to the entire Windows IT Pro online article archive, which houses more than 9,000 helpful articles. This is a limited-time offer, so order now:
https://store.pentontech.com/index.cfm?s=1&promocode=eu2065uw

7. Events and Resources

Leveraging Centralized Event Log Monitoring
Consolidate the Windows Event Log and the Unix Syslog to save money and ensure continuous compliance. Also identify 50 critical events you should be monitoring! Live Web Seminar: Tuesday, June 6
http://www.windowsitpro.com/go/seminars/prism/eventlog/?partnerref=0524emailannc

Win a New iPod (for Mac or PC)
Download a Windows IT Pro podcast on I>Windows IT Pro Radio by your favorite author, editor, or industry figure. You'll automatically be entered to win!
http://www.windowsitpro.com/podcast

Does Your Antivirus Software REALLY Protect You Against Spyware?
Are you depending on antivirus software to protect you against spyware? Learn the key differences between antivirus and antispyware products and protect yourself against a false sense of security. Live Event: Thursday, June 8
http://www.windowsitpro.com/go/seminars/sunbelt/antivirus/?partnerref=0531winevents

Implement Real-Time Processes
Implement real-time processes in your email and data systems--you could also win an iPod Nano!
http://www.windowsitpro.com/go/toolkits/xosoft/disasterrecovery/?code=0531emailannc

Disaster Recovery Lessons Learned from Katrina and Rita
Make sure that your DR systems are up to the challenge of a real natural disaster by learning from messaging survivors of Hurricanes Katrina and Rita. On-demand Web seminar!
http://www.windowsitpro.com/go/seminars/mimosa/drlessons/?partnerref=0531emailannc

Contact Us

This email newsletter is brought to you by Windows IT Pro, the leading publication for IT professionals deploying Windows and related technologies. Subscribe today!
https://store.pentontech.com/index.cfm?s=1&promocode=eu205xfb

View the Windows IT Pro Privacy Policy.
http://www.winnetmag.com/aboutus/index.cfm?action=privacy

Windows IT Pro is a division of Penton Media, Inc. 221 East 29th Street, Loveland, CO 80538, Attention: Customer Service Department

Copyright 2006, Penton Media, Inc. All Rights Reserved.

Corrections to this Article:

  • In the email version of this newsletter, there was an inaccurate InstantDoc ID. The InstantDoc ID for the article "Getting Started in NT Shell Scripting, Part 1" should have been 8182. We apologize for any inconvenience this error might have caused.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like