0

I have one question about Powershell can not run through inside .NET Process.

I have the following Powershell

$livingDocPath = "C:\Users\chris\.dotnet\tools\livingdoc.exe"

$dllPath = "C:\Mylib\bin\Release\net6.0-windows\Mylib.dll"

$jsonPath = "C:\Mylib\bin\Release\net6.0-windows\TestExecution.json"

$command = "test-assembly"

$parameter = "-t"

& $livingDocPath $command $dllPath $parameter $jsonPath

BTW, livingdoc is a dotnet tool, which I confirmed is well installed in my machine.

This script can run through if I run it in the shell directly. The expected behavior is it will produce a html report.

But, when I try to execute this powershell script in a .NET process, it can not generate the expected result:

        static void Main(string[] args)
        {
            var binFolder = @"C:\Users\chris\Desktop\powershell";
            var arguments = Path.Combine(binFolder, "testLivingdoc.ps1");
            arguments = "-command " + arguments;
            Console.WriteLine($"script start with argument: {arguments}");

            var startInfo = new ProcessStartInfo
            {
                FileName = @"powershell",
                Arguments = arguments,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = false
            };

            using var p = Process.Start(startInfo);
            var stdError = p.StandardError.ReadToEnd();
            var stdOutput = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            if (!string.IsNullOrEmpty(stdError))
                Console.WriteLine($"powershell running error: {stdError}");
            if (!string.IsNullOrEmpty(stdOutput))
                Console.WriteLine($"powershell running result: {stdOutput}");
        }

You can see the testLivingdoc.ps1 is my script.

I tried a simple script which just prints the version of livingdoc tool,

$livingDocPath = "C:\Users\chris\.dotnet\tools\livingdoc.exe"
& $livingDocPath --version

Then it can run through inside the .NET process.

So what's happening here?

4
  • Do you get any error output? What is the exit code of the process? (Inspect $LastExitCode in the script immediately after calling & $livingDocPath ...) Commented Mar 20, 2024 at 12:15
  • no any error message, just the application just hung there. Commented Mar 20, 2024 at 12:37
  • BTW, If I on purpose make the path of json file wrong, then I can get the error message complaining "can't find the file". Commented Mar 20, 2024 at 12:45
  • Why are you not using the powershell.exe file already installed on the machine? It looks like the .NET process is executing a PowerShell command line installed in your user folder. Are you sure you and the .NET tool are using the same version of PowerShell? Commented Mar 20, 2024 at 17:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.