I'm having issues adding credentials to my code string. The purpose of this is to pull multiple logs and from a single machine and print out the logs in order of time. For some reason I can never get the get-winevent command to work once I add -credential. Any input is welcomed!
$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
The error I'm currently receiving.
Get-WinEvent : Attempted to perform an unauthorized operation. At line:40 char:5 + Get-WinEvent -FilterHashtable @{LogName=$Log;StartTime=$Startdate ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-WinEvent], UnauthorizedAccessException + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWinEventCommand