Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I preview the PowerShell command before execute?

Let's say I have the following PowerShell source:

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

Now, before call shell.Invoke(), I want check, for logging purposes, the final command line. In this case I expect something like

Get-NetAdapter -name Ethernet* -ThrottleLimit 5

I tested these, but none works:

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

Is there some built in way to check the final command line?

like image 651
Click Ok Avatar asked Jan 29 '21 03:01

Click Ok


People also ask

How do I view PowerShell commands?

To find the commands in a module, select the module from the Modules drop-down list. To select a command, click the command name. To use the command window, select a command, either by using the Name or by clicking the command name in the Commands list.

How can I tell what PowerShell script is doing?

When you turn on script-level tracing, each command that is executed is displayed in the Windows PowerShell console. By watching the commands as they are displayed, you can determine if a line of code in your script executes or if it is being skipped.

How do I Run a PowerShell command in the background?

The Start-Job cmdlet starts a PowerShell background job on the local computer. A PowerShell background job runs a command without interacting with the current session. When you start a background job, a job object returns immediately, even if the job takes an extended time to finish.

How to run PowerShell commands from the command prompt?

How to run PowerShell commands from the command prompt? How to run PowerShell commands from the command prompt? To run Powershell commands from the command prompt or cmd, we need to call the PowerShell process PowerShell.exe. Similarly, you can call any command.

How do I make Windows PowerShell wait for a job to finish?

Calling the Wait-Job cmdlet will make the process wait until the Receive-Job is called in the command line. Another method of making Windows PowerShell wait for the command line to finish is the Wait-Process cmdlet. Like our first example, we can pipe this into our executable file and wait for the command line to finish.

How do I review and run a PowerShell script?

An easy way to review and run a PowerShell script is to use the PowerShell ISE. To open a PowerShell file in the editor, simply right-click it and choose Edit

How does get-command work in PowerShell?

Beginning in PowerShell 3.0, by default, Get-Commandgets only the commands that run when you type the command name. In the following examples, the session includes a Get-Datefunction and a Get-Datecmdlet.


1 Answers

How about:

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());
            }
        }
    }
}

which given

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();
        }
    }
}

Outputs what you wanted:

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

Given more complex parameters, you may need to get more fancy as it will just output the type of the parameter and not necessarily a nice string representation of it.

like image 110
A Noethe Avatar answered Oct 07 '22 04:10

A Noethe