2017-09-15 20 views
0
中傳遞日期時間參數

運行以下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 

回答

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

$早日重啓& $ latedate變量是你的機器上定義沒有遠程機器上。遠程機器不會自動繼承變量,因此遠程機器上的變量將爲空。

+0

完美!這工作,非常感謝你的反應如此之快! –

0

你的劇本是尋找一個DateTime類型,我想你傳遞一個字符串。如果你想在一個字符串傳遞它轉換成datetime自己的函數中的DateTime對象,否則傳如:

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

謝謝你回覆約翰,我從其他問題看過這個選項,但無法讓它爲我工作。當然我會繼續學習PowerShell。 –