GenerateRandomUser.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

#
#
# 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 {}

}