TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Devi
NA
55
16.2k
Escape ampersand while executing the script
Dec 8 2016 5:42 AM
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.
Reply
Answers (
8
)
Split and sum array like below
Error in visual studio project