PowerShell Tester Tool Template

<#
.SYNOPSIS
PowerShell Tester Tool Template

.DESCRIPTION
This script provides a template for a PowerShell-based tester tool that can be customized
for various testing scenarios. It includes functions for defining tests, running them,
and generating reports.

.NOTES
File Name      : TesterToolTemplate.ps1
Author         : [Your Name]
Prerequisite   : PowerShell V5.1 or later
Version        : 1.0
Date           : [Current Date]

.EXAMPLE
.\TesterToolTemplate.ps1
#>

# Global variables
$global:testResults = @()
$global:reportPath = "$env:USERPROFILE\Desktop\Test_Report_$(Get-Date -Format 'yyyyMMdd_HHmmss').html"

<#
.SYNOPSIS
Displays the main menu of the tester tool.
#>
function Show-Menu {
    Clear-Host
    Write-Host "=== PowerShell Tester Tool ===" -ForegroundColor Cyan
    Write-Host "1. Run All Tests"
    Write-Host "2. Run Specific Test"
    Write-Host "3. View Test Results"
    Write-Host "4. Generate HTML Report"
    Write-Host "5. Exit"
}

<#
.SYNOPSIS
Defines the test cases. Add your test functions here.
#>
function Define-Tests {
    $global:tests = @{
        "Test1" = { Test-Case1 }
        "Test2" = { Test-Case2 }
        "Test3" = { Test-Case3 }
        # Add more test cases as needed
    }
}

<#
.SYNOPSIS
Example test case 1. Replace with your actual test logic.
#>
function Test-Case1 {
    $testName = "Test Case 1"
    $description = "Description of Test Case 1"
    try {
        # Your test logic here
        $result = $true
        $message = "Test Case 1 passed successfully"
    }
    catch {
        $result = $false
        $message = "Test Case 1 failed: $_"
    }
    return [PSCustomObject]@{
        TestName = $testName
        Description = $description
        Result = $result
        Message = $message
    }
}

<#
.SYNOPSIS
Example test case 2. Replace with your actual test logic.
#>
function Test-Case2 {
    $testName = "Test Case 2"
    $description = "Description of Test Case 2"
    try {
        # Your test logic here
        $result = $true
        $message = "Test Case 2 passed successfully"
    }
    catch {
        $result = $false
        $message = "Test Case 2 failed: $_"
    }
    return [PSCustomObject]@{
        TestName = $testName
        Description = $description
        Result = $result
        Message = $message
    }
}

<#
.SYNOPSIS
Example test case 3. Replace with your actual test logic.
#>
function Test-Case3 {
    $testName = "Test Case 3"
    $description = "Description of Test Case 3"
    try {
        # Your test logic here
        $result = $true
        $message = "Test Case 3 passed successfully"
    }
    catch {
        $result = $false
        $message = "Test Case 3 failed: $_"
    }
    return [PSCustomObject]@{
        TestName = $testName
        Description = $description
        Result = $result
        Message = $message
    }
}

<#
.SYNOPSIS
Runs all defined tests.
#>
function Run-AllTests {
    Write-Host "`nRunning All Tests..." -ForegroundColor Yellow
    $global:testResults = @()
    foreach ($test in $global:tests.GetEnumerator()) {
        Write-Host "Running $($test.Key)..." -ForegroundColor Cyan
        $result = & $test.Value
        $global:testResults += $result
        if ($result.Result) {
            Write-Host "  Passed: $($result.Message)" -ForegroundColor Green
        } else {
            Write-Host "  Failed: $($result.Message)" -ForegroundColor Red
        }
    }
}

<#
.SYNOPSIS
Runs a specific test based on user input.
#>
function Run-SpecificTest {
    Write-Host "`nAvailable Tests:" -ForegroundColor Yellow
    $global:tests.Keys | ForEach-Object { Write-Host "  $_" }
    $testName = Read-Host "`nEnter the name of the test to run"
    if ($global:tests.ContainsKey($testName)) {
        Write-Host "Running $testName..." -ForegroundColor Cyan
        $result = & $global:tests[$testName]
        $global:testResults += $result
        if ($result.Result) {
            Write-Host "  Passed: $($result.Message)" -ForegroundColor Green
        } else {
            Write-Host "  Failed: $($result.Message)" -ForegroundColor Red
        }
    } else {
        Write-Host "Test not found." -ForegroundColor Red
    }
}

<#
.SYNOPSIS
Displays the results of all run tests.
#>
function View-TestResults {
    if ($global:testResults.Count -eq 0) {
        Write-Host "`nNo tests have been run yet." -ForegroundColor Yellow
    } else {
        Write-Host "`nTest Results:" -ForegroundColor Yellow
        $global:testResults | Format-Table -AutoSize
    }
}

<#
.SYNOPSIS
Generates an HTML report of the test results.
#>
function Generate-HTMLReport {
    Write-Host "`nGenerating HTML Report..." -ForegroundColor Yellow
    $reportContent = @"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Results Report</title>
    <style>
        body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 800px; margin: 0 auto; padding: 20px; }
        h1 { color: #0078D4; }
        table { border-collapse: collapse; width: 100%; margin-top: 20px; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
        .pass { color: green; }
        .fail { color: red; }
    </style>
</head>
<body>
    <h1>Test Results Report</h1>
    <p>Generated on: $(Get-Date)</p>
    <table>
        <tr>
            <th>Test Name</th>
            <th>Description</th>
            <th>Result</th>
            <th>Message</th>
        </tr>
        $(foreach ($result in $global:testResults) {
            $resultClass = if ($result.Result) { "pass" } else { "fail" }
            "<tr>
                <td>$($result.TestName)</td>
                <td>$($result.Description)</td>
                <td class='$resultClass'>$(if ($result.Result) { "Pass" } else { "Fail" })</td>
                <td>$($result.Message)</td>
            </tr>"
        })
    </table>
</body>
</html>
"@

    $reportContent | Out-File -FilePath $global:reportPath
    Write-Host "Report generated and saved to: $global:reportPath" -ForegroundColor Green
}

# Main program loop
Define-Tests
do {
    Show-Menu
    $choice = Read-Host "`nEnter your choice (1-5)"

    switch ($choice) {
        "1" { Run-AllTests }
        "2" { Run-SpecificTest }
        "3" { View-TestResults }
        "4" { Generate-HTMLReport }
        "5" { Write-Host "Exiting program..." -ForegroundColor Yellow; break }
        default { Write-Host "Invalid choice. Please try again." -ForegroundColor Red }
    }

    if ($choice -ne "5") {
        Read-Host "`nPress Enter to continue..."
    }
} while ($choice -ne "5")

This PowerShell Tester Tool Template includes:

  1. A menu-driven interface for easy navigation.
  2. Functions for defining and running tests.
  3. Placeholder test case functions that you can replace with your actual test logic.
  4. Ability to run all tests or a specific test.
  5. Function to view test results.
  6. HTML report generation for test results.

Key features:

  • Flexible structure for defining multiple test cases
  • Easy-to-use menu for running tests and viewing results
  • Detailed test result tracking, including test name, description, result, and message
  • HTML report generation for easy sharing and documentation of test results
  • Modular design for easy expansion and modification

To use this template effectively:

  1. Replace the placeholder test case functions (Test-Case1, Test-Case2, Test-Case3) with your specific test logic.
  2. Add more test cases as needed in the Define-Tests function.
  3. Customize the test result object properties if you need to capture additional information.
  4. Modify the HTML report generation function if you want to change the report format or add more details.
  5. Add any necessary module imports or additional global variables at the beginning of the script.

This template provides a solid foundation for creating various types of testing tools in PowerShell, whether you’re testing system configurations, application functionality, or any other scenario that requires structured testing and reporting.

Template Menu-Driven PowerShell Tool

<#
.SYNOPSIS
Template Menu-Driven PowerShell Tool

.DESCRIPTION
This script provides a template for a menu-driven PowerShell tool that can be customized
for various administrative or analysis tasks.

.NOTES
File Name      : TemplateMenuTool.ps1
Author         : [Your Name]
Prerequisite   : PowerShell V5.1 or later
Version        : 1.0
Date           : [Current Date]

.EXAMPLE
.\TemplateMenuTool.ps1
#>

# Global variables
$global:reportPath = "$env:USERPROFILE\Desktop\Analysis_Report_$(Get-Date -Format 'yyyyMMdd_HHmmss').html"

<#
.SYNOPSIS
Displays the main menu of the tool.
#>
function Show-Menu {
    Clear-Host
    Write-Host "=== Template Menu-Driven Tool ===" -ForegroundColor Cyan
    Write-Host "1. Set Target (e.g., computer, path, etc.)"
    Write-Host "2. Perform Analysis Task 1"
    Write-Host "3. Perform Analysis Task 2"
    Write-Host "4. Perform Analysis Task 3"
    Write-Host "5. Perform Analysis Task 4"
    Write-Host "6. Perform Analysis Task 5"
    Write-Host "7. Generate Comprehensive HTML Report"
    Write-Host "8. Exit"
}

<#
.SYNOPSIS
Sets the target for analysis (e.g., computer name, file path, etc.).
#>
function Set-Target {
    $target = Read-Host "Enter the target for analysis (e.g., computer name, file path)"
    if (-not [string]::IsNullOrWhiteSpace($target)) {
        $global:target = $target
        Write-Host "Target set to: $global:target" -ForegroundColor Green
    } else {
        Write-Host "Invalid target. Please try again." -ForegroundColor Red
    }
}

<#
.SYNOPSIS
Performs Analysis Task 1.

.OUTPUTS
Array of PSObjects containing analysis results.
#>
function Perform-AnalysisTask1 {
    Write-Host "`nPerforming Analysis Task 1..." -ForegroundColor Yellow
    # Add your analysis logic here
    $results = @([PSCustomObject]@{
        Property1 = "Value1"
        Property2 = "Value2"
    })
    $results | Format-Table -AutoSize
    return $results
}

<#
.SYNOPSIS
Performs Analysis Task 2.

.OUTPUTS
Array of PSObjects containing analysis results.
#>
function Perform-AnalysisTask2 {
    Write-Host "`nPerforming Analysis Task 2..." -ForegroundColor Yellow
    # Add your analysis logic here
    $results = @([PSCustomObject]@{
        Property1 = "Value1"
        Property2 = "Value2"
    })
    $results | Format-Table -AutoSize
    return $results
}

<#
.SYNOPSIS
Performs Analysis Task 3.

.OUTPUTS
Array of PSObjects containing analysis results.
#>
function Perform-AnalysisTask3 {
    Write-Host "`nPerforming Analysis Task 3..." -ForegroundColor Yellow
    # Add your analysis logic here
    $results = @([PSCustomObject]@{
        Property1 = "Value1"
        Property2 = "Value2"
    })
    $results | Format-Table -AutoSize
    return $results
}

<#
.SYNOPSIS
Performs Analysis Task 4.

.OUTPUTS
Array of PSObjects containing analysis results.
#>
function Perform-AnalysisTask4 {
    Write-Host "`nPerforming Analysis Task 4..." -ForegroundColor Yellow
    # Add your analysis logic here
    $results = @([PSCustomObject]@{
        Property1 = "Value1"
        Property2 = "Value2"
    })
    $results | Format-Table -AutoSize
    return $results
}

<#
.SYNOPSIS
Performs Analysis Task 5.

.OUTPUTS
Array of PSObjects containing analysis results.
#>
function Perform-AnalysisTask5 {
    Write-Host "`nPerforming Analysis Task 5..." -ForegroundColor Yellow
    # Add your analysis logic here
    $results = @([PSCustomObject]@{
        Property1 = "Value1"
        Property2 = "Value2"
    })
    $results | Format-Table -AutoSize
    return $results
}

<#
.SYNOPSIS
Generates a comprehensive HTML report of all analyses.

.PARAMETER AllResults
Hashtable containing all analysis results.

.OUTPUTS
Saves an HTML report to the desktop.
#>
function Generate-HTMLReport {
    param([hashtable]$AllResults)

    Write-Host "`nGenerating Comprehensive HTML Report..." -ForegroundColor Yellow
    $reportContent = @"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Analysis Report</title>
    <style>
        body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 1200px; margin: 0 auto; padding: 20px; }
        h1, h2, h3 { color: #0078D4; }
        table { border-collapse: collapse; width: 100%; margin-bottom: 20px; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
    </style>
</head>
<body>
    <h1>Analysis Report</h1>
    <p>Generated on: $(Get-Date)</p>
    <p>Target: $global:target</p>

    <h2>Analysis Task 1 Results</h2>
    $($AllResults.Task1Results | ConvertTo-Html -Fragment)

    <h2>Analysis Task 2 Results</h2>
    $($AllResults.Task2Results | ConvertTo-Html -Fragment)

    <h2>Analysis Task 3 Results</h2>
    $($AllResults.Task3Results | ConvertTo-Html -Fragment)

    <h2>Analysis Task 4 Results</h2>
    $($AllResults.Task4Results | ConvertTo-Html -Fragment)

    <h2>Analysis Task 5 Results</h2>
    $($AllResults.Task5Results | ConvertTo-Html -Fragment)
</body>
</html>
"@

    $reportContent | Out-File -FilePath $global:reportPath
    Write-Host "Report generated and saved to: $global:reportPath" -ForegroundColor Green
}

# Main program loop
$allResults = @{}

do {
    Show-Menu
    $choice = Read-Host "`nEnter your choice (1-8)"

    switch ($choice) {
        "1" { Set-Target }
        "2" { $allResults.Task1Results = Perform-AnalysisTask1 }
        "3" { $allResults.Task2Results = Perform-AnalysisTask2 }
        "4" { $allResults.Task3Results = Perform-AnalysisTask3 }
        "5" { $allResults.Task4Results = Perform-AnalysisTask4 }
        "6" { $allResults.Task5Results = Perform-AnalysisTask5 }
        "7" { Generate-HTMLReport -AllResults $allResults }
        "8" { Write-Host "Exiting program..." -ForegroundColor Yellow; break }
        default { Write-Host "Invalid choice. Please try again." -ForegroundColor Red }
    }

    if ($choice -ne "8") {
        Read-Host "`nPress Enter to continue..."
    }
} while ($choice -ne "8")

This Template Menu-Driven Tool includes:

  1. A menu-driven interface for easy navigation.
  2. Placeholder functions for five different analysis tasks.
  3. A function to set a target for analysis (which can be customized based on your needs).
  4. A function to generate an HTML report of all collected data.
  5. Basic error handling and user input validation.

Key features:

  • Easy-to-customize menu structure
  • Placeholder functions for various analysis tasks
  • Ability to set and use a global target variable
  • HTML report generation capability
  • Modular design for easy expansion and modification

To use this template effectively:

  1. Replace the placeholder analysis functions (Perform-AnalysisTask1, Perform-AnalysisTask2, etc.) with your specific analysis logic.
  2. Customize the Set-Target function to handle the type of target you need (e.g., computer name, file path, etc.).
  3. Modify the menu options and corresponding switch statement in the main program loop to match your specific tasks.
  4. Adjust the HTML report generation function to include the specific results you want to display.
  5. Add any necessary module imports or additional global variables at the beginning of the script.

This template provides a solid foundation for creating various types of menu-driven PowerShell tools for system administration, security analysis, or any other task that requires user interaction and multiple analysis steps.