我有一个脚本应该从用户读取输入,并将其传递给脚本的函数。该值应该是System.Int64类型,即使我这样抛出它,我仍然会收到以下错误:
Get-WinEvent:无法绑定参数'MaxEvents'。无法将值“$ b”转换为“System.Int64”类型。错误:“输入字符串的格式不正确。”
我添加了一行来输出变量的类型:
Write-Host "Type of MaxEntries: " $maxEntries.GetType()
它表明它是System.Int64,但我仍然得到同样的错误。此外,如果我从函数中的实际命令中的变量周围删除双引号,我会收到此错误:
Get-WinEvent:无法验证参数'MaxEvents'的参数。参数为null,为空,或者参数集合的元素包含空值。 提供不包含任何空值的集合,然后再次尝试该命令。
因此在这种情况下它被传递为null。奇怪的是,我写出了函数外部和内部存储在MaxEntries变量中的值,它正确地显示了我输入的值。
以下是剧本:
$DesktopPath = [Environment]::GetFolderPath("Desktop")
$path = "$DesktopPath\Hashes.txt"
$apiKey = Read-Host -Prompt "Enter your VT api key"
$password = Read-Host -Prompt "Enter your VT account password" | ConvertTo-SecureString -AsPlainText -Force
Set-VTAPIKey -APIKey $apiKey -MasterPassword $password
$PatternMD5 = '(?<=MD5=)(.*)(?=,)'
$PatternSHA256 = '(?<=SHA256=)(.*)'
#try{
$days = Read-Host -Prompt "How far back would you like to search the logs (days)?"
#} catch {
#until
#} (($days -ge 1 -and $days -lt 100))
$daysBack = (Get-Date).AddDays(-$days)
#try{
[int64]$maxEntries = Read-Host -Prompt "What is the maximum number of log events you would like to retrieve? (1-100)?"
#} catch{
#until
#} (($maxEntries -ge 1 -and $MaxEntries -lt 100))
Write-Host "MaxEntries BEFORE function: " $maxEntries
function GetHashes($a, $b){
Write-Host "daysBack: " $daysBack
Write-Host "MaxEntries AFTER function: " $maxEntries
Write-Host "Type of MaxEntries: " $maxEntries.GetType()
$events = Get-WinEvent -FilterHashtable @{logname="Microsoft-Windows-Sysmon/Operational";StartTime = $a} -MaxEvents $b
ForEach ($event in $events) {
if ($event.Message.Contains("Hashes")){
Write-Host "Event contains hashes"
# Create hashtable
$Dictionary = @{}
# Convert event message to string
$string = $event.Message.ToString()
# Split lines in event message based off newline, and remove any null values
$string.Split([environment]::NewLine,[System.StringSplitOptions]::RemoveEmptyEntries) | ForEach-Object {
#Split each pair into key and value based off ':' delimiter
$key,$value = $_.Split(':')
#Write-Host "Key: " $key
#Write-Host "Value: " $value
#Populate $Dictionary
$Dictionary[$key] = $value
}
Write-Host "Hashes: " $Dictionary.Item("Hashes")
$hashes = $Dictionary.Item("Hashes")
$MD5Hash = ($hashes -match $patternMD5)
$MD5Hash = $matches[0]
$SHA256Hash = ($hashes -match $patternSHA256)
$SHA256Hash = $matches[1]
$SHA256Hash >> $path
$MD5Hash >> $path
#Write-Host "MD5 Hash is: " $MD5Hash
#Write-Host "SHA256 Hash is: " $SHA256Hash
Write-Host
}
}
}
GetHashes $daysBack, $maxEntries
Submit-VTFile $path
我想提示用户返回最大数量的日志条目$maxEntries
,然后将该值传递到最终在此命令中使用的function GetHashes($a, $b)
:
$events = Get-WinEvent -FilterHashtable @{logname="Microsoft-Windows-Sysmon/Operational";StartTime = $a} -MaxEvents $b
我有一种直觉,认为这是一种非常愚蠢和幼稚的东西,但我很欣赏一些推特或链接到一些有益的阅读材料。
我怀疑以下内容: