main.psm1

function New-PetName()
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$false,
                ValueFromPipelineByPropertyName=$true,
                HelpMessage="Path to one or more adverb lists.")]
        [string[]]
        $Adverbs,

        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Path to one or more adjective lists.")]
        [string[]]
        $Adjectives,

        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Path to one or more name lists.")]
        [string[]]
        $Names,

        [Parameter(Mandatory=$false,
                   Position=0,
                   HelpMessage="Length of pet name to produce"
        )]
        [int]
        $Length = 3,
        [Parameter(Mandatory=$false,
                   HelpMessage="String to use to join the names"
        )]
        $Separator = "-",

        [Parameter(Mandatory=$false)]
        [ValidateSet('Small','Medium','Large')]
        [string]$Complexity = "Small"
    )

    if ($null -eq $Adverbs -or $Adverbs.Count -eq 0) {
        $Adverbs = Join-Path $PSScriptRoot petname\usr\share\petname $Complexity.ToLower() .\adverbs.txt
    }
    if ($null -eq $Adjectives -or $Adjectives.Count -eq 0) {
        $Adjectives = Join-Path $PSScriptRoot petname\usr\share\petname $Complexity.ToLower() .\adjectives.txt
    }
    if ($null -eq $Names -or $Names.Count -eq 0) {
        $Names = Join-Path $PSScriptRoot petname\usr\share\petname $Complexity.ToLower() .\names.txt
    }

    &{
        while ($Length -gt 0) {
            if ($Length -eq 1) {
                Get-Content $Names | Get-Random
                $Length = $Length - 1
            }
            if ($Length -eq 2) {
                Get-Content $Adjectives | Get-Random
                $Length = $Length - 1
            }
            if ($Length -gt 2) {
                # Special case to prevent dupes.
                Get-Content $Adverbs | Get-Random -Count ($Length - 2)
                $Length = $Length - ($Length - 2)
            }
        }
    } | Join-String -Separator $Separator
}