From 38f9ed12545858d3d403cd77571be7263922e484 Mon Sep 17 00:00:00 2001 From: mW Date: Tue, 11 Dec 2018 14:13:00 +1000 Subject: [PATCH 1/4] Added UserRelation --- .../Public/Helpers/Add-GSUserRelation.ps1 | 96 +++++++++++++++++++ PSGSuite/Public/Users/Update-GSUser.ps1 | 10 ++ 2 files changed, 106 insertions(+) create mode 100644 PSGSuite/Public/Helpers/Add-GSUserRelation.ps1 diff --git a/PSGSuite/Public/Helpers/Add-GSUserRelation.ps1 b/PSGSuite/Public/Helpers/Add-GSUserRelation.ps1 new file mode 100644 index 00000000..7d8d95da --- /dev/null +++ b/PSGSuite/Public/Helpers/Add-GSUserRelation.ps1 @@ -0,0 +1,96 @@ +function Add-GSUserRelation { + <# + .SYNOPSIS + Builds a Relation object to use when creating or updating a User + + .DESCRIPTION + Builds a Relation object to use when creating or updating a User + + .PARAMETER CustomType + If the external ID type is custom, this property holds the custom type + + .PARAMETER Type + The type of the organization. + + If using a CustomType + + .PARAMETER Value + The value of the ID + + .PARAMETER InputObject + Used for pipeline input of an existing UserExternalId object to strip the extra attributes and prevent errors + + .EXAMPLE + $address = Add-GSUserAddress -Country USA -Locality Dallas -PostalCode 75000 Region TX -StreetAddress '123 South St' -Type Work -Primary + + $phone = Add-GSUserPhone -Type Work -Value "(800) 873-0923" -Primary + + $extId = Add-GSUserExternalId -Type Login_Id -Value jsmith2 + + $email = Add-GSUserEmail -Type work -Address jsmith@contoso.com + + New-GSUser -PrimaryEmail john.smith@domain.com -GivenName John -FamilyName Smith -Password (ConvertTo-SecureString -String 'Password123' -AsPlainText -Force) -ChangePasswordAtNextLogin -OrgUnitPath "/Users/New Hires" -IncludeInGlobalAddressList -Addresses $address -Phones $phone -ExternalIds $extId -Emails $email + + Creates a user named John Smith and adds their work address, work phone, login_id and alternate non gsuite work email to the user object. + #> + [CmdletBinding(DefaultParameterSetName = "InputObject")] + Param + ( + [Parameter(Mandatory = $false, ParameterSetName = "Fields")] + [String] + $CustomType, + [Parameter(Mandatory = $false, ParameterSetName = "Fields")] + [String] + $Type, + [Parameter(Mandatory = $false, ParameterSetName = "Fields")] + [String] + $Value, + [Parameter(Mandatory = $false, ParameterSetName = "Fields")] + [String] + $ETag, + [Parameter(Mandatory = $false, ValueFromPipeline = $true, ParameterSetName = "InputObject")] + [Google.Apis.Admin.Directory.directory_v1.Data.UserRelation[]] + $InputObject + ) + Begin { + $propsToWatch = @( + 'CustomType' + 'Type', + 'Value' + 'ETag' + ) + if ($PSBoundParameters.Keys -contains 'CustomType') { + $PSBoundParameters['Type'] = 'CUSTOM' + } + } + Process { + try { + switch ($PSCmdlet.ParameterSetName) { + Fields { + $obj = New-Object 'Google.Apis.Admin.Directory.directory_v1.Data.UserRelation' + 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.Admin.Directory.directory_v1.Data.UserRelation' + 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/Users/Update-GSUser.ps1 b/PSGSuite/Public/Users/Update-GSUser.ps1 index 63064e4a..c0d74424 100644 --- a/PSGSuite/Public/Users/Update-GSUser.ps1 +++ b/PSGSuite/Public/Users/Update-GSUser.ps1 @@ -146,6 +146,9 @@ function Update-GSUser { [Google.Apis.Admin.Directory.directory_v1.Data.UserOrganization[]] $Organizations, [parameter(Mandatory = $false)] + [Google.Apis.Admin.Directory.directory_v1.Data.UserRelation[]] + $Relations, + [parameter(Mandatory = $false)] [Google.Apis.Admin.Directory.directory_v1.Data.UserPhone[]] $Phones, [parameter(Mandatory = $false)] @@ -251,6 +254,13 @@ function Update-GSUser { } $body.Organizations = $orgList } + Relations { + $relList = New-Object 'System.Collections.Generic.List`1[Google.Apis.Admin.Directory.directory_v1.Data.UserRelation]' + foreach ($relation in $Relations) { + $relList.Add($relation) + } + $body.Relations = $relList + } Phones { $phoneList = New-Object 'System.Collections.Generic.List`1[Google.Apis.Admin.Directory.directory_v1.Data.UserPhone]' foreach ($phone in $Phones) { From 8768446c5df305a664cbb2ef5c2be34fd7368b4f Mon Sep 17 00:00:00 2001 From: mW Date: Tue, 11 Dec 2018 14:27:56 +1000 Subject: [PATCH 2/4] Added UserRelation to New-GSUser --- PSGSuite/Public/Users/New-GSUser.ps1 | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/PSGSuite/Public/Users/New-GSUser.ps1 b/PSGSuite/Public/Users/New-GSUser.ps1 index e45745b4..ea639ebd 100644 --- a/PSGSuite/Public/Users/New-GSUser.ps1 +++ b/PSGSuite/Public/Users/New-GSUser.ps1 @@ -50,6 +50,11 @@ function New-GSUser { This parameter expects a 'Google.Apis.Admin.Directory.directory_v1.Data.UserOrganization[]' object type. You can create objects of this type easily by using the function 'Add-GSUserOrganization' + .PARAMETER Relations + The relation objects of the user + + This parameter expects a 'Google.Apis.Admin.Directory.directory_v1.Data.UserRelation[]' object type. You can create objects of this type easily by using the function 'Add-GSUserRelation' + .PARAMETER Phones The phone objects of the user @@ -143,6 +148,9 @@ function New-GSUser { [Google.Apis.Admin.Directory.directory_v1.Data.UserOrganization[]] $Organizations, [parameter(Mandatory = $false)] + [Google.Apis.Admin.Directory.directory_v1.Data.UserRelation[]] + $Relations, + [parameter(Mandatory = $false)] [Google.Apis.Admin.Directory.directory_v1.Data.UserPhone[]] $Phones, [parameter(Mandatory = $false)] @@ -194,7 +202,7 @@ function New-GSUser { switch ($prop) { PrimaryEmail { if ($PSBoundParameters[$prop] -notlike "*@*.*") { - $PSBoundParameters[$prop] = "$($PSBoundParameters[$prop])@$($Script:PSGSuite.Domain)" + $PSBoundParameters[$prop] = "$($PSBoundParameters[$prop])@$($global:PSGSuite.Domain)" } $body.$prop = $PSBoundParameters[$prop] } @@ -222,6 +230,13 @@ function New-GSUser { } $body.Organizations = $orgList } + Relations { + $relList = New-Object 'System.Collections.Generic.List`1[Google.Apis.Admin.Directory.directory_v1.Data.UserRelation]' + foreach ($relation in $Relations) { + $relList.Add($relation) + } + $body.Relations = $relList + } Phones { $phoneList = New-Object 'System.Collections.Generic.List`1[Google.Apis.Admin.Directory.directory_v1.Data.UserPhone]' foreach ($phone in $Phones) { From 15040e691d0b1a1054635fb6bbc314bc196c5e7c Mon Sep 17 00:00:00 2001 From: Nate Ferrell Date: Tue, 11 Dec 2018 14:56:35 -0600 Subject: [PATCH 3/4] switched scope of PSGSuite var in New-GSUser to Script scope --- PSGSuite/Public/Users/New-GSUser.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PSGSuite/Public/Users/New-GSUser.ps1 b/PSGSuite/Public/Users/New-GSUser.ps1 index ea639ebd..d936f986 100644 --- a/PSGSuite/Public/Users/New-GSUser.ps1 +++ b/PSGSuite/Public/Users/New-GSUser.ps1 @@ -202,7 +202,7 @@ function New-GSUser { switch ($prop) { PrimaryEmail { if ($PSBoundParameters[$prop] -notlike "*@*.*") { - $PSBoundParameters[$prop] = "$($PSBoundParameters[$prop])@$($global:PSGSuite.Domain)" + $PSBoundParameters[$prop] = "$($PSBoundParameters[$prop])@$($script:PSGSuite.Domain)" } $body.$prop = $PSBoundParameters[$prop] } @@ -273,4 +273,4 @@ function New-GSUser { } } } -} \ No newline at end of file +} From 26ded36966d575e3509c14a1fc247b91453fe80d Mon Sep 17 00:00:00 2001 From: Nate Ferrell Date: Tue, 11 Dec 2018 15:12:46 -0600 Subject: [PATCH 4/4] corrected casing back to the original source --- PSGSuite/Public/Users/New-GSUser.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PSGSuite/Public/Users/New-GSUser.ps1 b/PSGSuite/Public/Users/New-GSUser.ps1 index d936f986..d54a8464 100644 --- a/PSGSuite/Public/Users/New-GSUser.ps1 +++ b/PSGSuite/Public/Users/New-GSUser.ps1 @@ -202,7 +202,7 @@ function New-GSUser { switch ($prop) { PrimaryEmail { if ($PSBoundParameters[$prop] -notlike "*@*.*") { - $PSBoundParameters[$prop] = "$($PSBoundParameters[$prop])@$($script:PSGSuite.Domain)" + $PSBoundParameters[$prop] = "$($PSBoundParameters[$prop])@$($Script:PSGSuite.Domain)" } $body.$prop = $PSBoundParameters[$prop] }