From 32ba41f6233d2a9f256f1fa7c7268fd44d738b57 Mon Sep 17 00:00:00 2001 From: Chris O'Sullivan Date: Tue, 6 Oct 2020 14:39:20 +1000 Subject: [PATCH 1/2] Contacts --- PSGSuite/Public/Contacts/Get-GSContactList.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/PSGSuite/Public/Contacts/Get-GSContactList.ps1 b/PSGSuite/Public/Contacts/Get-GSContactList.ps1 index 276d4050..a3fe28e2 100644 --- a/PSGSuite/Public/Contacts/Get-GSContactList.ps1 +++ b/PSGSuite/Public/Contacts/Get-GSContactList.ps1 @@ -14,6 +14,9 @@ Function Get-GSContactList { .EXAMPLE Get-GSContactList -User user@domain.com + .NOTES + Updated: 6 Oct 2020 by Chris O'Sullivan / CompareCloud + Updates: Added Organization details to output object #> [cmdletbinding()] Param @@ -60,6 +63,9 @@ Function Get-GSContactList { FamilyName = $i.name.familyName EmailAddresses = $(if($i.email.address){$i.email.address}else{$null}) PhoneNumber = $i.phonenumber + JobTitle = $i.organization.orgTitle #added by CompareCloud 6 Oct 2020 + Department = $i.organization.orgDepartment #added by CompareCloud 6 Oct 2020 + CompanyName = $i.organization.orgName #added by CompareCloud 6 Oct 2020 Updated = $i.updated Edited = $i.edited.'#text' Path = $(if($i.email.rel){$i.email.rel}else{$null}) From de3d5aec5d88f1c6bae0b7443228238d46d4ce30 Mon Sep 17 00:00:00 2001 From: Chris O'Sullivan Date: Tue, 6 Oct 2020 14:51:34 +1000 Subject: [PATCH 2/2] Contacts --- PSGSuite/Public/Contacts/New-GSContact.ps1 | 109 ++++++++++++++++++ .../Public/Helpers/Add-GSContactEmail.ps1 | 97 ++++++++++++++++ .../Helpers/Add-GSContactOrganization.ps1 | 89 ++++++++++++++ 3 files changed, 295 insertions(+) create mode 100644 PSGSuite/Public/Contacts/New-GSContact.ps1 create mode 100644 PSGSuite/Public/Helpers/Add-GSContactEmail.ps1 create mode 100644 PSGSuite/Public/Helpers/Add-GSContactOrganization.ps1 diff --git a/PSGSuite/Public/Contacts/New-GSContact.ps1 b/PSGSuite/Public/Contacts/New-GSContact.ps1 new file mode 100644 index 00000000..f3ee578e --- /dev/null +++ b/PSGSuite/Public/Contacts/New-GSContact.ps1 @@ -0,0 +1,109 @@ +function New-GSContact { + <# + .SYNOPSIS + Creates a new G Suite Contact + + .DESCRIPTION + Creates a new G Suite Contact + + .PARAMETER GivenName + The given (first) name of the Contact + + .PARAMETER FamilyName + The family (last) name of the Contact + + .PARAMETER Emails + The email objects of the Contact + + This parameter expects a 'Google.Apis.PeopleService.v1.Data.EmailAddress[]' object type. You can create objects of this type easily by using the function 'Add-GSContactEmail' + + .PARAMETER Organizations + The organization objects of the Contact + + This parameter expects a 'Google.Apis.PeopleService.v1.Data.Organization[]' object type. You can create objects of this type easily by using the function 'Add-GSContactOrganization' + + .EXAMPLE + $emails = Add-GSContactEmail -Type work -Address jsmith@contoso.com -DisplayName "John Smith | Google" + $organizations = Add-GSContactOrganization -Title "Sales Manager" -Department "Sales" -Name "Google" + + New-GSContact -GivenName John -FamilyName Smith -Emails $email -Organizations $organizations + + Creates a contact named John Smith and adds their work address, work phone, login_id and alternate non gsuite work email to the Contact object. + + .NOTES + Author: Chris O'Sullivan / CompareCloud + Acknowledgements: Used existing New-GSUser function scructure and format to maintain convention + Date: 6 Oct 2020 + Version: 1.0 + #> + [OutputType('Google.Apis.PeopleService.v1.Data.Person')] + [cmdletbinding()] + Param + ( + [parameter(Mandatory = $true)] + [String] + $GivenName, + [parameter(Mandatory = $true)] + [String] + $FamilyName, + [parameter(Mandatory = $false)] + [Google.Apis.PeopleService.v1.Data.EmailAddress[]] + $EmailAddresses, + [parameter(Mandatory = $false)] + [Google.Apis.PeopleService.v1.Data.Organization[]] + $Organizations + ) + Begin { + $serviceParams = @{ + Scope = 'https://www.googleapis.com/auth/contacts' + ServiceType = 'Google.Apis.PeopleService.v1.PeopleServiceService' + } + $service = New-GoogleService @serviceParams + } + Process { + try { + Write-Verbose "Creating contact '$PrimaryEmail'" + $body = New-Object 'Google.Apis.PeopleService.v1.Data.Person' + $name = New-Object 'Google.Apis.PeopleService.v1.Data.Name' -Property @{ + GivenName = $GivenName + FamilyName = $FamilyName + } + $nameList = New-Object 'System.Collections.Generic.List`1[Google.Apis.PeopleService.v1.Data.Name]' + $nameList.Add($name) + + $body.Names = $nameList + foreach ($prop in $PSBoundParameters.Keys | Where-Object {$body.PSObject.Properties.Name -contains $_}) { + switch ($prop) { + EmailAddresses { + $emailList = New-Object 'System.Collections.Generic.List`1[Google.Apis.PeopleService.v1.Data.EmailAddress]' + foreach ($email in $EmailAddresses) { + $emailList.Add($email) + } + $body.EmailAddresses = $emailList + } + Organizations { + $orgList = New-Object 'System.Collections.Generic.List`1[Google.Apis.PeopleService.v1.Data.Organization]' + foreach ($organization in $Organizations) { + $orgList.Add($organization) + } + $body.Organizations = $orgList + } + Default { + $body.$prop = $PSBoundParameters[$prop] + } + } + } + $request = $service.People.CreateContact($body) + $request.Execute() + } + catch { + if ($ErrorActionPreference -eq 'Stop') { + $PSCmdlet.ThrowTerminatingError($_) + } + else { + Write-Error $_ + } + } + } +} + diff --git a/PSGSuite/Public/Helpers/Add-GSContactEmail.ps1 b/PSGSuite/Public/Helpers/Add-GSContactEmail.ps1 new file mode 100644 index 00000000..80e17380 --- /dev/null +++ b/PSGSuite/Public/Helpers/Add-GSContactEmail.ps1 @@ -0,0 +1,97 @@ +function Add-GSContactEmail { + <# + .SYNOPSIS + Builds a Email object to use when creating or updating a contact + + .DESCRIPTION + Builds a Email object to use when creating or updating a contact + + .PARAMETER Address + The contact's email address. Also serves as the email ID. This value can be the contact's primary email address or an alias. + + .PARAMETER Type + The type of the email account. + + Acceptable values are: + * "home" + * "other" + * "work" + + .PARAMETER InputObject + Used for pipeline input of an existing Email object to strip the extra attributes and prevent errors + + .EXAMPLE + $emails = Add-GSContactEmail -Type work -Address jsmith@contoso.com -DisplayName "John Smith | Google" + $organizations = Add-GSContactOrganization -Title "Sales Manager" -Department "Sales" -Name "Google" + + New-GSContact -GivenName John -FamilyName Smith -Emails $email -Organizations $organizations + + Creates a contact named John Smith and adds their work address, work phone, login_id and alternate non gsuite work email to the Contact object. + + .NOTES + Author: Chris O'Sullivan / CompareCloud + Acknowledgements: Used existing Add-GSUserEmail function scructure and format to maintain convention + Date: 6 Oct 2020 + Version: 1.0 + #> + [OutputType('Google.Apis.PeopleService.v1.Data.EmailAddress')] + [CmdletBinding()] + Param + ( + [Parameter(Mandatory = $false, ParameterSetName = "Fields")] + [String] + $DisplayName, + [Parameter(Mandatory = $false, ParameterSetName = "Fields")] + [String] + $Value, + [Parameter(Mandatory = $false, ParameterSetName = "Fields")] + [ValidateSet('home', 'other', 'work')] + [String] + $Type, + [Parameter(Mandatory = $false, ValueFromPipeline = $true, ParameterSetName = "InputObject")] + [Google.Apis.PeopleService.v1.Data.EmailAddress[]] + $InputObject + ) + Begin { + $propsToWatch = @( + 'DisplayName' + 'Value' + 'Type' + ) + } + Process { + try { + switch ($PSCmdlet.ParameterSetName) { + Fields { + $obj = New-Object 'Google.Apis.PeopleService.v1.Data.EmailAddress' + foreach ($prop in $PSBoundParameters.Keys | Where-Object {$obj.PSObject.Properties.Name -contains $_}) { + if ($prop -eq 'Type') { + $obj.$prop = $PSBoundParameters[$prop].ToLower() + } + else { + $obj.$prop = $PSBoundParameters[$prop] + } + } + $obj + } + InputObject { + foreach ($iObj in $InputObject) { + $obj = New-Object 'Google.Apis.PeopleService.v1.Data.EmailAddress' + foreach ($prop in $iObj.PSObject.Properties.Name | Where-Object {$obj.PSObject.Properties.Name -contains $_ -and $propsToWatch -contains $_}) { + $obj.$prop = $iObj.$prop + } + $obj + } + } + } + } + catch { + if ($ErrorActionPreference -eq 'Stop') { + $PSCmdlet.ThrowTerminatingError($_) + } + else { + Write-Error $_ + } + } + } +} diff --git a/PSGSuite/Public/Helpers/Add-GSContactOrganization.ps1 b/PSGSuite/Public/Helpers/Add-GSContactOrganization.ps1 new file mode 100644 index 00000000..7c70e275 --- /dev/null +++ b/PSGSuite/Public/Helpers/Add-GSContactOrganization.ps1 @@ -0,0 +1,89 @@ +function Add-GSContactOrganization { + <# + .SYNOPSIS + Builds a Organization object to use when creating or updating a Contact + + .DESCRIPTION + Builds a Organization object to use when creating or updating a Contact + + .PARAMETER Department + Department within the organization + + .PARAMETER Name + Name of the organization + + .PARAMETER Title + Title (designation) of the user in the organization + + .PARAMETER InputObject + Used for pipeline input of an existing UserExternalId object to strip the extra attributes and prevent errors + + .EXAMPLE + $emails = Add-GSContactEmail -Type work -Address jsmith@contoso.com -DisplayName "John Smith | Google" + $organizations = Add-GSContactOrganization -Title "Sales Manager" -Department "Sales" -Name "Google" + + New-GSContact -GivenName John -FamilyName Smith -Emails $email -Organizations $organizations + + Creates a contact named John Smith and adds their work address, work phone, login_id and alternate non gsuite work email to the Contact object. + + .NOTES + Author: Chris O'Sullivan / CompareCloud + Acknowledgements: Used existing Add-GSUserOrganization function scructure and format to maintain convention + Date: 6 Oct 2020 + Version: 1.0 + #> + [OutputType('Google.Apis.PeopleService.v1.Data.Organization')] + [CmdletBinding(DefaultParameterSetName = "InputObject")] + Param + ( + [Parameter(Mandatory = $false, ParameterSetName = "Fields")] + [String] + $Department, + [Parameter(Mandatory = $false, ParameterSetName = "Fields")] + [String] + $Name, + [Parameter(Mandatory = $false, ParameterSetName = "Fields")] + [String] + $Title, + [Parameter(Mandatory = $false, ValueFromPipeline = $true, ParameterSetName = "InputObject")] + [Google.Apis.PeopleService.v1.Data.Organization[]] + $InputObject + ) + Begin { + $propsToWatch = @( + 'Department' + 'Name' + 'Title' + ) + } + Process { + try { + switch ($PSCmdlet.ParameterSetName) { + Fields { + $obj = New-Object 'Google.Apis.PeopleService.v1.Data.Organization' + foreach ($prop in $PSBoundParameters.Keys | Where-Object {$obj.PSObject.Properties.Name -contains $_}) { + $obj.$prop = $PSBoundParameters[$prop] + } + $obj + } + InputObject { + foreach ($iObj in $InputObject) { + $obj = New-Object 'Google.Apis.PeopleService.v1.Data.Organization' + foreach ($prop in $iObj.PSObject.Properties.Name | Where-Object {$obj.PSObject.Properties.Name -contains $_ -and $propsToWatch -contains $_}) { + $obj.$prop = $iObj.$prop + } + $obj + } + } + } + } + catch { + if ($ErrorActionPreference -eq 'Stop') { + $PSCmdlet.ThrowTerminatingError($_) + } + else { + Write-Error $_ + } + } + } +}