将动态值传递给Invoke-Command

时间:2017-08-18 10:01:23

标签: powershell

我正在编写一个脚本来连接远程服务器并获取日志。

这是woking:

foreach ($serv in $server) {
    Invoke-Command {
        Get-WinEvent -FilterHashtable @{
            logname   = 'application';
            starttime = '18/08/2017 10:00:00'
        }
    } -Credential $cred -ComputerName $serv
}

但是当我尝试传递如下动态值时:

$application = "Application"

foreach ($serv in $server) {
    Invoke-Command {
        Get-WinEvent -FilterHashtable @{
            logname   = $application;
            starttime = '18/08/2017 10:00:00'
        }
    } -Credential $cred -ComputerName $serv
}

显示错误:

A null value was encountered in the logname hash table key. Null values are not
permitted.
    + CategoryInfo          : InvalidArgument: (logname:String) [Get-WinEvent], Exception
    + FullyQualifiedErrorId : NullNotAllowedInHashtable,Microsoft.PowerShell.Commands.GetWinEventCommand

0 个答案:

没有答案