Expert Solutions: Advanced Event 10 of the 2010 Scripting Games

Expert Solutions: Advanced Event 10 of the 2010 Scripting Games

  • Comments 1
  • Likes

 

Bookmark and Share

 

(Note: These solutions were written for Advanced Event 10.)

Advanced Event 10 (Windows PowerShell)

Photo of Don Jones

Don Jones
http://concentratedtech.com
Windows PowerShell MVP Award Recipient
Co-Author, Windows PowerShell v2: TFM (3rd Edition)
Windows PowerShell Trainer,
http://CBTNuggets.com
Editor-in-Chief, Realtime Publishers, http://nexus.realtimepublishers.com

------------

Because extra points were being awarded for a graphical user interface, I decided to use SAPIEN’s excellent PrimalForms tool. It let me visually construct a form, and then add my script code using its built-in editor. I’m certain I could have built this as a one-liner, but the desire to have a user-friendly dialog and progress indicator drove a more programming-style approach. For example, rather than having the shell move all of the files in a batch—an easy one-liner to write—I chose to enumerate through the files and move each one as a discrete operation. That was the best way to ensure I could display a progress bar, which I did by using the shell’s built-in Write-Progress cmdlet.

Some interesting features: The script disallows the selection of a destination folder within the user’s profile path (that would defeat the purpose). It also checks to see if any of the specified file types exist when it loads; if they don’t, it immediately exits without bothering the user. There’s also some error checking to ensure the user has permission to create the target folder, although by using the Windows folder browser dialog (rather than accepting a manually typed path), I help ensure that the user will be able to create the target folder without issues.

As shown in the following image, the user gets a nice, pretty dialog box to work with. I did need to be careful to go through the checked list box so that I’m only moving the items that the user has left checked, although the dialog starts with all file types checked so that the user is encouraged to move as many files as possible.

Image of nice, pretty dialog box to deal with

One programming trick to pay attention to is the check for Single-Threaded Apartment (STA) mode when the script’s form loads. Some graphical scripts can run without STA mode, but because I elected to use the Windows Folder Browser dialog, I needed to ensure that the script was running under STA mode. That’s the default with the Windows PowerShell ISE, but it isn’t the default with the text console. So if I detect that I’m not in STA mode, I recursively launch the shell in STA mode to run the script. That recursive instance will exit automatically after the script completes, so it basically cleans up after itself.

This script looks complicated, but a lot of the code is generated automatically from PrimalForms. The user-generated code—that’s what I wrote—is less than 90 lines, and that includes comments. Speaking of comments, check out the comment-based help I added to the start of the script. That lets you use the built-in Help command to ask for help on this script, and it will display a properly formatted description of what the script does. It’s a great, standardized way to document what your scripts are doing.

The completed AdvancedPowerShell10.ps1 script has been placed in the Script Repository.

 

Advanced Event 10 (VBScript)

Kai.Bluesky

Title: System Specialist

Credentials: Cisco CCNA, CCNP, CCDA, CCDP, Microsoft MCP, MCSA, MCSE, MCDBA, MCTS, MCITP, Vmware VCP 3, VCP 4 Novell CNA, CLA, ICT CIFI, Redhat RHCT, RHCE, CITRIX CCA XS, CITRIX CCA XenApp…

Here is the list of what the script does. Because it is using VBScript, it may not have a fancy interface, but it will do what you want to do.

1. Gets the current profile location.

2. Asks for agreement to move files.

3. Creates folders such as c:\fso, c:\fso\mp3, and c:\fso\doc, based on user input.

4. Creates log file for writing (prepared for failback).

5. Moves files.

The Move file script is based on a previously published VBScript script from the Script Repository, which recursively moves the file to the parent directory.

In addition, because I do not want to move the system file, I only choose Desktop and My document folders. Finally, if you do not like the change, the second script, revert.vbs, will revert the movement of the files. This is seen in the following image.

Image of revert script in action

The complete Advanced Event 10 VBScript script and the Revert.vbs script are available in the Script Repository.


If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment