如何在 Visual Studio 中调试 Windows PowerShell 模块?

How do I debug a Windows PowerShell module in Visual Studio?

我正在尝试使用 Visual Studio 2015 将 PowerShell 模块编写为 VB.NET 项目。我已经能够将一些命令放在一起,编译 class 库到 DLL 中,将模块导入 PowerShell 会话并调用我创建的命令。到目前为止一切都很好。但是当我扩展这个模块时,我需要能够调试它。

所以我在解决方案中添加了另一个项目,一个控制台应用程序。我将其设置为启动项目,并在第一个项目中引用了 PowerShell class。到目前为止,当我调用 PowerShell 函数时,我编写的所有工作都在 EndProcessing() 子例程中完成。我无法从我的控制台应用程序调用它,因为它受到保护。

问题:如何从控制台应用程序正确调用我的 Get-TestCommand PowerShell 函数,使 Visual Studio 知道我引用的是单独项目中的代码,而不是已编译的 DLL在触发我在 Powershell class 库中设置的断点时?

编辑:试试这个link or this step-by-step

要么下载PowerShell Tools for Visual Studio 2015 或使用 Windows PowerShell ISE.

How to Debug Scripts in Windows PowerShell ISE

Updated: October 17, 2013 Applies To: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0 This topic describes how to debug scripts on a local computer by using the Windows PowerShell® Integrated Scripting Environment (ISE) visual debugging features. How to manage breakpoints How to manage a debugging session How to step over, step into, and step out while debugging How to display the values of variables while debugging
How to manage breakpoints A breakpoint is a designated spot in a script where you would like operation to pause so that you can examine the current state of the variables and the environment in which your script is running. Once your script is paused by a breakpoint, you can run commands in the Console Pane to examine the state of your script. You can output variables or run other commands. You can even modify the value of any variables that are visible to the context of the currently running script. After you have examined what you want to see, you can resume operation of the script. You can set three types of breakpoints in the Windows PowerShell debugging environment: Line breakpoint. The script pauses when the designated line is reached during the operation of the script...

无需单独的项目即可直接调试您的 cmdlet。打开 class 库项目的属性并按如下方式配置 Debug 选项卡(假设 Windows 安装到 C:):

  • 开始行动
    • 启动外部程序:C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • 启动选项
    • 命令行参数:-NoLogo -Command "Import-Module '.\MyModule.dll'; Get-TestCommand;"