2012-04-13 137 views
0

我想運行PowerShell腳本並嘗試按消息過濾。PowerShell錯誤使用GetEventLog CmdLet

param($server,$message) 
Try 
{ 
Invoke-Command -computername $server {Get-Eventlog -logname application -source "source" -message $message | Format-List} 
} 
Catch [Exception] 
{ 
Write-Host $_.Exception.ToString() 
} 

試圖用下面的參數

運行腳本GetEventLog.ps1 「服務器名稱」, 「TEXT_TO_FIND」

無法驗證的參數 '消息' 的說法。參數爲空或空。提供一個非空的參數或 爲空,然後再次嘗試該命令。 + CategoryInfo:InvalidData:(:) [獲取,事件日誌],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetEventLogCommand

出於某種原因,處理$服務器參數正常,但如果抱怨有關$消息變量。

任何線索?

+0

Get-EventLog太慢了!!我結束使用這個.. Get-WinEvent -computername $ server -FilterHashTable @ {LogName ='application'; providername = $ provider} | Where-Object {$ _。Message -match $ message -And $ _。TimeCreated -ge $ after -And $ _。TimeCreated -le $ before} – Maverick 2012-04-18 19:48:12

回答

0

您需要使用-ArgumentList參數將$ message作爲參數傳遞。 CHECK OUT例如9手冊頁:

http://technet.microsoft.com/en-us/library/dd347578.aspx

invoke-command -computername server01 -scriptblock {param($log, $num) get-eventlog -logname $log -newest $num} -ArgumentList $MWFO_Log, 10 
+0

必須嘗試此操作,但絕對不像Shay Levy那樣清晰和簡單。爲什麼我需要使用scriptblock ?.我想如果我不使用invoke-command,那麼我不需要這種方法。 :) – Maverick 2012-04-13 20:02:18

+0

@Maverick是的,它恰好如此,Get-EventLog cmdlet已經支持遠程執行,所以不需要使用Invoke-Command。 – 2012-04-13 20:05:13

1

試試這個方法:

Invoke-Command -computername $server {Get-Eventlog -logname application -source "source" -message $args[0] | Format-List} -ArgumentList $message 
+0

這沒有奏效 – Maverick 2012-04-13 20:01:00

+0

你有沒有同樣的錯誤? – 2012-04-13 20:05:53

+0

這不是@ShayLevy的解決方案,但無論如何,它解決了您發佈的錯誤。 – 2012-04-13 20:41:00

3

爲什麼使用調用命令時,你可以得到的事件沒有它?

Get-EventLog -ComputerName $server -LogName application -source "source" -message $message 

萬一命令生成您將無法趕上它,因爲它可能不會終止錯誤的錯誤。要使錯誤終止,請使用ErrorAction參數:

Get-EventLog -ComputerName $server -LogName application -ErrorAction Stop ... 
+0

好電話謝伊! – 2012-04-13 19:52:29

+0

謝謝@AndyArismendi :) – 2012-04-13 19:57:45

+0

這實際上工作! – Maverick 2012-04-13 20:00:43

相關問題