dinsdag 25 mei 2010

Debugging Powershell Cmdlets V2

As I was trying to debug my custom powershell commandlets through visual studio I had the problem that it was not hitting the breakpoints automatically when running through F5.

The trick is to create a powershell.exe.cfg in your PowerShell folder (C:\Windows\System32\WindowsPowerShell\v1.0):

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
   <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>

The other steps to follow are still quite the same as Bart De Smet has explained in his blogpost here: http://bartdesmet.net/blogs/bart/archive/2008/02/03/easy-windows-powershell-cmdlet-development-and-debugging.aspx

Some things I did differently are setting post build events like this:

@SET GACUTIL="C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\gacutil.exe"
@SET INSTALLUTIL="C:\Windows\Microsoft.NET\Framework64\v2.0.50727\installutil.exe"

%GACUTIL% -if Shaks.PowerShell.SharePoint.Cmdlets
%INSTALLUTIL% -i Shaks.PowerShell.SharePoint.Cmdlets.dll

In the Debug-tab of the project properties I start an external program (powershell.exe) with the following attributes:

-PSConsoleFile Shaks.PowerShell.SharePoint.Cmdlets.psc1  -noexit

I’ll post my psc1 file here for reference, although you can just generate this:

<?xml version="1.0" encoding="utf-8"?>
<PSConsoleFile ConsoleSchemaVersion="1.0">
  <PSVersion>2.0</PSVersion>
  <PSSnapIns>
    <PSSnapIn Name="shaks.powershell.sharepoint.cmdlets" />
  </PSSnapIns>
</PSConsoleFile>

The commandlet itself will be for a next blogpost.