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

自古美人都是妖i 提交于 2021-01-20 18:09:19

问题


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. 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. So far when I call the PowerShell function I wrote all the work is being done in the EndProcessing() subroutine. 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?


回答1:


It's possible to debug your cmdlet directly without needing a separate project. Open the properties of your class library project and configure the Debug tab as follows (assuming Windows is installed to C:):

  • Start Action
    • Start external program: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • Start Options
    • Command line arguments: -NoLogo -Command "Import-Module '.\MyModule.dll'; Get-TestCommand;"




回答2:


Edit: try this link or this step-by-step

Either downdload PowerShell Tools for Visual Studio 2015 or use 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...



来源:https://stackoverflow.com/questions/35298963/how-do-i-debug-a-windows-powershell-module-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!