Functions/New-GSuiteEndpointConfiguration.ps1

<#
.SYNOPSIS
    This function creates a GSuite Endpoint Configuration given a GSuite application id, client secret and refresh tokens.
#>

function New-GSuiteEndpointConfiguration {
    [CmdletBinding(PositionalBinding=$true)]
    [OutputType([Object])]
    param (
        # The username to be stored in the new endpoint.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$GSuiteApplicationID,

        # The client secret of the GSuite application.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$GSuiteClientSecret,

        # The refresh token which has a scope of 'https://www.googleapis.com/auth/admin.directory.user'
        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [String]$UserRefreshToken,

        # The refresh token which has a scope of 'https://www.googleapis.com/auth/admin.directory.group'
        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [String]$GroupRefreshToken,

        # The refresh token which has a scope of 'https://www.googleapis.com/auth/admin.directory.orgunit'
        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [String]$OrganizationalUnitRefreshToken,

        # The refresh token which has a scope of 'https://www.googleapis.com/auth/admin.directory.userschema'
        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [String]$UserSchemaRefreshToken,

        # The refresh token which has a scope of 'https://www.googleapis.com/auth/admin.directory.device.mobile'
        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [String]$MobileDeviceRefreshToken,

        # The refresh token which has a scope of 'https://www.googleapis.com/auth/admin.directory.user.security'
        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [String]$SecurityRefreshToken,

        # The refresh token which has a scope of 'https://www.googleapis.com/auth/admin.directory.customer'
        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [String]$CustomerRefreshToken,

        # The refresh token which has a scope of 'https://www.googleapis.com/auth/admin.directory.domain'
        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [String]$DomainRefreshToken
    )

    # Append the refresh tokens to the client secret
    $GSuitePassword = "ClientSecret:$($GSuiteClientSecret)"
    if (![String]::IsNullOrWhiteSpace($UserRefreshToken)) {
        $GSuitePassword += " UserRefreshToken:$($UserRefreshToken)"
    }
    if (![String]::IsNullOrWhiteSpace($GroupRefreshToken)) {
        $GSuitePassword += " GroupRefreshToken:$($GroupRefreshToken)"
    }
    if (![String]::IsNullOrWhiteSpace($OrganizationalUnitRefreshToken)) {
        $GSuitePassword += " OrganizationalUnitRefreshToken:$($OrganizationalUnitRefreshToken)"
    }
    if (![String]::IsNullOrWhiteSpace($UserSchemaRefreshToken)) {
        $GSuitePassword += " UserSchemaRefreshToken:$($UserSchemaRefreshToken)"
    }
    if (![String]::IsNullOrWhiteSpace($MobileDeviceRefreshToken)) {
        $GSuitePassword += " MobileDeviceRefreshToken:$($MobileDeviceRefreshToken)"
    }
    if (![String]::IsNullOrWhiteSpace($SecurityRefreshToken)) {
        $GSuitePassword += " SecurityRefreshToken:$($SecurityRefreshToken)"
    }
    if (![String]::IsNullOrWhiteSpace($CustomerRefreshToken)) {
        $GSuitePassword += " CustomerRefreshToken:$($CustomerRefreshToken)"
    }
    if (![String]::IsNullOrWhiteSpace($DomainRefreshToken)) {
        $GSuitePassword += " DomainRefreshToken:$($DomainRefreshToken)"
    }

    # Initialize a configuration for GSuite
    $importConfiguration = New-Object -TypeName ManagementProxy.ManagementService.GenericConfiguration -Property @{
        "Url"                          = "https://admin.google.com";
        "Username"                     = $GSuiteApplicationID;
        "Password"                     = $GSuitePassword;
        "UseAdministrativeCredentials" = $true;
    }

    return $importConfiguration
}