Functions/New-GSuiteUser.ps1


<#
.SYNOPSIS
    This function creates a GSuite user.
#>

function New-GSuiteUser {
    [CmdletBinding(PositionalBinding=$false)]
    [OutputType([Object])]
    param (
        # The access token.
        # It has to be generated using a refresh token, from Get-GSuiteAccessToken
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$token,

        # The first name for the user.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$firstName,

        # The last name for the user.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$lastName,

        # The primary email address for the user.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$primaryEmailAddress,

        # The password for the user.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$password
    )

    # Prepare REST call parameters
    $invokeRestMethodParams = @{
        Uri     = "https://www.googleapis.com/admin/directory/v1/users"
        Method  = "POST"
        Headers = @{
            "Content-Type" = "application/json"
            Accept         = "application/json"
            Authorization  = "Bearer $($token)"
        }
        Body    = @{
            name         = @{
                familyName = $lastName
                givenName  = $firstName
            }
            password     = $password
            primaryEmail = $primaryEmailAddress
        } | ConvertTo-Json
    }

    # Invoke the REST call
    return Invoke-RestMethod @invokeRestMethodParams
}