Call VBScript With Return Value on Remote Machine With Powershell

14,159

Solution 1

This is an old question but I would like to share my solution. It's the same as the one posted by Ansgar but it's been tested and working fine:

$VNC = '\\share\software\AppName\_Install_Silent.vbs'
$Computer = 'RemoteHost'
$TMP = "\\$Computer\c$\TEMP"

if (!(Test-Path $TMP)) {
    New-Item -Path $TMP -ItemType Directory
}

Copy-Item -LiteralPath (Split-Path $VNC -Parent) -Destination $TMP -Container -Recurse -Force -Verbose
$LocalPath = Join-Path 'C:\TEMP' (Join-Path (Split-Path $VNC -Parent | Split-Path -Leaf) (Split-Path $VNC -Leaf))
Invoke-Command -ScriptBlock {cscript.exe $Using:LocalPath} -Computer $Computer

# Restart might be needed of remote host

The difference is that you have to copy the files first to the remote machine to avoid the double hop issue and then you can install it with the $Using variable.

Hope this helps someone.

Solution 2

I'd probably try either remote invocation:

Invoke-Command -ScriptBlock { cscript.exe "C:\test.vbs" } -Computer $computer

or PsExec:

PsExec \\$computer cscript.exe "C:\test.vbs"

Can't test either of them right now, though.

Share:
14,159
paulioc
Author by

paulioc

Updated on June 04, 2022

Comments

  • paulioc
    paulioc almost 2 years

    I need to call a remote VB script from Powershell, and the VB script needs to run on the remote machine.

    I have been using \$computer\root\cimv2:Win32_Process").Create(C:\test.vbs)

    This works, however I can't get a return value from the script, just a return value from the win32 process.

    I would convert the whole thing to powershell, but can't as I'm connecting to a legacy domain I can't install additional tools on so have to call the remote vbscript