I'm using Visual Studio 2010 on Windows 7 64-bit Professional. I'm having trouble debugging a custom PowerShell cmdlet.
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe
-noexit -command Add-PSSnapIn MyCustomSnapIn
Problem 1: Failure to attach when I press F5 (Debug → Start Debugging)
Problem 2: Unexpectedly launches as a 32-bit process when I press Ctrl+F5 (Debug → Start Without Debugging)
The annoying way to debug right now: The only way I've found to debug my cmdlet is to press F5, then select Debug→Detach All, then select Debug→Attach To Process and reattach Visual Studio.
Problem 1:
Seems to me like a bug in VS2010 reported here: https://connect.microsoft.com/VisualStudio/feedback/details/539389/debugging-powershell-cmdlet-from-vs-2010-does-not-stop-at-breakpoints?wa=wsignin1.0
Using VS2008 should help.
Update: I found more convenient way to debug powershell cmdlets. In the solution explorer right click on solution node -> Add -> New project -> Select powershell.exe file (C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe). Set newly added project as start up project (right click and select "Set As Startup Project"). Then go to project properties (right click on project node and select "Properties") and set the "Debugger Type" property to "Managed (v2.0, v1.1, v1.0)". Don't forget to register your Provider or CmdLet (by running post build events, see http://msdn.microsoft.com/en-us/library/ms714644%28v=vs.85%29.aspx). Now, the program should stop at the breakpoint.
On problem #2, since Visual Studio is a 32-bit process running on WOW64, the path C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe
is redirected to C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
. Which is where the 32-bit version of PowerShell lives.
I know that this posting is about a year old at this point, but it may help someone else struggling with this anyway.
I have found that the following scenario works for me with PowerShell 2.0. I am also using VS2010 SP1 on Windows 7 64-bit.
With PS 2.0 you no longer have to install cmdlets using installutil. Instead you can use Import-Module
instead (which does not require Admin rights). I will not go into full detail on how that is done as a web search will reveal most details, but in short, you will need to create a folder (if it does not already exist) at:
md (Join-Path (Split-Path $profile) modules)
Under the modules folder, create another folder with the same name as your cmdlet DLL (minus “.DLL”). This folder will contain your binary and a psd1 file that describes your DLL (see Module Manifests). For my convenience I created this folder as a folder symbolic link to my project’s bin\debug folder.
You still need to run PowerShell (or PowerShell ISE) from Visual Studio as describe elsewhere in the “Start Action” section of the Debug tab of your project properties.
Set your breakpoints and go. Once PowerShell starts, type:
Import-Module <ModuleName>
Then run your cmdlet.
C:\Users\<me>\Documents\WindowsPowerShell\modules\MyCmdlet\MyCmdlet.dll
C:\Users\<me>\Documents\WindowsPowerShell\modules\MyCmdlet\MyCmdlet.pdb
C:\Users\<me>\Documents\WindowsPowerShell\modules\MyCmdlet\MyCmdlet.psd1
C:\Users\<me>\Documents\WindowsPowerShell\modules\MyCmdlet\MyCmdlet.Types.ps1xml (etc.)
In PowerShell type (can be put in your profile too):
Import-Module MyCmdlet
For me, this hits all my breakpoints and also stops on exceptions. All without having to attach to a process, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With