在Powershell中尝试运行外部程序时,我能够运行这行代码的唯一方法是使用[Diagnostics.Process]::Start()。然而,这似乎不适用于较旧的OSes,如Windows XP。还有别的选择吗?我尝试了一种与符号(&)类型的运行,但它阻塞了我的参数。这就是我所拥有的:
$vmrc = "C:\Program Files\Common Files\VMware\VMware Remote Console Plug-in\vmware-vmrc.exe"
$vmrcArgs = "-h $($server) -p $($unencoded) -M $($moref)"
[void] [Diagnostics.Process]::Start($vmrc, $vmrcArgs)发布于 2011-03-17 23:42:28
Start-Process也应该可以工作。
$vmrc = "C:\Program Files\Common Files\VMware\VMware Remote Console Plug-in\vmware-vmrc.exe"
Start-Process -FilePath $vmrc -ArgumentList "-h $($server) -p $($unencoded) -M $($moref)"发布于 2011-03-17 23:23:43
例如,我很幸运地使用invoke-expression来运行Robocopy,并加载了大量的命令行参数。
$cmd = "$vmrc$vmrcArgs"
$out = invoke-expression $cmd发布于 2011-03-18 23:15:27
我发现这个小C程序很有用,可以用来确定PowerShell对传递给外部程序的参数做了什么。(将其编译为UNICODE。)
#include "windows.h"
#include "stdio.h"
int wmain(int argc, wchar_t* argv[])
{
wprintf(L"%s\n", GetCommandLineW());
for (int i=0; i<argc; ++i)
{
wprintf(L"arg[%d]: %s\n", i, argv[i]);
}
return 0;
}在您的原始示例中,如果您按如下方式调用该命令,它可能会起作用:
& $vmrc -h $($server) -p $unencoded -M $moref我说“可能”是因为我不知道$unencoded和$moref中有什么。
尝试以如下方式调用它:
& $vrmr $vmrcArgs会导致$vmrcArgs的值在外部应用程序的命令行中用引号括起来,因此应用程序可能会将其解释为单个参数。尝试使用C程序,您将看到PowerShell在为外部应用程序生成命令行时,如何在引用内容方面做出明智的决策。
(如果您的意图是运行一个分离的进程,那么这个答案是不合适的。如果是这样的话,很抱歉。)
https://stackoverflow.com/questions/5340924
复制相似问题