CsdyPasswordGenerator.psm1

function Get-Password {
    <#
.SYNOPSIS
    Generates a password using the 'password.ninja' API and provides a shareable URL using the 'pwpush.com' API.
 
.DESCRIPTION
    This function generates a password based on the specified parameter type (Standard, Random, Crazy) and provides a shareable URL.
 
.PARAMETER Type
    Specifies the type of password to generate. Valid parmater values are; 'Standard', 'Random', 'Crazy'. Default is 'Standard'.
 
.EXAMPLE
    Get-Password
    Generates a 12 character password using coherent words.
 
.EXAMPLE
    Get-Password Random
    Generates a 16 character randomized password.
 
.EXAMPLE
    Get-Password Crazy
    Generates a 64 character randomized password.
 
.NOTES
    Author: Jason Cassidy
    Version: 1.3.0
    Source: www.powershellgallery.com/profiles/Csdy
    Copyright: (c) 2025 Csdy. All rights reserved.
#>


    [CmdletBinding()]
    param (
        [Parameter(Position = 0)]
        [ValidateSet("Standard", "Random", "Crazy")]
        [string]$Type = "Standard"
    )

    Write-Host "==========================================================" -ForegroundColor "Green"
    Write-Host " ██████╗ ███████╗ ██████╗ ██╗ ██╗" -ForegroundColor "Green"
    Write-Host " ██╔════╝ ██╔════╝ ██╔══██╗╚██╗ ██╔╝" -ForegroundColor "Green"
    Write-Host " ██║ ███████╗ ██║ ██║ ╚████╔╝" -ForegroundColor "Green"
    Write-Host " ██║ ╚════██║ ██║ ██║ ╚██╔╝" -ForegroundColor "Green"
    Write-Host " ╚██████╗ ███████║ ██████╔╝ ██║" -ForegroundColor "Green"
    Write-Host " ╚═════╝ ╚══════╝ ╚═════╝ ╚═╝" -ForegroundColor "Green"
    Write-Host "Name: Password Generator 🔑" -ForegroundColor "Green"
    Write-Host "Description: Generate a password with a shareable URL" -ForegroundColor "Green"
    Write-Host "Version: 1.3.0" -ForegroundColor "Green"
    Write-Host "Author: Jason Cassidy" -ForegroundColor "Green"
    Write-Host "Source: www.powershellgallery.com/profiles/Csdy" -ForegroundColor "Green"
    Write-Host "Notes:" -ForegroundColor "Green"
    Write-Host "==========================================================" -ForegroundColor "Green"


    ########################################################################################################################
    # Generate a password
    ########################################################################################################################

    # Supress Progress Bars
    $ProgressPreference = "SilentlyContinue"

    if ($Type -eq "Random") {
        do {
            # Generate complex password
            add-type -AssemblyName System.Web
            $Password = [System.Web.Security.Membership]::GeneratePassword(16,4)

            # Check for adjacent duplicate characters
            $isValid = $true
            for ($i = 0; $i -lt ($Password.Length - 1); $i++) {
                if ($Password[$i] -eq $Password[$i + 1]) {
                    $isValid = $false
                    break
                }
            }
        } while (-not $isValid)  # Regenerate if invalid
    } elseif ($Type -eq "Crazy") {
        do {
            # Generate supercomplex password
            add-type -AssemblyName System.Web
            $Password = [System.Web.Security.Membership]::GeneratePassword(64,16)

            # Check for adjacent duplicate characters
            $isValid = $true
            for ($i = 0; $i -lt ($Password.Length - 1); $i++) {
                if ($Password[$i] -eq $Password[$i + 1]) {
                    $isValid = $false
                    break
                }
            }
        } while (-not $isValid)  # Regenerate if invalid
    } else {
        do {
            # Determine the password generation URL
            $url = "https://password.ninja/api/password?minPassLength=12&maxLength=12&capitals=true&symbols=true&excludeSymbols=f&instruments=true&colours=true&shapes=true&food=true&sports=true&transport=true&numOfPasswords=1"

            # an attempt at password generation
            $Password = (Invoke-WebRequest -Uri $url | Select-Object -ExpandProperty content) -replace '[]" []', ''
            
            # Check for adjacent duplicate characters
            $isValid = $true
            for ($i = 0; $i -lt ($Password.Length - 1); $i++) {
                if ($Password[$i] -eq $Password[$i + 1]) {
                    $isValid = $false
                    break
                }
            }
        } while (-not $isValid)  # Regenerate if invalid
    }

    # Store successful generation into the clipboard
    $Password | Set-Clipboard

    ########################################################################################################################
    # Share the password with pwpush.com based on the criteria;
    # - Link expires after 7 days
    # - Link expires after 1 view
    ########################################################################################################################

    # Construct the API body
    $body = @{
        password = @{
            payload = $Password
            expire_after_days = 7
            expire_after_views = 1
        }
    } | ConvertTo-Json -Depth 10  # Convert hashtable to JSON

    # Send the request to pwpush.com
    $response = Invoke-RestMethod -Uri 'https://pwpush.com/p.json' -Method Post -Body $body -ContentType "application/json"

    ########################################################################################################################
    # Output
    ########################################################################################################################

    Write-Host ""
    Write-Host "(1)" -ForegroundColor "Yellow"
    Write-Host "The password has been copied to your clipboard 📋" -ForegroundColor "Yellow"
    Write-Host "Paste the password into your user creation prompt" -ForegroundColor "Yellow"
    Write-Host ""
    Write-Host "⛔ Do NOT share this password in plain text to anyone!" -ForegroundColor "Red"
    Write-Host ""
    Write-Host "----------------------------------------------------------" -ForegroundColor "Green"
    Write-Host ""
    Write-Host "(2)" -ForegroundColor "Yellow"
    Write-Host "Copy & share the bottom " -ForegroundColor "Yellow" -NoNewline
    Write-Host "URL" -ForegroundColor "Magenta" -NoNewline
    Write-Host " with the requestor" -ForegroundColor "Yellow"
    Write-Host "The URL will expire after 1 view or after 7 days" -ForegroundColor "Yellow"
    Write-Host ""
    Write-Host "⛔ Do NOT open the URL if you are NOT the requestor!" -ForegroundColor "Red"
    Write-Host ""
    Write-Host "🡖" -ForegroundColor "Yellow"
    if ([string]::IsNullOrEmpty($response.url_token)) {
        Write-Host "⛔ Failed to generate URL. Please try again." -ForegroundColor "Red"
    } else {
        Write-Host "https://pwpush.com/p/$($response.url_token)/r" -ForegroundColor "Magenta"
    }
    Write-Host "----------------------------------------------------------" -ForegroundColor "Green"
    Write-Host ""
}