使用C#Process.start()通过参数运行Powershell脚本 [英] Running Powershell Scripts with arguments using C# Process.start()
问题描述
这是我的脚本文件.它显示所有传递给它的参数及其计数.
ScriptwithArguments.ps1
=======================
参数:$($ args.count)
$ args
我正在尝试从C#Process.Start()运行此脚本
如果我说
Process.Start("path \ to \ Powershell.exe",@""ScriptwithArguments.ps1"""arg1"""arg2"")
它工作正常.它说有2个参数并显示它们.
arg1
arg2
但是,当我尝试使用带有空格的参数执行同一脚本时,即使我将它们放在双引号中,也会将它们作为不同的参数使用.
示例:
Process.Start("path \ to \ Powershell.exe",@""ScriptwithArguments.ps1""带有空间的arg"));
它说有3个论点.
arg
与
空间
请在这里帮助我...!
This is my Script file. It displays all the arguments that are passed to it and their count.
ScriptwithArguments.ps1
========================
Arguments: $($args.count)
$args
I''m trying to run this script from C# Process.Start()
If I say
Process.Start("path\to\Powershell.exe",@"""ScriptwithArguments.ps1"" ""arg1"" ""arg2""")
it works fine. It says there are 2 arguments and displays them.
arg1
arg2
But when I try to execute the same script with arguments that have spaces in between, it takes those as different arguments even though I put them inside doublequotes.
Example:
Process.Start("path\to\Powershell.exe",@"""ScriptwithArguments.ps1"" ""arg with space""");
It says there are 3 arguments.
arg
with
space
Please help me here...!
推荐答案
(
args.count)
args.count)
args
我正在尝试从C#Process.Start()运行此脚本
如果我说
Process.Start("path \ to \ Powershell.exe",@""ScriptwithArguments.ps1"""arg1"""arg2"")
它工作正常.它说有2个参数并显示它们.
arg1
arg2
但是,当我尝试使用带有空格的参数执行同一脚本时,即使我将它们放在双引号中,也会将它们作为不同的参数使用.
示例:
Process.Start("path \ to \ Powershell.exe",@""ScriptwithArguments.ps1""带有空间的arg"));
它说有3个论点.
arg
与
空间
请在这里帮助我...!
args
I''m trying to run this script from C# Process.Start()
If I say
Process.Start("path\to\Powershell.exe",@"""ScriptwithArguments.ps1"" ""arg1"" ""arg2""")
it works fine. It says there are 2 arguments and displays them.
arg1
arg2
But when I try to execute the same script with arguments that have spaces in between, it takes those as different arguments even though I put them inside doublequotes.
Example:
Process.Start("path\to\Powershell.exe",@"""ScriptwithArguments.ps1"" ""arg with space""");
It says there are 3 arguments.
arg
with
space
Please help me here...!
这篇关于使用C#Process.start()通过参数运行Powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!