Problemas com variáveis / expressões do Powershell

0

Estou tentando criar um comando Get-Winevent usando variáveis, mas estou tendo problemas com variáveis dentro de comandos "construídos" e atingi o proverbial muro de tijolos. No último bit de código, se eu remover $EventIDQueryAdd e $EntryTypeQueryAdd , o comando será executado sem problemas. Qualquer ajuda seria muito apreciada! Obrigado!

$ArgLastMinutes = 60
$ArgLogName = "Security"
$ArgEntryType = 0 
$ArgEventID = 4625
if ($ArgEventID) { $EventIDQueryAdd="id=$ArgEventID;" }
if ($ArgEntryType) { $EntryTypeQueryAdd="level=$ArgEntryType;" }

write-host "argeventid "$ArgEventID # returns 4625
write-host "argentrytype "$ArgEntryType # returns 1
write-host "eventidqueryadd "$EventIDQueryAdd # returns id=4625; as it should
write-host "entrytypequeryadd "$EntryTypeQueryAdd # returns level=1; as it should
$LogEntries=Get-WinEvent -FilterHashtable @{logname="$ArgLogName"; $EventIDQueryAdd $EntryTypeQueryAdd StartTime=(Get-Date).AddMinutes(-$ArgLastMinutes) }

... < loop por meio de LogEntries > ...

    
por user433873 02.04.2015 / 09:42

1 resposta

0

Você está tentando construir uma hashtable como uma string, não funcionaria dessa maneira. Quando você diz returns id=4625; as it should não é verdade, você tem uma string aqui, não uma hashtable. Para uma hashtable você deve ver esta saída:

Name    Value
----    -----
Id      4625

Tente isto:

$ArgLastMinutes = 60
$ArgLogName = 'Security'
$ArgEntryType = 0 
$ArgEventID = 4625

# Create a new hashtable with two keys
$Filter = @{
    LogName = $ArgLogName
    StartTime = (Get-Date).AddMinutes(-$ArgLastMinutes)
}

if($ArgEventID)
{
    # Add new key-value pair to the existing hashtable
    $Filter += @{Id = $ArgEventID}
}

if($ArgEntryType)
{
    # Add new key-value pair to the existing hashtable
    $Filter += @{Level = $ArgEntryType}
}

# Pass the hashtable to the -FilterHashtable parameter
$LogEntries = Get-WinEvent -FilterHashtable $Filter
    
por 02.04.2015 / 14:26

Tags