如何使用PowerShell Core编写调试流,以便被SysInternals DBGVIEW.EXE捕获

问题描述 投票:0回答:1

[我有许多脚本使用OutputDebugString Windows API输出有意义的状态信息,以便当它们在生产环境中运行时,我可以拉起SysInternals调试查看器,查看正在发生的一切。

当PowerShell Core 7推出时,我想将脚本移过来,以便可以利用新功能。我希望只要在Windows上运行Pwsh.exe,此Windows特定代码就可以正常运行,但是我错了。

Write-Debug完全没有帮助。

是否有跨平台支持的方式来输出在Windows上可以由Debug Viewer读取的消息?

FWIW,这是我使用很长时间的代码:

$VB = @"
'http://www.pinvoke.net/default.asp
Public Class WinAPI
    <System.Runtime.InteropServices.DllImport("kernel32.dll")> _
    Public Shared Sub OutputDebugString(ByVal lpOutputString As String)
    End Sub
End Class
"@

Add-Type $VB -Language Visualbasic

function Write-DebugMessage([string]$Message)
{
    $Msg = "[$Script:ScriptHelperAppName] $Message"
    [WinAPI]::OutputDebugString($Msg)
    Write-Verbose $Msg
}

powershell-core
1个回答
0
投票
我刚刚将.Net代码转换为C#,它可以正常工作。

$WinAPI = @" public class WinAPI { [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)] public static extern void OutputDebugString(string message); } "@ Add-Type $WinAPI -Language CSharp [WinAPI]::OutputDebugString("C# Test")

© www.soinside.com 2019 - 2024. All rights reserved.