Search This Blog

Tuesday, May 16, 2023

 Create Testusers in AzureAD with Powershell Graph


####################################################################################
# The Real Shrimp
####################################################################################
#Functions
Function Get-RandomPassword
{
    #define parameters
    param([int]$PasswordLength = 10)
 
    #ASCII Character set for Password
    $CharacterSet = @{
            Uppercase   = (97..122) | Get-Random -Count 10 | % {[char]$_}
            Lowercase   = (65..90)  | Get-Random -Count 10 | % {[char]$_}
            Numeric     = (48..57)  | Get-Random -Count 10 | % {[char]$_}
            SpecialChar = (33..47)+(58..64)+(91..96)+(123..126) | Get-Random -Count 10 | % {[char]$_}
    }
 
    #Frame Random Password from given character set
    $StringSet = $CharacterSet.Uppercase + $CharacterSet.Lowercase + $CharacterSet.Numeric + $CharacterSet.SpecialChar
 
    -join(Get-Random -Count $PasswordLength -InputObject $StringSet)
}
#Read more: https://www.sharepointdiary.com/2020/04/powershell-generate-random-password.html#ixzz81sBsb0UW
####################################################################################
# Custom Objects
####################################################################################
$Results = New-Object System.Collections.ArrayList
####################################################################################
# Script
####################################################################################
Write-Host "This script creates a number of testusees in a designated tenant" -ForegroundColor Green
Do {
    Write-Host "Enter a Tenant ID, please" -ForegroundColor Green
    $TenantID = Read-Host
    $TenantIdCount = $TenantId | Measure-Object -Character
  }
Until ($TenantIdCount.characters -eq "36")
Do {
  Write-Host "Enter the number of test users to create" -ForegroundColor Green
  [Int]$TestUsers = Read-Host
}
Until ($TestUsers -is [Int])
####################################################################################
# Connecting to the tenant
####################################################################################
$Scopes = (Find-MgGraphCommand -Command New-MgUser | Select-Object Permissions).Permissions
Connect-MgGraph -Scopes User.ReadWrite.all, Domain.Read.All -TenantId $TenantID
####################################################################################
# Gathering
####################################################################################
$PrefDomain = (Get-MgDomain | Where-Object {$_.IsDefault -eq $true}).Id
####################################################################################
1..$TestUsers | foreach {
# Create Password Profile
  $PasswordProfile = @{
    Password = Get-RandomPassword -PasswordLength 12
    ForceChangePasswordNextSignIn = $true
    ForceChangePasswordNextSignInWithMfa = $true
  }
  $Passw = $PasswordProfile.Password
  # Creating DisplayName
  $NumCount = $_
  $BaseUsFirstName = "Test"
  $BaseUsLastName = "User"
  $DisplayName = $BaseUsFirstName + " " + $BaseUsLastName + $NumCount
  Write-Host "$($DisplayName)"
  # Creating MailNickName
  $MailNickName = $BaseUsFirstName + $BaseUsLastName + $NumCount
  # Creating UserPrincipamName
  $UserPrincipalName = $MailNickName + "@" + $PrefDomain
  # Creating User
  New-MgUser -DisplayName $DisplayName -PasswordProfile $PasswordProfile -AccountEnabled -userPrincipalName $UserPrincipalName -MailNickName $MailNickName
  $result = New-Object -TypeName psobject
  $Result | Add-Member -Name DisplayName -MemberType NoteProperty -Value $DisplayName
  $Result | Add-Member -Name UserPrincipalName -MemberType NoteProperty -Value $UserPrincipalName
  $result | Add-Member -Name Password -MemberType NoteProperty -Value $Passw
  $Results.add($Result) | Out-Null
}
# exporting data
$FileName = "testusers"+"-"+$($PrefDomain).csv
$Results | Select-Object * | Export-csv -Path C:\temp\$FileName -Delimiter ";"
Notepad.exe C:\Temp\$FileName

No comments:

Post a Comment