Powershell Script to create users and assign to roles

Here's a snip-it from a PowerShell script that I've been working on for my organization. I've automated the creation of new users in our environment via a PS script that creates their AD and Email accounts as well as now creating their Genesys Cloud account and adding them to the base roles for our org. The only thing that I haven't gotten this to do yet is to figure out how to set the location of the user. I've scoured documentation but don't see any API/CLI commands for adding a location to a user account. This is especially important as that is how our E911 is programmed. I have a ticket open with my vendor on this and am hoping to get some insight soon and will update this when I get an answer.

For my internal usage I have all of the variable pulling data from a master CSV file with all the relevant data filled out for each user that way I can bulk add multiple users. But for the sake of keeping this simple I've just blanked out the variables in this example and didn't include the foreach loop that would be needed for multiple users.


# Gather all variables needed for account creation. These could be pulled from a CSV or manually entered in the script. Create foreach loop for running through multiple users via a CSV. 
$PhoneNumber = ""
$Number = "+1$PhoneNumber"
$extension = ""
$email = ""
$DisplayName = ""
$Title = ""
$Department = ""
$DivisionID = ""
$Password = ""

# Create the user account with a 10 digit DID and Extension. This uses the US country code. Will need to adjust if in another country. 
$NewGCUser = @{
    addresses = @{
        address = $Number
        countryCode = "US"
        mediaType = "PHONE"
        type = "WORK"
        },
        @{
        countryCode = "US"
        extension = $extension
        mediaType = "PHONE"
        type = "WORK2"
        }
    department = $Department
    divisionID = $DivisionID
    email = $email
    name = $DisplayName
    title = $JobTitle
    state = "active"
    password = $Password
}

#Convert above array into JSON for importing into Genesys Cloud
$NewGCUser | ConvertTo-Json | Out-File C:\Temp\GCNewUser.txt -Force

gc.exe users create -f "C:\Temp\GCNewUser.txt"


#Give above time to process before adding roles to the account
Start-Sleep -Seconds 5

#Pull the new user's ID from Genesys Cloud
$GCUserID = (gc.exe users list --autopaginate --filtercondition="email==$email" | ConvertFrom-Json).id

#Specify what roles to add to users by default. Add as many or as few as needed, just specify the Role's ID inside the quotes.
$Roles = (
        "RoleID#1",
        "RoleID#2",
        "RoleID#3"  
    )

#Convert above variable string into JSON for importing into Genesys Cloud
$Roles | ConvertTo-Json | Out-File C:\temp\GCRoles.txt -force

gc.exe users roles update $GCuserID -f C:\temp\GCRoles.txt

The PATCH api looks to include location Id for user patch user

The gc command will be users update {userId}
You will need to build body.

FYI, not sure if you come across this but you can do inline Json to a powershell variable to pass to GC methods, rather than needing to create a file each time.
The gotcha on that is you need to use powershell 7

1 Like

Ohh my, thank you so much for that! It took me a while to get familiar with how to build the body but I was able to get this implemented!

I have seen some references regarding PowerShell 7 being able to do that but I haven't used it too much yet. I have it installed but primarily use ISE still. Now that I have this working pretty well I may go ahead and try to clean it up using PowerShell 7.

See below for updated code where it will also set the user's location.

# Gather all variables needed for account creation. These could be pulled from a CSV or manually entered in the script. Create foreach loop for running through multiple users via a CSV. 
$PhoneNumber = ""
$Number = "+1$PhoneNumber"
$extension = ""
$email = ""
$DisplayName = ""
$Title = ""
$Department = ""
$DivisionID = ""
$Password = ""
$LocationID = ""

# Create the user account with a 10 digit DID and Extension. This uses the US country code. Will need to adjust if in another country. 
$NewGCUser = @{
    addresses = @{
        address = $Number
        countryCode = "US"
        mediaType = "PHONE"
        type = "WORK"
        },
        @{
        countryCode = "US"
        extension = $extension
        mediaType = "PHONE"
        type = "WORK2"
        }
    department = $Department
    divisionID = $DivisionID
    email = $email
    name = $DisplayName
    title = $JobTitle
    state = "active"
    password = $Password
}

#Convert above array into JSON for importing into Genesys Cloud
$NewGCUser | ConvertTo-Json | Out-File C:\Temp\GCNewUser.txt -Force

gc.exe users create -f "C:\Temp\GCNewUser.txt"


#Give above time to process before adding roles to the account
Start-Sleep -Seconds 5

#Pull the new user's data from Genesys Cloud
$GCUser = gc.exe users list --autopaginate --filtercondition="email==$email" | ConvertFrom-Json

#Specify what roles to add to users by default. Add as many or as few as needed, just specify the Role's ID inside the quotes.
$Roles = (
        "RoleID#1",
        "RoleID#2",
        "RoleID#3" 
    )

#Convert above variable string into JSON for importing into Genesys Cloud.
$Roles | ConvertTo-Json | Out-File C:\temp\GCRoles.txt -force

gc.exe users roles update $GCuser.id -f C:\temp\GCRoles.txt


