SYS320 Week 10 Lab - JadenGil/Jaden-Tech-Journal GitHub Wiki
#Storyline: View the event logs, check for a valid log, and print the results
function select_log() {
cls
`#list all event logs`
`$theLogs = Get-EventLog -list | select Log`
`$theLogs | Out-Host`
`# initialize the arry to store Logs`
`$arrLog = @()`
`foreach ($tempLog in $theLogs) {`
`#add eacg log to the array`
`#NOTE: These are stored in the array as a hashtable in the format:`
`# @{Log=LOGNAME}`
`$arrLog += $tempLog`
`}`
`# Test to be sure the array is being populated`
`Write-Host $arrLog[0]`
`#Prompt the user for the log to view or quit`
`$readLog = Read-Host -Prompt "Please enter a log from the list above or 'q' to quit the program" `
`# Check if the user wants to quit`
`if ($readLog -match "^[qQ]$") {`
`# Stop exe the program and close the script`
`break`
`}`
`log_check -logToSearch $readLog`
`$arrLog`
} # ends the select_log()
function log_check() {
`# String the user types within the select_log function`
`Param([string]$logToSearch)`
`# Format the user input`
`#Example: @{Log=glassbottle}`
`$theLog = "^@{Log=" + $logToSearch + "}$"`
`# Search the array for the exact hash table string`
`if ($arrLog -match $theLog){`
`write-host -BackgroundColor DarkGreen -ForegroundColor White "Please wait. It may take a few moments to retrieve the log entries."`
`sleep 2`
`view_log -logToSearch $logToSearch`
`} else {`
`Write-Host -BackgroundColor Red -ForegroundColor White "the log specified does not exist"`
`sleep 2`
`select_log`
`}`
} # ends the log_check()
function view_log() {
`cls`
`# Get the Logs`
`Get-EventLog -Log $logToSearch -Newest 10 -After "1/18/2023"`
`#Pause the screen and wait til the user is ready to be proceed`
`Read-Host -Prompt "Press 'enter' when you are done."`
`# Go back to select_log`
`select_log `
} # ends the view_log()
# Run the select_log as the first function
select_log