Tech
Forums
Jobs
Books
Events
Videos
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
8
Answers
Escape ampersand while executing the script
Devi
8y
470
1
Reply
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();
}
My script looks as follows:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -Command
"& {&'C:\TestPS\Testps1' -Arg1 -Arg2}"
which is executing fine when I directly copy paste into PowerShell, but not from the Win froms application.
Post
Reset
Cancel
Answers (
8
)
Next Recommended Forum
Split and sum array like below
Error in visual studio project