【发布时间】: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 控制台时,它可以工作:
但是:
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