Lab 10: Get Service Logs - squatchulator/Tech-Journal GitHub Wiki
logCheck.ps1
# 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
$arrLog = @()
foreach ($tempLog in $theLogs) {
# Add each 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 our 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 executing and close the script
break
}
log_check -logToSearch $readLog
} # ends select_log()
function log_check() {
# String the user types in within the select_log function
Param([string]$logToSearch)
# Format the user input
$theLog = "^@{Log=" + $logToSearch + "}$"
# Search the array for the exact hashtable string
if ($arrLog -match $theLog){
write-host -BackgroundColor Green -ForegroundColor White "Please wait, it may take a few moments to retrieve log entries."
sleep 2
# Call the function to view the log by passing the arguement
view_log -logToSearch $logToSearch
} else {
write-host -BackgroundColor Red -ForegroundColor White "The log specified does not exist."
sleep 2
select_log
}
} # ends log_check()
function view_log() {
cls
Get-EventLog -Log $logToSearch -Newest 10 -After "1/18/2020"
# Pause and wait until the user is ready
read-host -Prompt "Press enter when finished."
# Go back to select_log
select_log
} # ends view_log()
function service_log() {
cls
$services = @('all', 'stopped', 'running')
Write-Host "1. All"
Write-Host "2. Stopped"
Write-Host "3. Running"
# Accept the user input and determine whether they selected option 1,2,3 or to quit.
$input = Read-Host -Prompt "Select an option to view, or enter q to quit"
# Output all services, whether running or stopped
if ($input -eq "1" -or $input -eq "all" -or $input -eq "All") {
Get-Service
}
# Output only stopped services
elseif ($input -eq "2" -or $input -eq "stopped" -or $input -eq "Stopped") {
Get-Service | Where-Object { $_.Status -eq "stopped" }
}
# Output only running services
elseif ($input -eq "3" -or $input -eq "running" -or $input -eq "Running") {
Get-Service | Where-Object { $_.Status -eq "running" }
}
# Quit the program
elseif ($input -match "^[qQ]$") {
break
}
# Triggers when the input is none of the above and starts the function over again (not from the main menu)
else {
Write-Host "That is not a valid input. Please try again."
sleep 2
service_log
}
} # ends service_log()
function menu() {
cls
Write-Host "1. System Logs"
Write-Host "2. Service Logs"
$input = Read-Host -Prompt "Enter the number 1 or 2 to select an option"
if ($input -eq "1"){
select_log
}
elseif ($input -eq "2") {
service_log
}
else {
Write-Host "That is not a valid selection. Please try again."
sleep 2
menu
}
} # ends menu()
# Run the menu function to start the program
menu