Untitled

 avatar
unknown
plain_text
24 days ago
2.8 kB
2
Indexable
<#
    Author: Daytona Thornton
    Student ID: 010588214
    Script: prompts.ps1
    Description: Provides a menu of four options:
                 1. List .log files and save to DailyLog.txt
                 2. Display directory contents in table format
                 3. Show CPU and memory usage
                 4. Display sorted list of running processes in a 
                    grid view
                 5. Exit
#>

function Show-Menu {
    Clear-Host
    Write-Host "Select an option:"
    Write-Host "1. List .log files and output to DailyLog.txt"
    Write-Host "2. Display directory contents in table format"
    Write-Host "3. Show CPU and memory usage"
    Write-Host "4. Show sorted list of running processes"
    Write-Host "5. Exit"
}

do {
    Show-Menu
    $choice = Read-Host "Enter your choice (1-5)"

    switch ($choice) {
        '1' {
            # List .log files and output to DailyLog.txt
            $logFiles = Get-ChildItem -Path (Get-Location) -Filter *.log
            $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
            $outputFile = "DailyLog.txt"

            Add-Content -Path $outputFile -Value "`n$timestamp - Log File Listing"
            foreach ($file in $logFiles) {
                Add-Content -Path $outputFile -Value $file.Name
            }

            Add-Content -Path $outputFile -Value ""
            Add-Content -Path $outputFile -Value (Get-ChildItem -Path (Get-Location) | Format-Table -AutoSize | Out-String)

            Write-Host "`nOutput saved to $outputFile"
            Pause
        }

        '2' {
            # Display directory contents in table format
            Write-Host "`nDirectory contents of: $(Get-Location)`n"
            Get-ChildItem -Path (Get-Location) | Format-Table Mode, LastWriteTime, Length, Name -AutoSize
            Pause
        }

        '3' {
            # Show CPU and memory usage using modern cmdlet
            $cpu = Get-CimInstance -ClassName Win32_Processor | Select-Object -ExpandProperty LoadPercentage
            $memory = Get-CimInstance -ClassName Win32_OperatingSystem | ForEach-Object {
                [math]::Round((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory) / $_.TotalVisibleMemorySize) * 100, 2)
            }

            Write-Host "`nCPU Load: $cpu%"
            Write-Host "Memory Usage: $memory%"
            Pause
        }

        '4' {
            # Display running processes sorted by memory usage in grid view
            Get-Process | Sort-Object WorkingSet -Descending | Out-GridView -Title "Running Processes (Sorted by Memory)"
        }

        '5' {
            Write-Host "Exiting script..."
        }

        default {
            Write-Host "Invalid selection. Please enter 1 to 5." -ForegroundColor Red
            Pause
        }
    }
} while ($choice -ne '5')
Editor is loading...
Leave a Comment