ADUserManagement.psm1

#Generate password using a specific number of words and numbers
function Get-XKCDPassword {

    Param(
    [int]$words = 2,
    [string]$delimiter = "",
    [ValidateSet("en","de")] 
    [string]$lang = "en",
    [switch]$FirstLetterUpperCase=$true  
    )
        
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    $password = ""
    $wordlist = @{
        de = "https://janikvonrotz.ch/wp-content/uploads/2017/08/wordlist.de_.txt"
        en = "https://janikvonrotz.ch/wp-content/uploads/2017/08/wordlist.en_.txt"
    }
    
    switch($words) {
        {$_ -ge 6 } { throw "Word parameter cannot be greater or equal 6." }
        5 { $range = (3,4) }
        4 { $range = (4,5) }
        3 { $range = (5,6) }
        2 { $range = (7,8) }
        {$_ -le 1 } { throw "Word parameter cannot be less or equal 1." }
    }

    $list = (((Invoke-WebRequest $wordlist[$lang]).Content -split "`n" | ForEach-Object{ 
        New-Object PSObject -Property @{
            Value = $_.ToLower()
            Length=$_.length
        }
    }) | Where-Object { ($_.Length -eq ($range[0] + 1)) -or ($_.Length -eq ($range[1] + 1)) })

    1..$words | ForEach-Object {
        $part =  (Get-Random $list).Value.Trim()

        if($FirstLetterUpperCase ) {
                $password += ((Get-Culture).TextInfo).ToTitleCase($part)
        } else {
            $password += $part
        }

        if($_ -lt $words){ 
            $password += $delimiter 
        }
    }
    $password = $password + (get-random -Maximum 99 -Minimum 1)

    return $password
}

#Convert AD distinguishedName into canonical name format
#Example: cn=user1,ou=users,dc=cdizzlefizzleshizzle,dc=com -> cdizzlefizzleshizzle.com/users/user1
function Get-CanonicalName ([string[]]$DistinguishedName) {    
    foreach ($dn in $DistinguishedName) {      
        $d = $dn.Split(',') ## Split the dn string up into it's constituent parts
        $arr = (@(($d | Where-Object { $_ -notmatch 'DC=' }) | ForEach-Object { $_.Substring(3) }))  ## get parts excluding the parts relevant to the FQDN and trim off the dn syntax
        [array]::Reverse($arr)  ## Flip the order of the array.
 
        ## Create and return the string representation in canonical name format of the supplied DN
        "{0}/{1}" -f  (($d | Where-Object { $_ -match 'dc=' } | ForEach-Object { $_.Replace('DC=','') }) -join '.'), ($arr -join '/') 
    } 
}
#Get list of domains and credentials from encrypted file
Function Get-DomainInformation {
    Param (
        [string]$domainFilePath
    )

    Write-LogInfo -LogPath $logFilePath -Message "Retreiving domain info from $domainFilePath"
    if(!(Test-Path $domainFilePath)) {
        Write-LogError -LogPath $logFilePath -Message "File not found at location $domainFilePath"
    } 
    else {
        Import-Csv $domainFilePath
    }
}