简体   繁体   English

如何从C#中的PowerShell命令获取Invoke-Command详细信息或调试输出

[英]How to get Invoke-Command verbose or debug output from a PowerShell command in C#

I have a PowerShell command that invokes a command on a remote machine to print out a debug message, and then prints out a debug message on the running machine, as follows: 我有一个PowerShell命令,该命令在远程计算机上调用命令以打印出调试消息,然后在运行的计算机上打印出调试消息,如下所示:

function Start-DebugTest {
    [cmdletbinding()]
    param ()

    $cmd = {
        $DebugPreference = 'Continue'
        Write-Debug -Message 'This is invoked DEBUG message'
    }

    $params = @{
        ComputerName = $ip
        Credential = $cred
        ScriptBlock = $cmd
    }

    Invoke-Command @params

    Write-Debug -Message 'This is a normal DEBUG message'
}

When I run the Start-DebugTest command locally I see the following output: 当我在本地运行Start-DebugTest命令时,我看到以下输出:

DEBUG: This is invoked DEBUG message
DEBUG: This is a normal DEBUG message

When I run this through C# like this: 当我像这样通过C#运行此代码时:

using (PowerShell powershell = PowerShell.Create())
{
    var command = powershell.AddCommand("Start-DebugTest");
    powershell.Runspace.SessionStateProxy.SetVariable("DebugPreference", "Continue");

    PSDataCollection<PSObject> output = new PSDataCollection<PSObject>();
            output.DataAdded += (sender, args) => DataAdded((PSDataCollection<PSObject>) sender, progress);
    powershell.Streams.Debug.DataAdded += (sender, args) => StreamDataAdded((PSDataCollection<DebugRecord>) sender, args, "DEBUG", progress);

    var resources = await Task.Factory.FromAsync(
         powershell.BeginInvoke<PSObject, PSObject>(null, output),
         asyncResult => powershell.EndInvoke(asyncResult));

    return resources;
}

private void StreamDataAdded<T>(PSDataCollection<T> records, DataAddedEventArgs e, string msgType, IProgress<string> progress) where T : InformationalRecord
{
    string msg = records[e.Index].Message;
    progress.Report($"{msgType}: {msg}");
}

private void DataAdded(PSDataCollection<PSObject> myp, IProgress<string> progress)
{
    Collection<PSObject> results = myp.ReadAll();
    foreach (PSObject result in results)
    {
        progress.Report($"OUTPUT: {result.ToString()}");
    }
}

I only receive the debug message running on the local machine and not the one from the invoked command, from either of the DataAdded events: 我仅从两个DataAdded事件之一中收到在本地计算机上运行的调试消息,而没有收到来自调用命令的调试消息:

DEBUG: This is a normal DEBUG message

If I see the output when running it locally, somehow the debug message is available. 如果在本地运行时看到输出,则可以通过某种方式获得调试消息。 Is there any way to access it from C#? 有什么办法可以从C#中访问它吗?

I reckon you probably have to specify your debug preference on the Invoke-Command call... 我认为您可能必须在Invoke-Command调用上指定调试首选项...

about_Preference_Variables 参阅about_Preference_Variables

The preference variables affect the PowerShell operating environment and all commands run in the environment. 首选项变量会影响PowerShell操作环境,并且所有命令都在该环境中运行。

The key point here is "in the environment" 这里的重点是“在环境中”

The command running on the other environment doesn't share the same preference setting as the calling command. 在其他环境上运行的命令与调用命令的偏好设置不同。

about_CommonParameters 参阅about_CommonParameters

-Debug[:{$true | $false}]

Displays programmer-level detail about the operation performed by the command. 显示有关命令执行的操作的程序员级详细信息。 This parameter works only when the command generates a debugging message. 仅当命令生成调试消息时,此参数才有效。 For example, this parameter works when a command contains the Write-Debug cmdlet. 例如,当命令包含Write-Debug cmdlet时,此参数有效。

Solution

$params = @{
    ComputerName = $ip
    Credential = $cred
    ScriptBlock = $cmd
    Debug = $true
}

Invoke-Command @params

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

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