GenerateRandomUser.psm1


#
#
# GenerateRandomUser
#
#


$ErrorActionPreference = 'Stop'


#
# Module functions
#


Function Get-RandomUserList {

    <#
 
    .SYNOPSIS
    Generate random users
 
    .DESCRIPTION
    Generate random users
 
    .PARAMETER Results
    The number of results or users to generate (max 5,000 per call)
    This parameter is optional
 
    .PARAMETER Gender
    The gender of the results (Male or Female)
    This parameter is optional
 
    .PARAMETER Nationality
    The nationality of the results
    (AU, BR, CA, CH, DE, DK, ES, FI, FR, GB, IE, IR, NO, NL, NZ, TR, US)
    This parameter is optional
 
    .PARAMETER Exclude
    Fields to exclude from the results
    (gender, name, location, email, login, registered, dob, phone, cell, id, picture, nat)
    This parameter is optional
 
    .PARAMETER Include
    Fields to include in the results
    (gender, name, location, email, login, registered, dob, phone, cell, id, picture, nat)
    This parameter is optional
 
    .EXAMPLE
    Get-RandomUserList
 
    The cmdlet without arguments will return 1 result
 
    .EXAMPLE
    Get-RandomUserList -Results 10 -Gender male
 
    This will generate 10 male users
 
    .EXAMPLE
    Get-RandomUserList -Results 10 -Gender female -Nationality US -Exclude location
 
    This will generate 10 Female users from the US and will not include the Location field
 
    .EXAMPLE
    Get-RandomUserList -Results 10 -Include name, location -Nationality IR | Export-Csv C:\Temp\UserList.csv -NoTypeInformation -Encoding UTF8
 
    This will generate 10 users from Iran showing only Name and Location, the encoding (UTF8) is added to the Export-Csv cmdlet to properly
    display the arabic characters
 
    .NOTES
    There's a limit of usage to prevent abuse, if this limit is reached the
    system will display an error message
 
    Some fonts might not display readable data when the results are
    using arabic or asian characters
 
    .LINK
    N/A
 
    #>


    [CmdletBinding ()]

    Param (

        [Parameter (Mandatory = $False,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True,
                    HelpMessage = 'Enter number of results'
                   )
        ]

        [Int]$Results,

        [Parameter (Mandatory = $False,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True,
                    HelpMessage = 'Enter gender of results'
                   )
        ]

        [ValidateSet('male', 'female')]

        [String]$Gender,

        [Parameter (Mandatory = $False,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True,
                    HelpMessage = 'Enter nationality of results'
                   )
        ]

        [ValidateSet('AU', 'BR', 'CA', 'CH', 'DE', 'DK', 'ES', 'FI', 'FR', 'GB', 'IE', 'IR', 'NO', 'NL', 'NZ', 'TR', 'US')]

        [String[]]$Nationality,

        [Parameter (Mandatory = $False,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True,
                    HelpMessage = 'Enter fields to exclude'
                   )
        ]

        [ValidateSet('gender', 'name', 'location', 'email', 'login', 'registered', 'dob', 'phone', 'cell', 'id', 'picture', 'nat')]

        [String[]]$Exclude,

        [Parameter (Mandatory = $False,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True,
                    HelpMessage = 'Enter fields to include'
                   )
        ]

        [ValidateSet('gender', 'name', 'location', 'email', 'login', 'registered', 'dob', 'phone', 'cell', 'id', 'picture', 'nat')]

        [String[]]$Include

    )

    BEGIN {

        If ($Nationality) {

            ForEach ($Item In $Nationality) {

                $NationalityField = $NationalityField + $Item + ','

            }

            $NationalityField = $NationalityField.TrimEnd(',')

        }

        If ($Exclude) {

            ForEach ($Item In $Exclude) {

                $ExcludeField = $ExcludeField + $Item + ','

            }

            $ExcludeField = $ExcludeField.TrimEnd(',')

        }

        If ($Include) {

            ForEach ($Item In $Include) {

                $IncludeField = $IncludeField + $Item + ','

            }

            $IncludeField = $IncludeField.TrimEnd(',')

        }

    }

    PROCESS {

        Try {

            $UserData = Invoke-WebRequest -Uri "https://randomuser.me/api/?results=$Results&gender=$Gender&nat=$NationalityField&exc=$ExcludeField&inc=$IncludeField&format=csv&noinfo"

            Write-Output $UserData.Content | ConvertFrom-Csv

        }

        Catch {

            Write-Warning -Message $PSItem.Exception.Message

        }

    }

    END {}

}