将参数传递给远程计算机?

时间:2016-05-26 18:59:01

标签: powershell

我正在尝试使用附加脚本将参数传递给远程计算机。但它抛出异常说:"无法索引到null数组"。我哪里错了?

param(
[Parameter(Mandatory = $True, Position = 0)][ValidateNotNull()]$server = $(throw "Please provide Environment name. Ex: cyclone,jwoww or joker"),
[Parameter(Mandatory = $true, position = 1)][string]$logType = $(throw "Please provide type of log you want"),
[Parameter(Mandatory = $true, position = 2)][string]$howMany = $(throw "Please provide number of log rows you want")
)

invoke-command -computername $server -credential abc\def{

Get-EventLog -logname $arg[0] -newest $arg[1] | Out-File c:\EventLogs.txt 

} -argumentlist $logType,$howMany

1 个答案:

答案 0 :(得分:1)

只需将Param块添加到Invoke-Command cmdlet,然后使用参数名称而不是$arg[0]$arg[1]

invoke-command -computername $server -credential abc\def{
    Param($logType, $howMany)

Get-EventLog -logname $logType -newest $howMany | Out-File c:\EventLogs.txt 

} -argumentlist $logType,$howMany