Skip to content

Instantly share code, notes, and snippets.

@webtroter
Last active December 1, 2021 22:21
Show Gist options
  • Save webtroter/70047cd16aa1bbf6b8e7a3914723a4d2 to your computer and use it in GitHub Desktop.
Save webtroter/70047cd16aa1bbf6b8e7a3914723a4d2 to your computer and use it in GitHub Desktop.
Three function to Add/Get/Remove testing Users.

Generate Users

Add-GeneratedUsers

SYNOPSIS

Creates Generated Users

SYNTAX

Add-GeneratedUsers [[-Count] <Int32>] [-Username <String>] [-Password <SecureString>] [-UseRandomUserMe]
 [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION

This function creates users automatically. Just provide some information if customization is needed

EXAMPLES

EXAMPLE 1

Add-GeneratedUsers

Creates 5 users with default password and no FullName

EXAMPLE 2

Add-Generatedusers -Count 10 -Username random -UseRandomUserMe

Creates 10 users with username random_1 with a FullName

PARAMETERS

-Count

How many users will be created.

Default : 5

Type: Int32
Parameter Sets: (All)
Aliases:

Required: False
Position: 1
Default value: 5
Accept pipeline input: False
Accept wildcard characters: False

-Username

The Username you wish to have. A serial number WILL BE appended

Default : test_user

Result : test_user_4

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: Test_user
Accept pipeline input: False
Accept wildcard characters: False

-Password

A secure string representing the desired password

Type: SecureString
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-UseRandomUserMe

If you wish to have "real" FullName on your local user

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.

Type: SwitchParameter
Parameter Sets: (All)
Aliases: wi

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Confirm

Prompts you for confirmation before running the cmdlet.

Type: SwitchParameter
Parameter Sets: (All)
Aliases: cf

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

NOTES

No Notes

RELATED LINKS

Get-GeneratedUsers

SYNOPSIS

Gets generated user

SYNTAX

Get-GeneratedUsers

DESCRIPTION

Gets all users with description

EXAMPLES

EXAMPLE 1

Get-GeneratedUsers

PARAMETERS

INPUTS

OUTPUTS

NOTES

General notes

RELATED LINKS


external help file: IdefixAPIClient-help.xml Module Name: IdefixAPIClient online version: schema: 2.0.0

Remove-GeneratedUsers

SYNOPSIS

Remove generated Users

SYNTAX

Remove-GeneratedUsers [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION

This function will removed generated users

EXAMPLES

EXAMPLE 1

Remove-GeneratedUsers

PARAMETERS

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.

Type: SwitchParameter
Parameter Sets: (All)
Aliases: wi

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Confirm

Prompts you for confirmation before running the cmdlet.

Type: SwitchParameter
Parameter Sets: (All)
Aliases: cf

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

NOTES

Will confirm before removing.

RELATED LINKS

function Add-GeneratedUsers {
<#
.SYNOPSIS
Creates Generated Users
.DESCRIPTION
This function creates users automatically. Just provide some information if customization is needed
.PARAMETER Count
How many users will be created.
Default : 5
.PARAMETER Username
The Username you wish to have. A serial number WILL BE appended
Default : test_user
Result : test_user_4
.PARAMETER Password
A secure string representing the desired password
.PARAMETER UseRandomUserMe
If you wish to have "real" FullName on your local user
.EXAMPLE
PS> Add-GeneratedUsers
Creates 5 users with default password and no FullName
.EXAMPLE
PS> Add-Generatedusers -Count 10 -Username random -UseRandomUserMe
Creates 10 users with username random_1 with a FullName
.NOTES
General notes
#>
[CmdletBinding(SupportsShouldProcess)]
param (
# Count
[Parameter(Position = 0)]
[int]
$Count = 5,
# Username
[Parameter()]
[string]
$Username = "test_user",
# SecureString Password
[Parameter()]
[securestring]
$Password,
# Use randomuser.me API
[Parameter()]
[switch]
$UseRandomUserMe
)
begin {
$WarningString = "This command will create {0} users with serialized username like {1}_1" -f $Count, $Username
Write-Warning $WarningString
Write-Warning "All users will have the same password"
if (-not $PSBoundParameters.ContainsKey("Password")) {
$UnsecurePassword = "!very!Unsecure123!"
$Password = ConvertTo-SecureString -String $UnsecurePassword -AsPlainText -Force
Write-Warning "You have NOT provided a password. The password will be: $UnsecurePassword"
}
if ($UseRandomUserMe) {
$GeneratedUsers = Invoke-RestMethod -Method Get -Uri $("https://randomuser.me/api/?results={0}" -f $Count) | Select-Object -ExpandProperty results
}
}
process {
for ($i = 0; $i -lt $Count; $i++) {
$curUsername = "{0}_{1}" -f $Username, $i
$newLocalUserSplat = @{
Name = $curUsername
FullName = ""
Description = "Created Automatically"
Password = $Password
}
if ($UseRandomUserMe) {
$newLocalUserSplat.FullName = "{0} {1}" -f $GeneratedUsers[$i].name.first, $GeneratedUsers[$i].name.last
}
New-LocalUser @newLocalUserSplat
}
}
end {
}
}
function Get-GeneratedUsers {
<#
.SYNOPSIS
Gets generated user
.DESCRIPTION
Gets all users with description
.EXAMPLE
An example
.NOTES
General notes
#>
param (
)
Get-LocalUser | Where-Object { $_.Description -eq "Created Automatically" }
}
function Remove-GeneratedUsers {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
param (
)
begin {
Write-Warning "Users to be removed: `n$(Get-GeneratedUsers | Select-Object -Property Name, FullName, Description | Out-String)"
}
process {
foreach ($curUser in (Get-GeneratedUsers)) {
if ($PSCmdlet.ShouldProcess("$($curUser.Name)", "Removing")) {
Remove-LocalUser $curUser -Verbose
}
}
}
end {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment