Thursday, May 20, 2010

PowerShell and slash-colon parameters

PowerShell plays around with the quotes in parameters when part of the parameter is quoted like this:

/param:"quoted path"

This gets passed to the application as:
"/param:quoted path"

The only workaround I have found works like this:
$cmd = "& `"application`" /p:`"path`"; `$result = `$?"
Invoke-expression $cmd
if(!$result)
{
    throw "Application failed."
}

Monday, May 3, 2010

Running MSTest in Parallel

This blog describes how to execute tests in parallel in MSTest.

Writing Debug (-Verbose) Output in PowerShell

To write output in PowerShell that is only displayed when the -Verbose switch is given, use the following attribute in your script:
[CmdletBinding]

Then use write-verbose.

This makes the function an Advanced Function.

Sunday, May 2, 2010

Highlight PowerShell in red when running as Administrator

If you have the PowerShell Community Extensions installed, you can load your profile for editing by running notepad $profile or ep.

Paste the code from this blog into your profile:
& {
    $wid=[System.Security.Principal.WindowsIdentity]::GetCurrent()
    $prp=new-object System.Security.Principal.WindowsPrincipal($wid)
    $adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
    $IsAdmin=$prp.IsInRole($adm)
    if ($IsAdmin)
    {
        (get-host).UI.RawUI.Backgroundcolor="DarkRed"
        clear-host
    }
}

If you are running all users as standard users (which you should really be doing, see why here), there is no need to use this script. Simply put the following code into the Administrator profile:
(get-host).UI.RawUI.Backgroundcolor="DarkRed"
clear-host