Domanda

Sto costruendo una funzione PowerShell in modo da poter interrogare più registri di eventi di computer.Ive ha copiato il codice qui sotto che ho bisogno di aiuto con.Se non invii un eventid, voglio che lo script trovi tutti gli ID eventi, come posso ottenere questo?

#this would be the parameter part of the script
[String]$ComputerName = $env:COMPUTERNAME#Current computer 
[String[]]$EventLogNames=@("Application","System")#Main eventlogs 
[int[]]$EventIds = 1 #Event Ids 
[System.DateTime[]]$EventStartDate = (((Get-Date).addDays(-2)).date)#date 10 days ago 
[System.DateTime[]]$EventEndTime = (Get-Date)#date 10 days ago 

#This fits in the process section
$EventCritea = @{logname = $EventLogNames; StartTime=$EventStartDate; EndTime=$EventEndTime; ID=$EventIds}
Get-WinEvent -ComputerName $ComputerName -FilterHashTable $EventCritea  -ErrorAction SilentlyContinue
.

È stato utile?

Soluzione

Primo, il tuo altro Domanda corregge ilTipi di $EventStartDate e $EventEndTime.

Per questa domanda: costruire l'hashtable in modo incrementale:

$filter = @{logname = $EventLogNames; StartTime=$EventStartDate; EndTime=$EventEndTime}

if ($EventIds -ne $null -and $EventIds.Length -gt 0) {
  $filter.ID=$EventIds
}

Get-WinEvent -ComputerName $ComputerName -FilterHashTable $filter #...
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top