Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

For Love of Software: Calling a PowerShell script in a path with a white space from command line[^]

My script looks as follows

C#
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -Command "& 'c:\Test PS\Palindrome.ps1' -param Hello"


which is executing fine when I directly copy paste into PowerShell, but not from the Win froms application

Code for the same

ConsoleApplication2.rar - Google Drive[^]

What I have tried:

C#
ProcessStartInfo proc = new ProcessStartInfo();
           string PSPath = string.Concat(Environment.SystemDirectory, "\\WindowsPowerShell\\v1.0\\powershell.exe");
           proc.FileName = PSPath;
           string DirectoryPath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
           string script1 = string.Concat(DirectoryPath, "\\Palindrome.ps1 -param Hello");
           var match1 = Regex.Split(script1, @"(?<=\.ps1)\s+");
           string newfile1 = string.Concat("\"& '" + match1[0] + "'", " ", match1[1], "\"");
           string ScriptPath = string.Concat(PSPath, " -ExecutionPolicy Unrestricted -Command ");
           ScriptPath = string.Concat(ScriptPath, newfile1);

           //ScriptPath = string.Concat(ScriptPath, script);
           proc.Arguments = ScriptPath;
           using (Process p = new Process())
           {
               proc.Verb = "runas";
               proc.UseShellExecute = false;
               proc.RedirectStandardError = true;
               p.StartInfo = proc;
               p.Start();
               StreamReader myStreamReader = p.StandardError;
               p.WaitForExit();
               if (p.ExitCode != 0)
               {
                   MessageBox.Show(myStreamReader.ReadToEnd());
               }
           }
Posted
Updated 11-Dec-16 19:36pm
v3

That's because your scripts aren't the same:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -Command "& 'c:\Test PS\Palindrome.ps1' -param Hello"
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -Command \&'D:\Documents\AA Backed Up\My Projects\GeneralTesting\GeneralTesting\bin\Debug\Palindrome.ps1' -param Hello"

Start by replacing that "\" with a double quote, and adding a space after the ampersand:
C#
string newfile = string.Concat("\"& '" + match1[0] + "'", " ", match1[1], "\"");
 
Share this answer
 
Comments
Learner PS 9-Dec-16 3:51am    
No luck still getting the same issue. The code for the same is here http://www.c-sharpcorner.com/forums/uploadfile/2cd341/370588/Test_PS.rar
OriginalGriff 9-Dec-16 4:01am    
If what you show us is exactly what you execute at the command line and it works, then double check the string in ScriptPath against the string - it should now be identical. So try removing the "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe" part from the front and passing the remains as the parameters.
Learner PS 9-Dec-16 4:03am    
The code in my solution is same as I just updated, but still not working can you help me
Learner PS 9-Dec-16 4:16am    
Here is the complete code https://drive.google.com/open?id=0B6o552kozQh4VkdMZ3M5TElvY1E
Michael_Davies 9-Dec-16 4:21am    
Try using && to escape the &, not saying it will work and nothing to hand to test it but worth a try.
Try the following code

C#
string script1 = string.Concat(DirectoryPath, "\\Palindrome.ps1 -param Hello");
var match1 = Regex.Split(script1, @"(?<=\.ps1)\s+");
string ScriptPath = string.Concat(" -ExecutionPolicy Unrestricted -Command ", @"&'" + match1[0] + "'", " ", "" + match1[1] + "");
proc.Arguments = ScriptPath;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900