I’ve done a lot of work with Powershell this week, I just wanted to note a few things that I ran across.
1) To find the version of Powershell installed:
$host.version
PS 3.0 is the current version, although a preview for version 4.0 was recently released.
2) The Set-Location cmdlet, to change the working location, much like cd at the command line. I was trying to run some BAT files, which used relative paths. The BAT didn’t work correctly until I set the location to the same directory the BAT was in (which seems obvious now but did trip me up at first).
3) The Invoke-Sqlcmd cmdlet that allows you to execute a SQL statement or run a script, much like the SqlCmd command line tool.
Technet
Using this cmdlet will set the current location to the specified database location, which messed me up since I was working with relative paths afterwards.
4) I was using Copy_Item to move some files. There were sub-directories to move over as well, which the -recursive parameter handles. The directories were under source control (Subversion) so I wanted to exclude the _svn folders. Using the -exclude parameter excluded the _svn directory at the root level, but it didn’t exclude them in the sub-directories (I ran across many other posts with the same finding). I only had one sub-directory, so I just used remove-item to delete it after it was copied across, but it seems like it should have been excluded in the first place.
5) Related to item #4, when I first tried to copy a directory with its sub-directories, I ended up with all of the files in the destination directory, without the sub-directory structure. Once I used new-item to create the destination folder first, then I was able to copy the files over with the directory structure intact.
6) Running unit tests
I wanted to be able to run unit test against a database from a Powershell script. We use MBUnit and Gallio. I had an existing unit test that runs through a work flow in the sequence that I specified.
I set the location for the Gallio.Echo executable, which will run the tests. I specify the path to the compiled DLL with the unit test code, and use a filter flag to specify the namespace and the name of the method that I want executed.
The output from the tests will be written to the console window.
set-location “C:\Program Files\Gallio\bin”
$GallioArgumentList = “C:\UnitTestPath\UnitTests.dll /f:`”Type:Namespace.UnitTest and Member:MethodName`””
start-process -filepath “Gallio.Echo.exe” -argumentlist $GallioArgumentList -nonewwindow -wait