PowerShell 5在

PowerShell 5在

问题描述:

中传递日期时间参数

运行以下ps1,但datetime参数在出错时重新执行。PowerShell 5在

调试显示的日期是正确的($早日重启= I输入的日期)

的主机IP参数被传递,因此似乎有必要传递,我不知道的日期时间一个特殊的技巧。

我收到的错误是:

无法在参数验证参数“后”。参数为空或 为空。提供非空或空的参数,然后再次尝试 命令。

任何帮助,非常感谢。

param( 
    [Parameter(Mandatory=$True)] 
    [string]$HostIP, 

    [Parameter(Mandatory=$True)] 
    [datetime]$earlydate, 

    [Parameter(Mandatory=$True)] 
    [datetime]$latedate 
) 

Invoke-Command {Get-EventLog Security -After $earlydate -Before $latedate} -ComputerName $HostIP | 
Export-Csv c:\Events\$HostIP.csv 

Invoke-Command {Get-EventLog Security -After $using:earlydate -Before $using:latedate} 

$早日重启& $ latedate变量是你的机器上定义没有远程机器上。远程机器不会自动继承变量,因此远程机器上的变量将为空。

+0

完美!这工作,非常感谢你的反应如此之快! –

你的剧本是寻找一个DateTime类型,我想你传递一个字符串。如果你想在一个字符串传递它转换成datetime自己的函数中的DateTime对象,否则传如:

(Get-Date -year 2017 -month 9 -day 15) 
+0

谢谢你回复约翰,我从其他问题看过这个选项,但无法让它为我工作。当然我会继续学习PowerShell。 –