Skip to content

Instantly share code, notes, and snippets.

View dfinke's full-sized avatar

Doug Finke dfinke

View GitHub Profile
@dfinke
dfinke / Show-GitLikeDiff.ps1
Created August 25, 2024 18:31
Git-like Diff Tool in PowerShell
# Git-like Diff Tool in PowerShell
function Compare-Files {
param (
[string]$OldFilePath,
[string]$NewFilePath,
[int]$ContextLines = 3
)
$oldFile = Get-Content -Path $OldFilePath
@dfinke
dfinke / Compare-PicsUsingAI.ps1
Last active August 10, 2024 17:18
AI-powered image comparison in PowerShell using PSAI module
# Install-module PSAI
# Get OpenAI API key from https://platform.openai.com/account/api-keys
# Set $env:OpenAIKey
function Compare-PicsUsingAI {
param(
$pic1,
$pic2
)
@dfinke
dfinke / powershell-agent-team.ps1
Last active July 29, 2024 10:55
The difference between building LLM-powered applications in summer 2024 vs summer 2023?
<#
This PowerShell script is powered by my PSAI and PSAIAgent modules.
PSAI - Is a port of the OpenAI Python SDK
PSAIAgent - Is a PowerShell module that allows you turn an OpenAI LLM into an AI Assistant with memory and tools
Register-Tool - Takes PowerShell functions signatures and creates the JSON to be used by OpenAI
New-Agent - Creates an AI Agent that can be used to interact with OpenAI
Get-AgentResponse - Takes the agent and prompt, calls the OpenAI model, runs the functions if needed and returns the response
#>
@dfinke
dfinke / Singleton.ps1
Created June 28, 2024 13:50
PowerShell Singleton Pattern: Ensure single instance for shared resource management
# Singleton
class Product {
$Name
Product($name) {
$this.Name = $name
}
}

PowerShell Issues FAQ

General

Q: How frequently are issues in the PowerShell repository reviewed? A: The frequency of issue reviews can vary; however, issues are often reviewed when they are newly created. Regular review cycles might also be in place depending on the projectΓÇÖs maintenance policy.

Q: How can I determine if a cmdlet/module is maintained in the PowerShell repository? A: You can use the repository's search function, issue templates, and officially provided documentation to determine if a cmdlet/module is maintained in the powershell/powershell repository.

@dfinke
dfinke / ChatMediator.ps1
Created June 21, 2024 14:06
Demonstrates the implementation of the Mediator Design Pattern in PowerShell
<#
Define an object that encapsulates how a set of objects interact.
Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
#>
class ChatMediator {
$users
ChatMediator() {
$this.users = New-Object System.Collections.ArrayList
@dfinke
dfinke / StartComputer.ps1
Last active July 30, 2024 13:53
PowerShell example demonstrating the Facade Design Pattern implementation
class CPU {
freeze() { "$this freezing" | Out-Host }
jump($position) { "$this jump to $position" | Out-Host }
execute() { "$this execute" | Out-Host }
}
class Memory {
load($position, $data) {
"$this load $position $data" | Out-Host
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
}, {
"category": "fiction",
@dfinke
dfinke / FinanceReport.ps1
Last active July 3, 2024 17:24
PowerShell script using the Template Method pattern to generate financial reports in plain text and HTML formats. Extensible and flexible.
class ReportTemplate {
hidden $data
GenerateReport() {
$this.RetrieveFinancialData()
$this.FormatReport()
$this.SendToStakeholders()
}
RetrieveFinancialData() {
class Command {
execute() {
"[$(get-date)] Logging execute of command [$this]" | Out-Host # Logs the execution of the command
}
}
class Loony : Command {
execute() {
([Command]$this).execute() # Calls the execute method of the base class (Command)
"You're a loony." | Out-Host # Outputs "You're a loony."