mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add testexe.exe -echocmdline to output raw command line received by the process on Windows (#18591)
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace TestExe
|
||||
{
|
||||
@@ -11,6 +12,7 @@ namespace TestExe
|
||||
{
|
||||
private static int Main(string[] args)
|
||||
{
|
||||
int exitCode = 0;
|
||||
if (args.Length > 0)
|
||||
{
|
||||
switch (args[0].ToLowerInvariant())
|
||||
@@ -18,6 +20,9 @@ namespace TestExe
|
||||
case "-echoargs":
|
||||
EchoArgs(args);
|
||||
break;
|
||||
case "-echocmdline":
|
||||
EchoCmdLine();
|
||||
break;
|
||||
case "-createchildprocess":
|
||||
CreateChildProcess(args);
|
||||
break;
|
||||
@@ -28,17 +33,23 @@ namespace TestExe
|
||||
case "-stderr":
|
||||
Console.Error.WriteLine(args[1]);
|
||||
break;
|
||||
case "--help":
|
||||
case "-h":
|
||||
PrintHelp();
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine("Unknown test {0}", args[0]);
|
||||
exitCode = 1;
|
||||
Console.Error.WriteLine("Unknown test {0}. Run with '-h' for help.", args[0]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Test not specified");
|
||||
exitCode = 1;
|
||||
Console.Error.WriteLine("Test not specified");
|
||||
}
|
||||
|
||||
return 0;
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
// <Summary>
|
||||
@@ -52,6 +63,36 @@ namespace TestExe
|
||||
}
|
||||
}
|
||||
|
||||
// <Summary>
|
||||
// Echos the raw command line received by the process plus the arguments passed in.
|
||||
// </Summary>
|
||||
private static void EchoCmdLine()
|
||||
{
|
||||
string rawCmdLine = "N/A";
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
nint cmdLinePtr = Interop.GetCommandLineW();
|
||||
rawCmdLine = Marshal.PtrToStringUni(cmdLinePtr);
|
||||
}
|
||||
|
||||
Console.WriteLine(rawCmdLine);
|
||||
}
|
||||
|
||||
// <Summary>
|
||||
// Print help content.
|
||||
// </Summary>
|
||||
private static void PrintHelp()
|
||||
{
|
||||
const string Content = @"
|
||||
Options for echoing args are:
|
||||
-echoargs Echos back to stdout the arguments passed in.
|
||||
-echocmdline Echos the raw command line received by the process.
|
||||
|
||||
Other options are for specific tests only. Read source code for details.
|
||||
";
|
||||
Console.WriteLine(Content);
|
||||
}
|
||||
|
||||
// <Summary>
|
||||
// First argument is the number of child processes to create which are instances of itself
|
||||
// Processes automatically exit after 100 seconds
|
||||
@@ -73,4 +114,10 @@ namespace TestExe
|
||||
Thread.Sleep(100000);
|
||||
}
|
||||
}
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
[LibraryImport("Kernel32.dll")]
|
||||
internal static partial nint GetCommandLineW();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user