【发布时间】:2019-02-03 11:04:03
【问题描述】:
我是 Powershell 的新手,我发现很难告诉我的第一个 powershell 命令行 (powershell_1):启动一个新的 powershell 命令行 (powershell_2) 并让它 (powershell_2) 执行我的多行脚本(在 powershell 下编写)。
我的脚本是:
$wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop;
$wshell.Popup("Are you looking at me?",0,"Hey!",48+4);
我用来启动第一个 powershell 命令行并按预期工作的代码
Process powershell_1 = new Process();
powershell_1.StartInfo.FileName = "powershell";
string argPowershell_2 = "help";
powershell_1.StartInfo.Arguments = "Start-Sleep -Milliseconds 500; start powershell -argumentlist \" "+ argPowershell_2 + " \"";
MessageBox.Show(powershell_1.StartInfo.Arguments);
powershell_1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
powershell_1.Start();
在上面的示例中,我可以告诉第二个命令行在第二个命令行中执行帮助。我正在寻找一种逃避引号问题并以正确方式告诉它如何执行 myscript 的好方法。
编辑
Ps:我不想将 someScript.ps 的路径传递给第二个 powershell 命令行。我想以多行格式逐字传递。
【问题讨论】:
-
但是为什么呢?只需使用脚本块和作业
-
您可以使用
Invoke-Expression并将其传递给您要运行的脚本的路径以及参数(如果您要这样做的话)。 -
我编辑了我的帖子。我不想将脚本存储在 .ps 文件中。
标签: c# powershell escaping quoting