Tomafa06
Tomafa06

Reputation: 140

Call VBS on a server with parameters

I want to call a VBScript located on a server with a PowerShell script, I want to pass parameters in the VBS by using invoke-command.

Code:

$username = [Environment]::UserName # Get the current user name
# Get with a Credential the logins to connect to an AS400 Server
$credential = $host.ui.PromptForCredential("Login", "Please enter your ID.", "", "")
$ASUser = $credential.Username.Substring(1) 
$ASPwrd = $credential.GetNetworkCredential().password 

# Call of the VBS on the server with these parameters
Invoke-Command -ComputerName MyServerName -ScriptBlock {
  CSCRIPT "C:\MyScript.vbs"
} -ArgumentList $username, $ASUser, $ASpwrd

But I've got an issue caused by the parameters in the VBS, at these lines:

User = WScript.Arguments(0)
UID = WScript.Arguments(1)
PWD = WScript.Arguments(2)

The PowerShell script returns me this error:

C:\MyScript.vbs(64, 1) Microsoft VBScript runtime error: Subscript out of range
    + CategoryInfo          : NotSpecified: (C:\Excel Script...pt out of range:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

The line 64 is User = WScript.Arguments(0).

It seems that my VBS does not recognize that I pass parameters, so I think the problem comes from the PS script, I also tried to pass the parameters like this:

Invoke-Command -ComputerName MyServerName -ScriptBlock {
  CSCRIPT "C:\MyScript.vbs" $username $ASUser $ASpwrd
}

It does not work.

Upvotes: 0

Views: 1458

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200443

You're passing arguments into the scriptblock, but you never use them.

Change this:

Invoke-Command -ComputerName MyServerName -ScriptBlock {
  CSCRIPT "C:\MyScript.vbs"
} -ArgumentList $username, $ASUser, $ASpwrd

into something like this:

Invoke-Command -ComputerName MyServerName -ScriptBlock {
  CSCRIPT "C:\MyScript.vbs" $args[0] $args[1] $args[2]
} -ArgumentList $username, $ASUser, $ASpwrd

or this:

Invoke-Command -ComputerName MyServerName -ScriptBlock {
  Param($user, $user2, $pass2)
  CSCRIPT "C:\MyScript.vbs" $user $user2 $pass2
} -ArgumentList $username, $ASUser, $ASpwrd

Upvotes: 1

Related Questions