【问题标题】:How can I preview the PowerShell command before execute?如何在执行前预览 PowerShell 命令?
【发布时间】:2021-05-03 00:32:21
【问题描述】:

假设我有以下 PowerShell source

PowerShell shell = PowerShell.Create().AddCommand("Get-NetAdapter")
                                      .AddParameter("name", "Ethernet*")
                                      .AddParameter("ThrottleLimit", 5);

现在,在调用shell.Invoke() 之前,我想检查一下最终的命令行,以用于记录目的。在这种情况下,我期望类似

Get-NetAdapter -name Ethernet* -ThrottleLimit 5

我测试了这些,但没有一个有效:

shell.Commands.ToString()
shell.Commands.Commands.ToString()
shell.Commands.Commands.First().CommandText
shell.Commands.Commands.First().ToString()

是否有一些内置方法可以检查最终命令行?

【问题讨论】:

    标签: c# powershell .net-core powershell-core


    【解决方案1】:

    怎么样:

    namespace SomeProject.Extensions
    {
        using System;
        using System.Management.Automation;
        using System.Management.Automation.Runspaces;
        using System.Text;
    
        public static class PowerShellExtensions
        {
            public static void LogCommandLine(this PowerShell commandToLog)
            {
                foreach (Command command in commandToLog.Commands.Commands)
                {
                    StringBuilder commandLine = new StringBuilder(command.ToString());
    
                    foreach (CommandParameter parameter in command.Parameters)
                    {
                        commandLine.Append($" --{parameter.Name} {parameter.Value}");
                    }
    
                    Console.WriteLine(commandLine.ToString());
                }
            }
        }
    }
    

    给定的

    namespace SomeProject.Extensions.UnitTests
    {
        using System.Management.Automation;
        using NUnit.Framework;
    
        [TestFixture]
        [Parallelizable(ParallelScope.All)]
        [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        public class PowerShellExtensionsTests
        {
            [Test]
            [Category(nameof(PowerShellExtensions))]
            public void TestCommandLine()
            {
                PowerShell shell = PowerShell.Create().AddCommand("Get-NetAdapter")
                                                      .AddParameter("name", "Ethernet*")
                                                      .AddParameter("ThrottleLimit", 5);
    
                shell.LogCommandLine();
            }
        }
    }
    

    输出你想要的:

    Get-NetAdapter --name Ethernet* --ThrottleLimit 5
    

    给定更复杂的参数,你可能需要更花哨,因为它只会输出参数的类型,不一定是它的良好字符串表示。

    【讨论】:

    • 这行得通,但我宁愿让你的“LogCommandLine”扩展方法返回一个字符串,让调用者处理控制台输出。这样,调用者可以决定在其他地方记录命令转储。虽然这实际上是一个小修改,但 OP 可能已经弄清楚了。
    【解决方案2】:

    如果您在从参数构造对象时特别需要记录对象的创建,您可能需要考虑创建一个在构造过程中记录参数的扩展方法。

    class Program
        {
            static void Main()
            {
                PowerShell shell = PowerShell.Create().Setup("Get-NetAdapter", ("name", "Ethernet"), ("ThrottleLimit", 5));
            }
        }
        public static class Logger
        {
            public static void Log(in string message)
            {
                Console.WriteLine(message);
            }
        }
        public static class PowershellExtenstions
        {
            public static PowerShell Setup(this PowerShell powerShell, in string command, params (string Name, object Value)[] parameters)
            {
                string message = command ?? string.Empty;
    
                powerShell.AddCommand(command);
    
                foreach (var parameter in parameters)
                {
                    message += $" -{ parameter.Name } { parameter.Value }";
    
                    powerShell.AddParameter(parameter.Name, parameter.Value);
                }
    
                Logger.Log(message);
    
                return powerShell;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 2018-11-09
      • 2018-12-04
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多