简体   繁体   English

调试PowerShell cmdlet时出现问题

[英]Problems debugging a PowerShell cmdlet

I'm using Visual Studio 2010 on Windows 7 64-bit Professional. 我在Windows 7 64位专业版上使用Visual Studio 2010。 I'm having trouble debugging a custom PowerShell cmdlet. 我在调试自定义PowerShell cmdlet时遇到问题。

Configuration 组态

  • Language: C#, targeting .NET Framework 3.5 SP1. 语言:C#,面向.NET Framework 3.5 SP1。
  • Platform target: Any CPU 平台目标:任何CPU
  • Start Action: C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe 启动操作: C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe
  • Command line arguments: -noexit -command Add-PSSnapIn MyCustomSnapIn 命令行参数: -noexit -command Add-PSSnapIn MyCustomSnapIn

Problem 1: Failure to attach when I press F5 (Debug → Start Debugging) 问题1:按F5时无法连接(调试→开始调试)

  • PowerShell opens, and Task Manager indicates that powershell.exe is running as a 64-bit process. PowerShell打开,任务管理器指示powershell.exe作为64位进程运行。 The Image Path Name column shows the same executable specified in the Start Action. “映像路径名称”列显示“开始操作”中指定的相同可执行文件。
  • If I choose Debug → Break All in Visual Studio, I receive a message "Unable to break execution. This process is not currently executing the type of code that you selected to debug." 如果我在Visual Studio中选择Debug→Break All,我会收到一条消息“无法中断执行。此过程当前没有执行您选择调试的代码类型。”

Problem 2: Unexpectedly launches as a 32-bit process when I press Ctrl+F5 (Debug → Start Without Debugging) 问题2:当我按下Ctrl + F5时,意外启动为32位进程(Debug→Start Without Debugging)

  • PowerShell opens. PowerShell打开。 Task Manager indicates that powershell.exe is running as a 32-bit process - this time the Image Path Name shows a SysWOW64 redirection. 任务管理器指示powershell.exe作为32位进程运行 - 这次映像路径名称显示SysWOW64重定向。

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. 现在调试的烦人方法:我发现调试cmdlet的唯一方法是按F5,然后选择Debug→Detach All,然后选择Debug→Attach To Process并重新附加Visual Studio。

Problem 1: 问题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 在我看来,这就像VS2010中的一个错误: 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. 使用VS2008应该有所帮助。

Update: I found more convenient way to debug powershell cmdlets . 更新:我找到了更方便的方法来调试PowerShell cmdlet In the solution explorer right click on solution node -> Add -> New project -> Select powershell.exe file (C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe). 在解决方案资源管理器中,右键单击解决方案节点 - >添加 - >新建项目 - >选择powershell.exe文件(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)". 然后转到项目属性(右键单击项目节点并选择“属性”)并将“调试器类型”属性设置为“托管(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 ). 不要忘记注册您的Provider或CmdLet(通过运行post build事件,请参阅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 . 在问题#2上,由于Visual Studio是在WOW64上运行的32位进程,因此路径C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe将重定向到C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe Which is where the 32-bit version of PowerShell lives. 这是32位版本的PowerShell所在的位置。

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. 我发现以下方案适用于PowerShell 2.0。 I am also using VS2010 SP1 on Windows 7 64-bit. 我也在Windows 7 64位上使用VS2010 SP1。

With PS 2.0 you no longer have to install cmdlets using installutil . 使用PS 2.0,您不再需要使用installutil安装cmdlet。 Instead you can use Import-Module instead (which does not require Admin rights). 相反,你可以使用Import-Module来代替( 不需要管理员权限)。 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”). 在modules文件夹下,创建另一个与cmdlet DLL同名的文件夹(减去“.DLL”)。 This folder will contain your binary and a psd1 file that describes your DLL (see Module Manifests ). 此文件夹将包含您的二进制文件和描述您的DLL的psd1文件(请参阅模块清单 )。 For my convenience I created this folder as a folder symbolic link to my project's bin\\debug folder. 为方便起见,我创建了这个文件夹,作为项目的bin \\ debug文件夹的文件夹符号链接。

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. 您仍然需要从Visual Studio运行PowerShell(或PowerShell ISE),如项目属性的“调试”选项卡的“开始操作”部分中的其他地方所述。

Set your breakpoints and go. 设置你的断点然后去吧。 Once PowerShell starts, type: PowerShell启动后,键入:

Import-Module <ModuleName>

Then run your cmdlet. 然后运行cmdlet。


Sample 样品

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): 在PowerShell类型中(也可以放在您的个人资料中):

Import-Module MyCmdlet

For me, this hits all my breakpoints and also stops on exceptions. 对我来说,这会触及我的所有断点,也会停止异常。 All without having to attach to a process, etc. 所有这些都无需附加到流程等。

Problem 1: powershell.exe is not actually a managed executable. 问题1:powershell.exe实际上不是托管可执行文件。 It hosts the CLR itself, so you need to enable native code debugging alongside managed for this to work. 它托管CLR本身,因此您需要启用本地代码调试以及托管服务才能工作。

As for problem 2, I'm not sure about this. 至于问题2,我对此不太确定。 Obviously VS itself is a 32bit process so perhaps it's interfering here. 显然VS本身是一个32位进程,所以它可能会干扰这里。

I was able to solve problem #2 by creating the folder C:\\dev\\PowerShell\\x64 and copying everything from C:\\Windows\\System32\\WindowsPowerShell\\v1.0 into it. 我能够通过创建文件夹C:\\dev\\PowerShell\\x64并将C:\\Windows\\System32\\WindowsPowerShell\\v1.0所有内容复制到其中来解决问题#2。 I created a similar folder C:\\dev\\PowerShell\\x86 . 我创建了一个类似的文件夹C:\\dev\\PowerShell\\x86 The intended version of PowerShell always launches when I specify these paths, so the shortest way to start debugging is now Start Without Debugging followed by Attach To Process. 当我指定这些路径时,PowerShell的预期版本总是启动,因此开始调试的最短方法现在是Start Without Debugging,然后是Attach To Process。

You don't mention how you're installing the plugin before calling Add-PSSnapin to make it usable in your debugging session. 在调用Add-PSSnapin以使其在调试会话中可用之前,您没有提到如何安装插件。

Make sure that you are installing the plugin using C:\\Windows\\Microsoft.NET\\Framework64\\v2.0.50727\\InstallUtil.exe , and not the one in ...\\Framework... - this stumped me for a couple days. 确保您使用C:\\Windows\\Microsoft.NET\\Framework64\\v2.0.50727\\InstallUtil.exe安装插件,而不是...... \\ Framework中的那个... - 这让我困扰了几天。

我在VS2010上遇到了这个问题,但在安装SP1后它就消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM