[英]powershell filterhashtable credential
我在向我的代碼字符串添加憑證時遇到問題。 這樣做的目的是從一台計算機上提取多個日志,並按時間順序打印出日志。 由於某種原因,一旦添加-credential,我將永遠無法使get-winevent命令正常工作。 歡迎任何輸入!
$creds = Get-Credential -Message "Please enter creds"
$Startdate = Read-Host -Prompt "Input your start date in the format of mm/dd/yyyy hh:mm:ss am"
Try{
[DateTime]::Parse($Startdate, [System.Globalization.CultureInfo]::GetCultureInfo("en-US"))
}
Catch{
Write-Host "This time format is incorrect."
}
$Enddate = Read-Host -Prompt "Input your end date in the format of mm/dd/yyyy hh:mm:ss am"
Try{
[DateTime]::Parse($Enddate, [System.Globalization.CultureInfo]::GetCultureInfo("en-US"))
}
Catch{
Write-Host "This time format is incorrect."
}
$Logs = @()
do{
$input = (Read-Host "Please enter in the name of a log")
if($input -ne'') {$Logs += $input}
}
until($input -eq '')
$table = foreach ($Log in $Logs)
{
Get-WinEvent -FilterHashtable @{LogName=$Log;StartTime=$Startdate;EndTime=$Enddate} -Credential $creds
}
$table | sort TimeCreated | Format-Table TimeCreated, Logname, Source, Message -wrap
我目前收到的錯誤。
Get-WinEvent:嘗試執行未經授權的操作。 在第40行:char:5 + Get-WinEvent -FilterHashtable @ {LogName = $ Log; StartTime = $ Startdate ... + ~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + CategoryInfo:未指定:(: )[Get-WinEvent],UnauthorizedAccessException + FullyQualifiedErrorId:System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWinEventCommand
我認為錯誤是由於沒有為-FilterHashtable
提供$Startdate
和$Enddate
正確的數據類型而$Enddate
。 您檢查用戶輸入的格式是否有效,但變量本身仍為字符串。 -FilterHashtable
要求這些參數為DateTime
對象,如下表所示:
Key name Value data type
--------------- ---------------
LogName <String[]>
ProviderName <String[]>
Path <String[]>
Keywords <Long[]>
ID <Int32[]>
Level <Int32[]>
StartTime <DateTime>
EndTime <DateTime>
UserID <SID>
Data <String[]>
嘗試這個:
$creds = Get-Credential -Message "Please enter creds"
# Create variable for parsed start date
[datetime]$Startdate = Get-Date
do {
$input = Read-Host -Prompt "Enter your start date. Use format 'mm/dd/yyyy hh:mm:ss am'"
# Check the user input
$success = ([DateTime]::TryParse($input,
[System.Globalization.CultureInfo]::GetCultureInfo("en-US"),
[System.Globalization.DateTimeStyles]::None,
[ref]$Startdate))
} while (!$success)
# Create variable for parsed end date
[datetime]$Enddate = Get-Date
do {
$input = Read-Host -Prompt "Enter your end date. Use format 'mm/dd/yyyy hh:mm:ss am'"
# Check the user input
$success = ([DateTime]::TryParse($input,
[System.Globalization.CultureInfo]::GetCultureInfo("en-US"),
[System.Globalization.DateTimeStyles]::None,
[ref]$Enddate))
} while (!$success)
$Logs = @()
while ($true) {
$logName = Read-Host -Prompt "Please enter in the name of a log"
if ([string]::IsNullOrEmpty($logName)) { break }
$Logs += $logName
}
$table = foreach ($Log in $Logs) {
# note that we use [DateTime] objects $Startdate and $Enddate
Get-WinEvent -FilterHashtable @{LogName=$Log;StartTime=$Startdate;EndTime=$Enddate} -Credential $creds
}
$table | Sort-Object TimeCreated | Format-Table TimeCreated, Logname, Source, Message -Wrap
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.