【问题标题】:Get-Winevent Filterhashtable Formatting errorGet-Winevent Filterhashtable 格式化错误
【发布时间】:2018-01-23 09:06:59
【问题描述】:

当我尝试运行此脚本时:

$Filename = ""U:\logfile_analysis\raw_data\SavedSecurity.evtx""
$EventIDsLogon.ToString() = "4624"
$EventIDsLogoff.ToString() = "4647"
$EventIDsLogonFailure.ToString() = "4625"
$EventIDsLockScreen.ToString() = "4800"
$EventIDsUnlockScreen.ToString() = "4801"
$EventIDstemp = $EventIDsLogon, $EventIDsLogoff, $EventIDsLogonFailure, $EventIDsLockScreen, $EventIDsUnlockScreen -join ","
$EventIDsSummary = $EventIDstemp.Trim()
#Write-Host $EventIDsSummary
Write-Host "Get-WinEvent -FilterHashtable @{Path='$Filename'; ID=$EventIDsSummary}"
pause
Get-WinEvent -FilterHashtable @{Path='$Filename'; ID=$EventIDsSummary}

并查看来自

的输出
Write-host "Get-WinEvent -FilterHashtable @{Path='$Filename'; ID=$EventIDsSummary}"

输出是:

Get-WinEvent -FilterHashtable @{Path='U:\logfile_analysis\raw_data\SavedSecurity.evtx'; ID=4624,4647,4625,4800,4801}

当我将输出从 Write-Host 复制到 PowerShell 控制台时,它可以工作:

PS> Get-WinEvent -FilterHashtable @{Path='U:\logfile_analysis\raw_data\SavedSecurity.evtx'; ID=4624,4647,4625,4800,4801} ProviderName:Microsoft-Windows-Security-Auditing TimeCreated Id LevelDisplayName 消息 ----------- -- ---------------- -------- 04.12.2017 13:56:56 4624 Informationen Ein Konto wurde erfolgreich angelmeldet... 04.12.2017 13:56:56 4647 Informationen Benutzerinitiierte Abmeldung:... 04.12.2017 13:56:48 4801 Informationen Die Arbeitsstation wurde entsperrt... 04.12.2017 13:56:48 4624 Informationen Ein Konto wurde erfolgreich angelmeldet... 04.12.2017 13:56:48 4624 Informationen Ein Konto wurde erfolgreich angelmeldet... ******** 截断**

但是:

Get-WinEvent -FilterHashtable @{Path='$Filename'; ID=$EventIDsSummary}

没用。

错误信息是:

Get-WinEvent : 找不到路径“U:\logfile_analysis\$Filename”,因为它不存在。

我尝试将"" 添加到@{Path="$Filename"...。 我尝试在@Path="$Filename"... 添加''。 我试图操纵$Filename variable and add"", the variable$Filename` 的样子

$Filename = '"U:\logfile_analysis\raw_data\SavedSecurity.evtx"'
$Filename = ""U:\logfile_analysis\raw_data\SavedSecurity.evtx""
$Filename = "'U:\logfile_analysis\raw_data\SavedSecurity.evtx'"

没有成功。

更深入的了解会发现问题,@Path='$Filename' 路径必须在两个“”之内,我怎样才能将它们添加到脚本有效?

【问题讨论】:

  • 就像$Filename="U:\logfile_analysis\raw_data\SavedSecurity.evtx" ; Get-WinEvent -FilterHashtable @{Path=$Filename; ID=$EventIDsSummary}一样简单
  • 除了 Michel 所说的之外,您还需要将键 ID 的值设为实际数组,而不是逗号分隔的字符串:$EventIDsSummary = 4624, 4647, 4625, 4800, 4801。此外,$var.ToString() = "..." 不可能工作,应该给你一个“不能在空值表达式上调用方法”错误。
  • 感谢您的提示。创建数组EventIDstemp=@($EventIDsLogon, $EventIDsLogoff, $EventIDsLogonFailure, $EventIDsLockScreen, $EventIDsUnlockScreen -join ",") 并运行Get-WinEvent -FilterHashtable @{Path=$Filename; ID=$EventsIDstemp} 时,我无法让它运行。不知道,为什么。 echo $EventIDstemp 给出4624,4647,4625,4800,4801,错误消息是Get-WinEvent : A null value was encountered in the ID hash table key. Null values are not permitted.

标签: powershell


【解决方案1】:

感谢@/lo%c3%afc-michel 和@ansgar-wiechers 的提示。我想我用以下代码解决了它:

$Filename="c:\temp"
$EventIDsLogon="4624"
$EventIDsLogoff="4647"
$EventIDsLogonFailure="4625"
$EventIDsLockScreen="4800"
$EventIDsUnlockScreen="4801"
$EventIDstemp=@($EventIDsLogon,$EventIDsLogoff,$EventIDsLogonFailure,$EventIDsLockScreen, $EventIDsUnlockScreen) 
echo $Filename
echo $EventIDstemp
Get-WinEvent -FilterHashtable @{Path=$Filename; ID=$EventIDstemp}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多