使用参数在服务器上调用VBS

时间:2016-06-30 06:54:58

标签: powershell vbscript

我想使用PowerShell脚本调用位于服务器上的VBScript,我想使用invoke-command在VBS中传递参数。

代码:

$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

但是我有一个由VBS中的参数引起的问题,在这些方面:

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

PowerShell脚本返回此错误:

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

第64行是User = WScript.Arguments(0)

我的VBS似乎无法识别我传递的参数,所以我认为问题来自PS脚本,我也尝试传递这样的参数:

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

它不起作用。

1 个答案:

答案 0 :(得分:1)

您正在将参数传递到scriptblock中,但您永远不会使用它们。

改变这个:

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

这样的事情:

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

或者这个:

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