run powershell script with C# System.Diagnostics.Process , powershell script sees no arguments

StackOverflow https://stackoverflow.com/questions/13550559

  •  02-12-2021
  •  | 
  •  

Question

I decided to use System.Diagnostics.Process to run a powershell script just because it seemed simple,I didn't care about the result output, and I wanted the user to be able to see what was running

Process process = new Process();

Process.StartInfo.FileName = "powershell.exe"
Process.StartInfo.Arguments = String.Format("-executionpolicy unrestricted -noexit \"{0}\" -XmlConfigFile {1}",scriptname, configFile);
process.Start();

When I run this the script sees no arguments. Due to the noexit I can test $args and $MyInvocation at the end. No arguments. If I run it in cmd.exe it works, If I leave off the {1} it complains about -XmlConfigFile missing an argument.

edit: turns out It was my fault I was running nested powershell sessions to deal with forcing powershell to use 4.0 and only passing along args which doesn't include arguments in $psboundparameters.

Était-ce utile?

La solution

Try this, as it works for me:

Process.StartInfo.Arguments = String.Format("-executionpolicy unrestricted 
-noexit -file \"{0}\" -XmlConfigFile {1}",scriptname, configFile);

Though I dont know what -XmlConfigFile does. As it seems to be no valid argument: http://technet.microsoft.com/en-us/library/hh847736.aspx

So you may remove it for a start.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top