Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Instructional demonstration
- .DESCRIPTION
- This script demonstrates the following:
- How to structure a switch statement
- How to nest a switch statement in a while loop
- How to use exception handling for general and specific error types
- How to write and document a function
- How to implement Comment Based Help
- .INPUTS
- None
- .OUTPUTS
- Outputs the selected option to the screen
- .EXAMPLE
- PS> ......
- .NOTES
- Author:
- StudentID:
- Date:
- #>
- Function Get-UserInput{
- <#
- .SYNOPSIS
- Function to display menu and collect user input
- .DESCRIPTION
- The function uses a StringBuilder to build and display
- the menu that is presented to the user. It then returns
- the results of the Read-Host command.
- #>
- $Menu = New-Object -TypeName System.Text.StringBuilder
- [void]$Menu.AppendLine("... Task 1 ...")
- [void]$Menu.AppendLine("-------------------")
- [void]$Menu.AppendLine("1. This will list all log files in requirements1 folder, with current date preceeding.")
- [void]$Menu.AppendLine("2. This will list all files in requirements1 folder in tabular format, ascending alphabetical order, in a file called c916contents.txt")
- [void]$Menu.AppendLine("3. This will current CPU and Memory Usage.")
- [void]$Menu.AppendLine("4. This will list all running processes within system, by virtual size least to greatest, in grid format.")
- [void]$Menu.AppendLine("5. Exit Script.")
- [void]$Menu.AppendLine("6. This will cause an error on purpose to test exception handling")
- [void]$Menu.AppendLine("-------------------")
- Write-Host -ForegroundColor Cyan $Menu.ToString()
- return $(Write-Host -ForegroundColor Cyan ">> Select a command (1-6):"; Read-Host)
- }
- #Variable to hold user input
- $UserInput = 1
- Try{
- while ($UserInput -ne 6)
- {
- # Prompt for User Input
- $UserInput = Get-UserInput
- # Run specfic code based on User Input
- switch ($UserInput)
- {
- 1 # User chose option 1
- {
- #This will list all log files in requirements1 folder, with current date preceeding
- Write-Host "Attempting to run Option 1"
- "Timestamp: " + (Get-Date) | Out-File -FilePath $PSScriptRoot\DailyLog.txt -Append
- Get-ChildItem -Path $PSScriptRoot -Filter *.log | Out-File -FilePath $PSScriptRoot\DailyLog.txt -Append
- }
- 2 # User chose option 2
- {
- #This will list all files in requirements1 folder in tabular format, ascending alphabetical order, in a file called c916contents.txt
- Get-ChildItem "$PSScriptRoot" | Sort-Object Name | Format-Table -AutoSize -Wrap | Out-File "$PSScriptRoot\C916Contents.txt"
- }
- 3 # User chose option 1
- {
- #This will current CPU and Memory Usage
- $CounterList = "\Processor(_Total)\% Processor Time", "\Memory\Committed Bytes"
- Get-Counter -Counter $CounterList -MaxSamples 4 -SampleInterval 5
- }
- 4
- {
- #This will list all running processes within system, by virtual size least to greatest, in grid format
- Get-Process | Select-Object ID, Name, VM | Sort-Object Name | Out-GridView
- }
- 5 # User chose option 5
- {
- #Quit
- Write-Host -ForegroundColor Red "Exiting Script"
- }
- }
- }
- }
- #Catch a specific error type
- Catch[System.OutOfMemoryException]
- {
- Write-Host -ForegroundColor Red "An Error Occurred."
- Write-Host -ForegroundColor Red "$($PSItem.ToString())'n'n$($PSItem.ScriptStackTrace)"
- }
- Finally
- {
- # Close any open resources
- # This code runs right before script ends
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement