Created
May 8, 2025 01:55
-
-
Save dfinke/cc6d318ae104d4889dfb22a55394f14f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Requires -Module PSAISuite | |
| # Step 1: Generate PowerShell function from a prompt (Model A - GPT-4) | |
| $genPrompt = @" | |
| You're a PowerShell expert. | |
| Write a function that takes a CSV file and creates an Excel file using ImportExcel module. | |
| Return only the code. | |
| "@ | |
| $modelId1 = "github:openai/gpt-4.1" | |
| Write-Host "Step 1: Generating PowerShell function using $modelId1..." -ForegroundColor Green | |
| $code = Invoke-ChatCompletion $genPrompt $modelId1 | |
| # Step 2: Review the code for issues (Model B - Claude 3 Opus) | |
| $reviewPrompt = @" | |
| You're a senior PowerShell reviewer. Analyze this function for: | |
| - Security risks | |
| - Misused cmdlets | |
| - General improvements | |
| Code: | |
| $($code.Response) | |
| "@ | |
| $modelId2 = "github:mistral-ai/Mistral-Nemo" | |
| Write-Host "Step 2: Reviewing the code for issues using $modelId2..." -ForegroundColor Green | |
| $review = Invoke-ChatCompletion $reviewPrompt $modelId2 | |
| # Step 3: Write Pester tests (Model C - Mistral if available, fallback to GPT-4) | |
| $testPrompt = @" | |
| Write Pester tests for this PowerShell function. Assume it's in a module and should be unit tested: | |
| $($code.Response) | |
| "@ | |
| $modelId3 = "github:meta/Llama-3.3-70B-Instruct" | |
| Write-Host "Step 3: Writing Pester tests using $modelId3..." -ForegroundColor Green | |
| $tests = Invoke-ChatCompletion $testPrompt $modelId3 | |
| # Output the pipeline | |
| Write-Host "Step 4: Outputting the pipeline results to ./result.md..." -ForegroundColor Green | |
| $( | |
| "`nGenerated Code:`n$($code.Response)" | |
| "`nCode Review:`n$($review.Response)" | |
| "`nPester Tests:`n$($tests.Response)" | |
| ) | Set-Content -Path "./result.md" -Force |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Generated Code:
Code Review:
Security Risks:
Path Manipulation: The script doesn't validate or sanitize input paths, which could lead to unintended behavior or security risks if an attacker provides a malicious path. For example, an attacker could provide a path that overwrites a critical system file.
Mitigation: Validate input paths to ensure they are safe and don't contain malicious content.
ErrorAction Stop: Using
StopinErrorActionhalts the script at the first error, which could mask underlying issues or prevent expected error handling behavior in calling scripts.Mitigation: Use a more appropriate
ErrorActionpreference, such asContinueorSilentlyContinue, or handle errors usingtry-catchblocks.Misused Cmdlets:
Import-Module -ErrorAction Stop: While it's good practice to stop the script on module import errors, using
-ErrorAction Stophere could prevent proper error handling in calling scripts. It's better to usetry-catchblocks for better control over error handling.Mitigation: Use
try-catchblocks for module import instead.General Improvements:
Input Validation: The script lacks input validation for CSV and Excel paths. It should ensure that provided paths exist and are of the correct type.
Error Handling: The script could benefit from better error handling. Currently, it only stops on module import errors. It should also handle errors related to CSV and Excel file operations.
Help and Documentation: The function lacks help and documentation, making it harder for users to understand and use the function correctly.
Consistent Naming: The function name
Convert-CsvToExcelsuggests that it converts a CSV file to an Excel file, but it actually imports a CSV file and exports it to Excel. Consider renaming the function to something likeExport-CsvToExcel.Improved Code:
This improved version includes input validation, better error handling, and more descriptive output for errors. It also uses consistent naming and includes helpful comments.
Pester Tests:
To unit test the
Convert-CsvToExcelfunction using Pester,