PowerShell: Parámetros de función
-
12-12-2019 - |
Pregunta
Estoy construyendo una función de PowerShell para que pueda consultar registros de eventos de múltiples computadoras.He copiado el código a continuación con el que necesito ayuda.Si no envío un evento, quiero que el script encuentre todos los ID de evento, ¿cómo puedo lograr esto?
#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
Solución
primero, su otro Pregunta arregla elTipos de $EventStartDate
y $EventEndTime
.
Para esta pregunta: construir el hashtable incrementalmente:
$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 #...
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow