I am trying to execute the PowerShell from WPF by pointing to the powershell.exe path. I have the following code
The error I am getting is The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.
http://blog.heshamamin.com/2009/04/calling-powershell-script-in-path-with.html
- string PSPath = string.Concat(Environment.SystemDirectory, "\\WindowsPowerShell\\v1.0\\powershell.exe");
- string ScriptPath = string.Concat(PSPath, " -ExecutionPolicy Unrestricted -Command ");
- string CommandPath = string.Concat("\"& {&'", regex[0].ToString(), "'", regex[1].ToString(), "}\"");
- ScriptPath = string.Concat(ScriptPath, CommandPath);
- using (Process p = new Process())
- {
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.FileName = PSPath;
- p.StartInfo.Arguments = ScriptPath;
- p.Start();
- string error = p.StandardOutput.ReadToEnd();
- }
- C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -Command "& {&'C:\TestPS\Testps1' -Arg1 -Arg2}"
Dorababu MekaRank #457 · 5.5K reputationPosted Dec 11, 2016, 8:49 PM EST
Amit GuptaRank #190 · 12.8K reputationPosted Dec 8, 2016, 1:34 AM EST
Anish Kumar88 reputationPosted Dec 8, 2016, 1:02 AM EST
HttpServerUtility.UrlEncode(your path)
HttpServerUtility.UrlDecode(your path)
I hope , it will work...Amit GuptaRank #190 · 12.8K reputationPosted Dec 8, 2016, 1:10 AM EST
Devi51 reputationPosted Dec 8, 2016, 1:17 AM EST
Devi51 reputationPosted Dec 8, 2016, 1:22 AM EST
Amit GuptaRank #190 · 12.8K reputationPosted Dec 8, 2016, 5:12 AM EST
Devi51 reputationPosted Dec 9, 2016, 12:34 AM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts