Advertisement
Guest User

Untitled

a guest
Jan 16th, 2022
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1.  
  2.  
  3. <#
  4. .SYNOPSIS
  5. Instructional demonstration
  6.  
  7. .DESCRIPTION
  8. This script demonstrates the following:
  9. How to structure a switch statement
  10. How to nest a switch statement in a while loop
  11. How to use exception handling for general and specific error types
  12. How to write and document a function
  13. How to implement Comment Based Help
  14.  
  15. .INPUTS
  16. None
  17.  
  18. .OUTPUTS
  19. Outputs the selected option to the screen
  20.  
  21. .EXAMPLE
  22. PS> ......
  23.  
  24. .NOTES
  25. Author:
  26. StudentID:
  27. Date:
  28. #>
  29.  
  30. Function Get-UserInput{
  31. <#
  32. .SYNOPSIS
  33. Function to display menu and collect user input
  34. .DESCRIPTION
  35. The function uses a StringBuilder to build and display
  36. the menu that is presented to the user. It then returns
  37. the results of the Read-Host command.
  38. #>
  39.  
  40. $Menu = New-Object -TypeName System.Text.StringBuilder
  41. [void]$Menu.AppendLine("... Task 1 ...")
  42. [void]$Menu.AppendLine("-------------------")
  43. [void]$Menu.AppendLine("1. This will list all log files in requirements1 folder, with current date preceeding.")
  44. [void]$Menu.AppendLine("2. This will list all files in requirements1 folder in tabular format, ascending alphabetical order, in a file called c916contents.txt")
  45. [void]$Menu.AppendLine("3. This will current CPU and Memory Usage.")
  46. [void]$Menu.AppendLine("4. This will list all running processes within system, by virtual size least to greatest, in grid format.")
  47. [void]$Menu.AppendLine("5. Exit Script.")
  48. [void]$Menu.AppendLine("6. This will cause an error on purpose to test exception handling")
  49. [void]$Menu.AppendLine("-------------------")
  50. Write-Host -ForegroundColor Cyan $Menu.ToString()
  51. return $(Write-Host -ForegroundColor Cyan ">> Select a command (1-6):"; Read-Host)
  52. }
  53.  
  54. #Variable to hold user input
  55. $UserInput = 1
  56. Try{
  57. while ($UserInput -ne 6)
  58. {
  59. # Prompt for User Input
  60. $UserInput = Get-UserInput
  61. # Run specfic code based on User Input
  62. switch ($UserInput)
  63. {
  64. 1 # User chose option 1
  65. {
  66. #This will list all log files in requirements1 folder, with current date preceeding
  67. Write-Host "Attempting to run Option 1"
  68. "Timestamp: " + (Get-Date) | Out-File -FilePath $PSScriptRoot\DailyLog.txt -Append
  69. Get-ChildItem -Path $PSScriptRoot -Filter *.log | Out-File -FilePath $PSScriptRoot\DailyLog.txt -Append
  70. }
  71.  
  72. 2 # User chose option 2
  73. {
  74. #This will list all files in requirements1 folder in tabular format, ascending alphabetical order, in a file called c916contents.txt
  75. Get-ChildItem "$PSScriptRoot" | Sort-Object Name | Format-Table -AutoSize -Wrap | Out-File "$PSScriptRoot\C916Contents.txt"
  76. }
  77.  
  78. 3 # User chose option 1
  79. {
  80. #This will current CPU and Memory Usage
  81. $CounterList = "\Processor(_Total)\% Processor Time", "\Memory\Committed Bytes"
  82. Get-Counter -Counter $CounterList -MaxSamples 4 -SampleInterval 5
  83. }
  84.  
  85. 4
  86. {
  87. #This will list all running processes within system, by virtual size least to greatest, in grid format
  88. Get-Process | Select-Object ID, Name, VM | Sort-Object Name | Out-GridView
  89. }
  90.  
  91. 5 # User chose option 5
  92. {
  93. #Quit
  94. Write-Host -ForegroundColor Red "Exiting Script"
  95. }
  96.  
  97. }
  98. }
  99. }
  100. #Catch a specific error type
  101. Catch[System.OutOfMemoryException]
  102. {
  103. Write-Host -ForegroundColor Red "An Error Occurred."
  104. Write-Host -ForegroundColor Red "$($PSItem.ToString())'n'n$($PSItem.ScriptStackTrace)"
  105. }
  106. Finally
  107. {
  108. # Close any open resources
  109. # This code runs right before script ends
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement