0

So my script is pretty simple. I have a command that I need to execute with OpenVPN, so I need to cd to that folder and execute: .\openvpn.exe $someCommand.

Problem is, I get this error when I try to use cd or Set-Location:

Set-Location : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Path'. Specified method is not supported.

After searching for a solution I found a workaround with Get-ChildItem -Path $path and then using the $_.Directory attribute, but that didn't work either.

Basically I have something like this:

$openVpnPath = "C:\Program Files\OpenVPN\bin"
Invoke-Command -ScriptBlock {
    Set-Location $openVpnPath,
    ".\openvpn.exe $conf"
}

The location is a regular path. If you're wondering why there's a comma at the end of Set-Location it's because I read that I can execute 2 commands in the same Invoke-Command block. Was I wrong?

Thanks :)

Daniel
  • 621
  • 5
  • 22
  • 1
    What exactly are the extra `".\openvpn.exe $conf"` supposed to do for Set-Location? Or did you perhaps not intend to have that comma at the end of the first command – Lasse V. Karlsen Feb 04 '21 at 17:20
  • Set-Location takes a single string for the -Path parameter. I suspect that you have spaces in the path and the command treats whatever you're passing as multiple paths. In short, put the path in quotes, or just reference .\openvpn.exe with the full path (in quotes too). Btw, I just noticed the scriptblock. Use $using:openVpnPath to reference the variable. – Dan Feb 04 '21 at 17:33
  • 2
    Remove the `,` after `Set-Location $openVpnPath` – Mathias R. Jessen Feb 04 '21 at 17:36
  • I've voted to close as typo. – zett42 Feb 04 '21 at 17:42
  • @LasseV.Karlsen I read that I can execute 2 commands in the same `Invoke-Command` block, hence the comma. It's not a typo :) – Daniel Feb 04 '21 at 18:56
  • You can, you just separate them with a linefeed, so the comma is not needed, instead the comma makes the powershell command parser think the command continues on the next line, so the next command will actually be added as arguments to the first. Take out the comma. – Lasse V. Karlsen Feb 04 '21 at 19:19

1 Answers1

0

Unless you're executing the command remotely, I don't see why Invoke-Command has to be used?

$openVpnPath = "C:\Program Files\OpenVPN\bin"
Set-Location $openVpnPath,
".\openvpn.exe $conf"

The above still won't work:

  • Remove the comma , as per Mathias R. Jessen's comment
  • ".\openvpn.exe $conf" is a string - it'll get printed, as nothing is telling PowerShell to execute it (maybe that's why you're using Invoke-Command?)

One way:

$openVpnPath = "C:\Program Files\OpenVPN\bin"
Set-Location $openVpnPath
.\openvpn.exe $conf

The required to Set-Location is only because you're using the path .\openvpn.exe, where . refers to the current directory... so let's replace that with the OpenVPN directory.

A better way, using call operator (probably the same as dot sourcing here but I think better practice - related)

$openVpnPath = "C:\Program Files\OpenVPN\bin\openvpn.exe"
& $openVpnPath $conf
G42
  • 9,791
  • 2
  • 19
  • 34