Functions/ConvertFrom-GSuiteEndpoint.ps1

<#
.SYNOPSIS
    This function converts a single set of passwords separated by a symbol into a hash table containing password names and values.
#>

function ConvertFrom-GSuiteEndpoint {
    [CmdletBinding(PositionalBinding=$true)]
    [OutputType([Hashtable])]
    param (
        # The GSuite credentials containing the application ID as username, client secret and refresh tokens as password.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        $GSuiteCredentials
    )

    # Initialize the credentials hash table
    $credentialsHashTable = @{}

    # Extract application ID and the list of passwords from the endpoint object
    $applicationId = $GSuiteCredentials.Credential.Username
    $credentialsHashTable.ApplicationID = $applicationId
    $passwords = $GSuiteCredentials.Credential.GetNetworkCredential().Password

    # Split the password by spaces
    $splitPasswords = $passwords.Split(" ")

    # Process each password
    foreach ($password in $splitPasswords) {
        # A valid password should be in the form of "name:password". Example: "user:TheUsersPassword".
        if ($password -notMatch "([\s\S]*):([\s\S]*)") {
            Write-Error "The password '$($password)' is invalid."
            return $null
        }

        # Retrieve the name and the password from the match result
        $passwordName = $Matches[1]
        $passwordValue = $Matches[2]

        # Update the hash table
        $credentialsHashTable.($passwordName) = $passwordValue
    }
    return $credentialsHashTable
}