Home SharingIsCaring. Create test users with Microsoft Graph API and Chat GPT
Post
Cancel

SharingIsCaring. Create test users with Microsoft Graph API and Chat GPT

Summary

This script will create test users with Microsoft Graph API and Chat GPT. Users can be created from a list of users or from a list of users generated by OpenAI API.

Pre-requisites

  • Microsoft.Graph.Sites PowerShell module
  • An Open AI API key which can be done by creating an account for using Open AI API
    • Once the key is obatined, set the value of $openai_api_key in the script with that key

    Example

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
# Check if Microsoft.Graph module is already installed, if not install it
if (-not(Get-Module Microsoft.Graph)) {
    Install-Module -Name Microsoft.Graph
}
# Connect to Microsoft Graph API with admin credentials
Connect-MgGraph -Scopes "User.ReadWrite.All"
# Configure variables
$domain = "mydomain.onmicrosoft.com" # change to your domain
$askOpenAI = $true # change to $false if you want to use the list of users above
$numUsers = 20 # change to integer value
$openai_api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
# Get list of users from OpenAI API or use the list of users above
if ($askOpenAI -eq $true) {
    $numUsers = 20 # change to integer value
    $users = Get-ListOfUsers -numUsers $numUsers -openai_api_key $openai_api_key
}else {
    # List of users to create
    $users = @(
        "Alice Smith",
        "Bob Johnson",
        "Charlie Brown"
    )
    # Number of users to create, change value to any number you want, just not exceeding the number of users in the list
    $numUsers = $users.Count # change to integer value
}
# list of users to create in the format of "FirstName LastName" using $numUsers
$users = $users | Select-Object -First $numUsers
# Loop through the list of users and create their accounts
foreach ($user in $users) {
 
    $name = $user.Split(" ")
    $givenName = $name[0].Trim()
    $surname = $name[1].Trim()
    $displayName = $user.Trim()
    $userPrincipalName = $name[0].ToLower() + "." + $name[1].ToLower() + "@" + $domain
    $mailNickName = $givenName.Substring(0,3).ToLower() + $surname.Substring(0,3).ToLower() + (Get-Random -Minimum 100 -Maximum 999).ToString()
    $PasswordProfile = @{
        Password = 'MyPassword123!'
        }
    $user = New-MgUser -AccountEnabled `
        -DisplayName $displayName `
        -MailNickName $mailNickName `
        -givenName $givenName `
        -surname $surname `
        -userPrincipalName $userPrincipalName `
        -PasswordProfile $PasswordProfile 
    Write-Host "User created: $($user.DisplayName) ($($user.UserPrincipalName))"
}
# function to get user names from a list of users in format "FirstName LastName"
function Get-ListOfUsers {
    
    param(
        [Parameter(Mandatory = $true)]
        [string]$numUsers
        [Parameter(Mandatory = $true)]
        [string]$openai_api_key
    )
    # $openai_api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $openai_api_endpoint = "https://api.openai.com/v1/chat/completions";
    $data = @{}
    $data["model"] = "gpt-3.5-turbo";
    $data["messages"] = @(@{});
    $data["messages"][0]["role"] = "user";
    $messageContent = "create list of test users in format 'FirstName LastName' separated by :. Number or records to create: $numUsers ";
    $data["messages"][0]["content"] = $messageContent;
    
    $headers = @{
        "Content-Type"  = "application/json"
        "Authorization" = "Bearer " + $openai_api_key
    }
    Write-Host "Calling OpenAI API to get list of users.";
    $response = Invoke-WebRequest -Method Post -Uri $openai_api_endpoint -Headers $headers -Body ($data | ConvertTo-Json);
    if ($response -and $response.StatusCode -eq 200) {
        $responseObject = $response.Content | ConvertFrom-Json
        $result  = $responseObject.choices[0].message.content.Split(",")
        $result  = $result.split(":")
        
        } else {
        $result = $null;
    }
    return $result;
}

PNP Script sample site

Script sample site

This post is licensed under CC BY 4.0 by the author.

Get teams tabs and export to CSV

CI/CD for SPFx Deployment in Azure DevOps using M365 CLI