Untitled
unknown
plain_text
a month ago
3.8 kB
4
Indexable
<# Author: Daytona Thornton Student ID: 010588214 Script Name: prompts.ps1 Description: This PowerShell script automates several administrative tasks. It presents a switch-based menu with five options for the user. Based on the input, the script can: 1. List .log files using a regular expression. 2. List folder contents sorted alphabetically and saved to file. 3. Display CPU and memory usage. 4. Display running processes sorted by virtual memory usage. 5. Exit the script. Each action includes error handling and descriptive console output. #> $folderPath = "$HOME\Desktop\Requirements1" do { Write-Host "`nSelect an option:" Write-Host "1. List .log files and append to DailyLog.txt" Write-Host "2. List folder contents into C916contents.txt" Write-Host "3. Show CPU and memory usage" Write-Host "4. Show running processes sorted by memory" Write-Host "5. Exit" $choice = Read-Host "Enter your choice (1-5)" switch ($choice) { '1' { <# OPTION 1: List .log files using regex and save results to DailyLog.txt Includes a timestamp and avoids overwriting previous data #> try { $date = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $logFiles = Get-ChildItem -Path $folderPath -Recurse -File | Where-Object { $_.Name -match '\.log$' } if ($logFiles) { Add-Content -Path "$folderPath\DailyLog.txt" -Value "`n$date - Log File Listing" $logFiles | ForEach-Object { Add-Content -Path "$folderPath\DailyLog.txt" -Value $_.Name } Write-Host "Log files listed and appended to DailyLog.txt" } else { Write-Host "No .log files found." } } catch { Write-Host "Error: $_" } } '2' { <# OPTION 2: List folder contents and save to C916contents.txt in tabular format #> try { $output = Get-ChildItem -Path $folderPath | Sort-Object Name | Format-Table Name, Length, LastWriteTime -AutoSize | Out-String Set-Content -Path "$folderPath\C916contents.txt" -Value $output Write-Host "Folder contents saved to C916contents.txt" } catch { Write-Host "Error: $_" } } '3' { <# OPTION 3: Show CPU and memory usage using WMI and CIM classes #> try { Write-Host "`nCPU Usage:" Get-WmiObject Win32_Processor | Select-Object Name, LoadPercentage | Format-Table -AutoSize Write-Host "`nMemory Info:" Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object TotalVisibleMemorySize, FreePhysicalMemory | Format-Table -AutoSize } catch { Write-Host "Error: $_" } } '4' { <# OPTION 4: Show running processes sorted by virtual memory usage #> try { Get-Process | Sort-Object -Property VirtualMemorySize64 -Descending | Select-Object -Property Name, VirtualMemorySize64 | Format-Table -AutoSize } catch { Write-Host "Error: $_" } } '5' { Write-Host "Exiting script..." } Default { Write-Host "Invalid selection. Please choose a number between 1 and 5." } } } while ($choice -ne '5')
Editor is loading...
Leave a Comment