#Set the user's location. This is useful if you use locations for E911 routing. 
$Location = @{
    locations = 
        @(
        @{id = @LocationID}
        ) 
     version = $GCUser.version
}

$Location | ConvertTo-Json | Out-File C:\Temp\GCLocation.txt -force 

gc.exe users update $GCUser.id -f C:\Temp\GCLocation.txt


Glad could help

Various ways to pass to gc.exe
With pipe you can pass json directly, e.g.

$Location | Convert-To-Json | gc.exe users update $GCUser.id

I use VSCode for powershell 7. Install powershell extension(s).
You can debug through script and watch variables, set breakpoints etc.

1 Like

That does look a lot cleaner. I will for sure start experimenting with powershell 7 now that I have it all working.

@SimonBrown Do you know of a way to create a WebRTC phone through the CLI? I would love to be able to have this script create and set the WebRTC phone as the user's default device. I'm pretty sure I've seen the command to set the default device but I haven't seen anything regarding actually creating the phone.

Nevermind, I do believe I've found them. They were just buried where I wasn't expecting and searching turned up no results. Will have an updated script shortly with creation and assigning of default phone to WebRTC.

Create a phone (genesys.cloud)

Alright, I've managed to get the creation of the user's WebRTC phone as well as setting that new phone as the user's default included in the script. See below for the completed script. At this point I'm not sure there's much else I need this to do for my purposes. Hopefully this will help others as they dive into the CLI and start customizing for their environments.

# Gather all variables needed for account creation. These could be pulled from a CSV or manually entered in the script. Create foreach loop for running through multiple users via a CSV. 
$PhoneNumber = ""
$Number = "+1$PhoneNumber"
$extension = ""
$email = ""
$DisplayName = ""
$Title = ""
$Department = ""
$DivisionID = ""
$Password = ""
$LocationID = ""
$phoneBaseSettingsID = ""
$siteID = ""
$lineBaseSettingsID = ""

# Create the user account with a 10 digit DID and Extension. This uses the US country code. Will need to adjust if in another country. 
$NewGCUser = @{
    addresses = @{
        address = $Number
        countryCode = "US"
        mediaType = "PHONE"
        type = "WORK"
        },
        @{
        countryCode = "US"
        extension = $extension
        mediaType = "PHONE"
        type = "WORK2"
        }
    department = $Department
    divisionID = $DivisionID
    email = $email
    name = $DisplayName
    title = $JobTitle
    state = "active"
    password = $Password
}

#Convert above array into JSON for importing into Genesys Cloud
$NewGCUser | ConvertTo-Json | Out-File C:\Temp\GCNewUser.txt -Force

gc.exe users create -f "C:\Temp\GCNewUser.txt"


#Give above time to process before adding roles to the account
Start-Sleep -Seconds 5

#Pull the new user's data from Genesys Cloud
$GCUser = gc.exe users list --autopaginate --filtercondition="email==$email" | ConvertFrom-Json

#Specify what roles to add to users by default. Add as many or as few as needed, just specify the Role's ID inside the quotes.
$Roles = (
        "RoleID#1",
        "RoleID#2",
        "RoleID#3" 
    )

#Convert above variable string into JSON for importing into Genesys Cloud.
$Roles | ConvertTo-Json | Out-File C:\temp\GCRoles.txt -force

gc.exe users roles update $GCuser.id -f C:\temp\GCRoles.txt


#Set the user's location. This is useful if you use locations for E911 routing. 
$Location = @{
    locations = 
        @(
        @{id = @LocationID}
        ) 
     version = $GCUser.version
}

$Location | ConvertTo-Json | Out-File C:\Temp\GCLocation.txt -force 

gc.exe users update $GCUser.id -f C:\Temp\GCLocation.txt



#Create user's WebRTC Phone
$WebRTCPhone = @{
    name = "$DisplayName - WebRTC"
    phoneBaseSettings = @{
        id = $phoneBaseSettingsID
    }
    webRtcUser = @{
        id = $GCUser.id
    }
    site = @{
        id = $siteID   
    }
    lines = @( 
        @{ 
        name = "Line1"
        lineBaseSettings = @{
            id = $lineBaseSettingsID
        }
        }
    )
}

$WebRTCPhone | ConvertTo-Json -depth 3 | Out-File C:\Temp\WebRTCPhone.txt -force

gc.exe telephony providers edges phones create -f C:\Temp\WebRTCPhone.txt


#Pull the new WebRTC Phone's Station ID and set as user's default station.
$WebRTCStation = gc.exe stations list -a --filtercondition="name contains $GCUsersName" | ConvertFrom-Json

gc.exe users station defaultstation update $GCUser.id $WebRTCStation.id



Glad you resolving.
Would suggest you need to include error handling as well on the gc commands.
on the gc command, pipe out the response with something like
gc.exe command prompts..... 2>&1 | Set-Variable -Name response

Then PS script to handle error, this example just writes out

# Check for error condition
    if ($LASTEXITCODE -ne 0) {  
        $errorResponse = $response | ConvertFrom-Json
        Write-Output "Had error with create, code $($errorResponse.code) and status $($errorResponse.status) and message: $($errorResponse.message)"
    }

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.