2016-12-02 45 views
0

powershell.exeパスを指定して、WPFからPowerShellを実行しようとしています。私は取得していますエラーがアンパサンド(&)文字が許可されていないスクリプトを実行中にアンパサンドをエスケープする

で次のコード

を持っています。 &演算子は、将来使用するために予約されています。アンパサンドを二重引用符( "&")で囲んで文字列の一部として渡します。私が直接コピーするときに細かい実行されている

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -Command "& {&'C:\TestPS\Testps1' -Arg1 -Arg2}" 

は、PowerShellのに貼り付けるはなく、WPFアプリケーションから次のように

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(); 
} 

私のスクリプトが見えます。

+0

? –

+0

私はいくつかの点で試してみてください。しかし運はありません – Dotnet

答えて

0

あなたはps1を呼び出す方法を見て、単にps1を実行したいと思うようです。次に「&」は使用しないでください。

直接のようなスクリプトを呼び出す: `:` - コマンド "\ TestPS \ Test.ps1 -arg1 -arg2 C" の何が問題になってい

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -Command "C:\TestPS\Test.ps1 -Arg1 -Arg2" 
関連する問題