使用C#Process.start()通过参数运行Powershell脚本 [英] Running Powershell Scripts with arguments using C# Process.start()

查看:589
本文介绍了使用C#Process.start()通过参数运行Powershell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的脚本文件.它显示所有传递给它的参数及其计数.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