简体   繁体   English

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

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

I'm trying to write a PowerShell module as a VB.NET project with Visual Studio 2015. I've been able to put a few commands together, compile the class library into a DLL, import the module into a PowerShell session and call the command I created.我正在尝试使用 Visual Studio 2015 将 PowerShell 模块编写为 VB.NET 项目。我已经能够将一些命令放在一起,将类库编译为 DLL,将模块导入到 PowerShell 会话中并调用我创建的命令。 All good so far.到目前为止一切都很好。 But as I'm expanding this module I'm going to need to be able to debug it.但是当我扩展这个模块时,我需要能够调试它。

So I added another project to the solution, a console application.所以我在解决方案中添加了另一个项目,一个控制台应用程序。 I set it as the startup project and referenced the PowerShell class in the first project.我将其设置为启动项目,并在第一个项目中引用了 PowerShell 类。 So far when I call the PowerShell function I wrote all the work is being done in the EndProcessing() subroutine.到目前为止,当我调用 PowerShell 函数时,我编写的所有工作都在EndProcessing()子例程中完成。 I can't call that from my console app because It's protected.我无法从我的控制台应用程序调用它,因为它受到保护。

The question: How do I properly call my Get-TestCommand PowerShell function from a console app in such a way that Visual Studio knows I'm referencing code in a separate project and not the compiled DLL while triggering the breakpoints I put in the Powershell class library?问题:如何从控制台应用程序正确调用我的Get-TestCommand PowerShell 函数,让 Visual Studio 知道我在单独的项目中引用代码而不是编译的 DLL,同时触发我放入 Powershell 类中的断点图书馆?

It's possible to debug your cmdlet directly without needing a separate project.可以直接调试您的 cmdlet,而无需单独的项目。 Open the properties of your class library project and configure the Debug tab as follows (assuming Windows is installed to C: ):打开类库项目的属性并按如下方式配置Debug选项卡(假设 Windows 安装到C: ):

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

PowerShell 调试的类库项目配置

调试 PowerShell cmdlet

Edit: try this link or this step-by-step编辑:试试这个链接或这个一步一步

Either downdload PowerShell Tools for Visual Studio 2015 or use Windows PowerShell ISE .下载适用于 Visual Studio 2015 的 PowerShell 工具或使用Windows PowerShell ISE

How to Debug Scripts in Windows PowerShell ISE如何在 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.更新时间:2013 年 10 月 17 日 适用于:Windows PowerShell 2.0、Windows PowerShell 3.0、Windows PowerShell 4.0、Windows PowerShell 5.0 本主题介绍如何使用 Windows PowerShell® 集成脚本环境 (ISE) 可视化调试功能在本地计算机上调试脚本. 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.您可以在 Windows PowerShell 调试环境中设置三种类型的断点: 行断点。 The script pauses when the designated line is reached during the operation of the script...脚本运行过程中到达指定行时脚本暂停...

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

相关问题 如何在 Visual Studio 中调试引用的解决方案? - How do I debug a referenced solution in Visual Studio? 没有Visual Studio的情况下如何调试Windows服务? (在客户的计算机上) - How can I debug a windows service without Visual Studio? (on a client's machine) Slim .net:如何使用visual studio调试测试夹具? - Slim .net: How do I debug test fixtures using visual studio? 如何在Visual Studio 2017中调试.NET 4.6框架源代码? - How do I debug .NET 4.6 framework source code in Visual Studio 2017? 当Visual Studio中仅显示一个线程时,如何调试死锁? - How do I debug a deadlock when only one thread shows up in Visual Studio? 如何使用Visual Studio在.Net中调试CLR SQL Server用户定义的功能? - How do i debug CLR SQL Server User-Defined Function in .Net using visual studio? 如何在Visual Studio中调试python - how to debug python in visual studio 如何从Visual Studio 2012调试引用的dll - How can I debug a referenced dll from Visual Studio 2012 如何从 Visual Studio 中的另一个解决方案调试项目? - How can I debug a project from another solution in Visual Studio? 如何在 vscode 中调试 visual studio 中的原始解决方案 - How can I debug a solution original in visual studio in vscode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM