Wednesday, October 15, 2008

nant tips and tricks - part #2 Windows Shares and Visual Basic

Here are some more nant examples I've used in my projects.

Nant Map a Windows Share
These simple tasks will map a Windows network share as a local drive letter using the NET USE command
The properties in the examples are pretty self-explainitory. The ones of interest were defined as follows in my scenario where I was mounting the c: of the remote machine defined in the property named 'server':

Nant reading the result code of an Installshield Install
When you run an installshield installer with silent mode, it outputs a text file. Sound practice says to read that file and ensure the installer did not fail. This reads the setup.log file from the path defined in deployLocalPath
UPDATE: The above is pretty hack - now that I recognized you can simply use a regex task.Here's a simplier example that does the same thing and loads the Result code into setupResultCode
Nant and visual basic scripts (vbs)
Sometimes doing this is easier with visual basic and if you don't have VisualStudio or can't code.. visual basic scripting is enough to access things like WMI so you can manipulate Windows machines. Visual Basic scripts are not intended to be console applications anymore.. so first problem is running them in a command line so output can be viewed and captured by nant. Solution.. run the script using cscript.exe. The following example calls the vbs script remoteprocUP.vbs and passes a few arguements to the vbs file
The second problem with visual basic scripts is getting some debugging or informational messages out to console so they can be seen or logged by nant. When using cscript.exe to launch the program, in the vbs file you can use WScript.StdOut.WriteLine(string) or WScript.Echo. Example:

The last challenge is handling error codes from visual basic apps. Again, when using cscript, you can simply use WScript.Quit 0 to return 0 (success) or WScript.Quit 1 to return 1 (failure). nant will see the failure code and automatically stop your script.

Here is a complete example. nant calls a target who's overall job is wait until a website is online. It calls a visual basic script using cscript and relies on error codes to know if things failed or not.

And here is the visual basic script

No comments: