JakeRobinson
JakeRobinson

Reputation: 298

Alternative to running external programs in Powershell

In attempting to run an external program in Powershell, the only way I have been able to get this particular line to run is with [Diagnostics.Process]::Start(). However, this does not seem to work in older OSes, like Windows XP. Is there another alternative? I have attempted an Ampersand (&) type of run, but it jacks up my arguments. Here's what I have:

$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)

Upvotes: 1

Views: 1677

Answers (3)

OldFart
OldFart

Reputation: 2479

I've found this little C program useful in determining what PowerShell does to arguments passed to an external program. (Compile it for 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;
}

It your original example, it might have worked if you had invoked the command as:

& $vmrc -h $($server) -p $unencoded -M $moref

I say "might" because I don't know what is in $unencoded and $moref.

Trying to invoke it as:

& $vrmr $vmrcArgs

would have resulted in the value of $vmrcArgs being enclosed in quotes in the command line to the external app so the app would likely interpret it as a single argument. Play around with the C program some and you'll see how PowerShell tries to make intellegent decisions on quoting things when it generates the command line for the external app.

(If your intention was to run a detached process then this answer is inappropriate. Sorry if that is the case.)

Upvotes: 1

ravikanth
ravikanth

Reputation: 25820

Start-Process should also work.

$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)"

Upvotes: 5

codepoke
codepoke

Reputation: 1282

I've had good luck using invoke-expression to run Robocopy, for example, with loads of command line parameters.

$cmd = "$vmrc$vmrcArgs"
$out = invoke-expression $cmd

Upvotes: 1

Related Questions