Functions/Get-GSuiteUser.ps1
<#
.SYNOPSIS This function retrieves one GSuite user or all GSuite users in a domain. This function returns either PSCustomObject when there is one user, or Object[] when there is more than one user. #> function Get-GSuiteUser { [CmdletBinding(PositionalBinding=$false)] [OutputType([Object[]], [PSCustomObject])] param ( # The primary email address for the user. [Parameter(Mandatory=$true, ParameterSetName="Email")] [ValidateNotNullOrEmpty()] [String]$primaryEmailAddress, # The domain for the users. [Parameter(Mandatory=$true, ParameterSetName="Domain")] [ValidateNotNullOrEmpty()] [String]$domain, # Whether to retrieve all the GSuite users. [Parameter(Mandatory=$true, ParameterSetName="All")] [ValidateNotNull()] [Switch]$all ) # Validate that the 'connection' has been established and the user access token exists Assert-GSuiteConnection -Scope "User" # Retrieve the user with the specified email address if ($primaryEmailAddress) { $Uri = "https://www.googleapis.com/admin/directory/v1/users/$($primaryEmailAddress)" } # Retrieve all the users in the specified domain elseif ($domain) { $Uri = "https://www.googleapis.com/admin/directory/v1/users?domain=$($domain)" } # Retrieve all the users else { $Uri = "https://www.googleapis.com/admin/directory/v1/users?customer=my_customer" } # Prepare the REST call parameters $invokeRestMethodParams = @{ Uri = $Uri Method = "GET" Headers = @{ Accept = "application/json" Authorization = "Bearer $($Global:GSuiteAccessTokensHashTable.User)" } } # Invoke the REST call $response = Invoke-RestMethod @invokeRestMethodParams # Return the user if $primaryEmailAddress is specified if ($primaryEmailAddress) { return $response } # Return the users if $domain or $all is specified return $response.users } |