Skip to content

Instantly share code, notes, and snippets.

@Jackbennett
Created May 24, 2017 11:58
Show Gist options
  • Save Jackbennett/3fc155773f96e909d1554a35f0a245ee to your computer and use it in GitHub Desktop.
Save Jackbennett/3fc155773f96e909d1554a35f0a245ee to your computer and use it in GitHub Desktop.
A waste of 10 minutes. Know what you're doing with try/catch
# By adding a credential parameter to my function I broke it and couldn't spot the fault for 10 mintues
# Rookie Mistake. Beware poor try/catch use.
try {
# Grab the events from a remote computer
$EventLog = Get-WinEvent -ComputerName $ComputerName -FilterHashtable -Credential:$Credential @{
Logname = 'Security';
Id = 4624;
StartTime = $StartDay.toShortDateString();
EndTime = $StopDay.toShortDateString();
} -ErrorAction Stop
} catch {
switch ($ComputerName)
{
'localhost' { Write-Error "Must run as an Administrator to acces logs for $ComputerName" }
Default { Write-Error "$ComputerName cannot be found" }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment