Life of a techno-guru
Friday, September 29, 2006
  Using the ShellExec Object to Your Advantage
Good day everyone! Today, I have for you a short article on using the WshScriptExec object to help you write quality VBscripts while still being able to make calls to external executables. The WshScriptExec object cannot be directly instantiated, but rather is captured when executing a process. By setting a variable equal to the output of the WshShell.Exec method, you can grab an instance of the WshScriptExec object. Once you "get" this object, you are able to manipulate the running process by sending commands to it, or by looking at the console output of the program. Let's run through a quick example of how we can use the WshScriptExec object to make a better script.

Sample Script

Purpose: We will create a sample script to back up some plain-text log files (IIS logs for example), compress them into an archive folder using 7-zip, and then delete the temporary copy of the log files we create. Yes, I know we could back them up directly from the source, but it's always good to have a temporary working copy and clean up afterwards.

Strategy: 1) Using the FileSystemObject (FSO), copy the log files from a network path to the local machine. 2) Use the WshShell.Exec method to execute 7-zip with proper arguments. 3) We will use the StdOut property of the WshScriptExec object to determine when 7-zip has completed. 4) Use existing FSO to remove the (now compressed) log files.

'Create FSO and Shell object
set fso = CreateObject("Scripting.FileSystemObject")
set shell = CreateObject("Wscript.Shell")
'Establish local and remote working directories
remotedir = "\\fileserver01\logs"
localdir = shell.SpecialFolders("MyDocuments") & "\logs\"

'Function to copy log files from remotedir to localdir
function copyFiles()
'Use CopyFile method of FSO to copy files
fso.copyfile remotedir & "\*.log", localdir
end function

'Function to compress files with 7-zip
function compressFiles()
'use WshShell.Exec method to execute 7-zip command. Double-double quotes are there to encapsulate file paths with spaces--IMPORTANT
'Capture return value (the WshScriptExec object we want) in variable
set output = shell.exec "7za a """ & localdir & "archive"" """ & localdir & "*.log"""

'Don't proceed with rest of script (log clean-up) until 7-zip is done executing. 7-zip will normally write "Everything is OK" to the console when completed successfully
while not (instr(output.stdout.readall, "Everything") <> 0)
wscript.sleep 100
end function

function cleanUp()
fso.deletefile localdir & "*.log"
end function

So there you have it. By using the StdOut property, we can test the console output to see if 7-zip executed properly. It's missing a main function to make calls to the other functions, but ... the concept is there, and 7-zip compression is awesome.
Thursday, September 28, 2006
  A little more personal info, and ...
Well, I've mostly recovered from the vehicle accident, and I've got a new car now, a 2006 Hyundai Sonata v6 :-) Finally settled with the insurance company and all ... which took much longer than it should have, but oh well, at least I got my reimbursement and hopefully I won't have to deal with this type of situation again in the near future ... *sigh*

Anyway, you all aren't here to hear about my personal life, I don't think ... so, I figured I would introduce a small utility included with VMware Server called vmware-mount. This utility is great, because it allows you to mount a virtual hard disk image as a partition in another Windows computer! Pretty neat isn't it? Well, here's the situation I've got: I'm installing Windows 2003 on a VM at work, and I need to transfer 2003 SP1 to the VM. I'm not sure if there's a way to do this through the VMware GUI, but I know that vmware-mount will come to the rescue! Here's the basic syntax to mount a hard-disk image: vmware-mount x: "c:\virtual machines\imagefile.vmdk" <-- this will mount the specified hard-disk to x:. Pretty handy 'eh?
My life of learning various things about technology including network administration, development, and 3D design

Location: Chicago, Illinois, United States
January 2006 / February 2006 / March 2006 / May 2006 / June 2006 / July 2006 / August 2006 / September 2006 / October 2006 / November 2006 / December 2006 / January 2007 / February 2007 / March 2007 / April 2007 / May 2007 / June 2007 / August 2007 / December 2007 / January 2008 / March 2008 / April 2008 / June 2008 / July 2008 / September 2008 / December 2008 / January 2009 / February 2009 / March 2009 / May 2009 /

Powered by Blogger