From 6aad71b1ad091dc09afd871d1dd80e79c8d85ab6 Mon Sep 17 00:00:00 2001 From: dwrusse Date: Mon, 8 Oct 2018 19:27:57 -0400 Subject: [PATCH 01/10] Added Set-GSGmailLabel function --- PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 | 86 ++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 diff --git a/PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 b/PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 new file mode 100644 index 00000000..a26b2df0 --- /dev/null +++ b/PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 @@ -0,0 +1,86 @@ +function Set-GSGmailLabel { + <# + .SYNOPSIS + Updates Gmail label information for the specified labelid + + .DESCRIPTION + Updates Gmail label information for the specified labelid + + .PARAMETER LabelId + The unique Id of the label to update. Required. + + .PARAMETER User + The user to update label information for + + Defaults to the AdminEmail user + + .EXAMPLE + Set-GSGmailLabel -user user@domain.com -LabelId Label_798170282134616520 - + + Gets the Gmail labels of the AdminEmail user + #> + [cmdletbinding()] + Param + ( + [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] + [Alias("Id")] + [string[]] + $LabelId, + [parameter(Mandatory = $false, Position = 0, ValueFromPipelineByPropertyName = $true)] + [Alias("PrimaryEmail", "UserKey", "Mail")] + [ValidateNotNullOrEmpty()] + [string] + $User = $Script:PSGSuite.AdminEmail, + [parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $false)] + [ValidateNotNullOrEmpty()] + [object[]] + $sourceObject = (Get-GSGmailLabel -user $user -LabelId $LabelId), + [parameter(Mandatory = $false, Position = 1, ValueFromPipelineByPropertyName = $true)] + [ValidateSet("labelHide","labelShow","labelShowIfUnread")] + [string] + $labelListVisibility = $sourceObject.labelListVisibility, + [parameter(Mandatory = $false, Position = 2, ValueFromPipelineByPropertyName = $true)] + [ValidateSet("hide","show")] + [string] + $messageListVisibility = $sourceObject.messageListVisibility, + [parameter(Mandatory = $false, Position = 1, ValueFromPipelineByPropertyName = $true)] + [string] + $name = $sourceObject.Name + ) + Begin { + if ($User -ceq 'me') { + $User = $Script:PSGSuite.AdminEmail + } + elseif ($User -notlike "*@*.*") { + $User = "$($User)@$($Script:PSGSuite.Domain)" + } + $serviceParams = @{ + Scope = 'https://mail.google.com' + ServiceType = 'Google.Apis.Gmail.v1.GmailService' + User = $User + } + $service = New-GoogleService @serviceParams + } + Process { + try { + foreach ($label in $LabelId) { + $updateObj = New-Object 'Google.Apis.Gmail.v1.Data.Label' -Property @{ + Name = $name + LabelListVisibility = $labelListVisibility + MessageListVisibility = $messageListVisibility + } + $request = $service.Users.Labels.Update($User, $label, $updateObj) + Write-Verbose "Updating Label Id '$label' for user '$User'" + $request.Execute() | Add-Member -MemberType NoteProperty -Name 'User' -Value $User -PassThru + } + } + catch { + if ($ErrorActionPreference -eq 'Stop') { + $PSCmdlet.ThrowTerminatingError($_) + } + else { + Write-Error $_ + } + } + } +} \ No newline at end of file From 34b61637130db4a727ddb7c21e26b8f01fd423b9 Mon Sep 17 00:00:00 2001 From: dwrusse Date: Mon, 8 Oct 2018 19:43:10 -0400 Subject: [PATCH 02/10] Updated CHANGELOG.md, README.md, PSGSuite.psd1 to version 2.15.2 --- CHANGELOG.md | 5 +++++ PSGSuite/PSGSuite.psd1 | 2 +- README.md | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e3e34ad..e212d9d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - [Changelog](#changelog) + - [2.15.2](#2152) - [2.15.1](#2151) - [2.15.0](#2150) - [2.14.1](#2141) @@ -52,6 +53,10 @@ +#### 2.15.2 + +* Added `Set-GSGmailLabel` to configure various properties exposed by Users.Labels.Update() API. + ## 2.15.1 * [Issue #87](https://github.com/scrthq/PSGSuite/issues/87) diff --git a/PSGSuite/PSGSuite.psd1 b/PSGSuite/PSGSuite.psd1 index 8ec120a1..02e5c039 100644 --- a/PSGSuite/PSGSuite.psd1 +++ b/PSGSuite/PSGSuite.psd1 @@ -12,7 +12,7 @@ RootModule = 'PSGSuite.psm1' # Version number of this module. - ModuleVersion = '2.15.1' + ModuleVersion = '2.15.2' # ID used to uniquely identify this module GUID = '9d751152-e83e-40bb-a6db-4c329092aaec' diff --git a/README.md b/README.md index e82e060b..31d6f91b 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,10 @@ Update-GSSheetValue Export-GSSheet ### Most recent changes +#### 2.15.2 + +* Added `Set-GSGmailLabel` to configure various properties exposed by Users.Labels.Update() API. + #### 2.15.1 * [Issue #87](https://github.com/scrthq/PSGSuite/issues/87) From e787225e9c3f5b60c00052562c2efd987e718375 Mon Sep 17 00:00:00 2001 From: dwrusse Date: Mon, 8 Oct 2018 20:56:03 -0400 Subject: [PATCH 03/10] Fixed an issue with Set-GSGmailLabel causing it to fail --- PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 b/PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 index a26b2df0..bc2fddcb 100644 --- a/PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 +++ b/PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 @@ -69,7 +69,7 @@ function Set-GSGmailLabel { LabelListVisibility = $labelListVisibility MessageListVisibility = $messageListVisibility } - $request = $service.Users.Labels.Update($User, $label, $updateObj) + $request = $service.Users.Labels.Update($updateObj, $User, $label) Write-Verbose "Updating Label Id '$label' for user '$User'" $request.Execute() | Add-Member -MemberType NoteProperty -Name 'User' -Value $User -PassThru } From 62b959a180e29426651d658992c93ed62249d418 Mon Sep 17 00:00:00 2001 From: dwrusse Date: Mon, 8 Oct 2018 21:21:24 -0400 Subject: [PATCH 04/10] Fixed Set-GSGmailLabel error causing pipeline input to fail --- PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 b/PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 index bc2fddcb..0352a294 100644 --- a/PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 +++ b/PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 @@ -37,15 +37,15 @@ function Set-GSGmailLabel { $sourceObject = (Get-GSGmailLabel -user $user -LabelId $LabelId), [parameter(Mandatory = $false, Position = 1, ValueFromPipelineByPropertyName = $true)] [ValidateSet("labelHide","labelShow","labelShowIfUnread")] - [string] + [string[]] $labelListVisibility = $sourceObject.labelListVisibility, [parameter(Mandatory = $false, Position = 2, ValueFromPipelineByPropertyName = $true)] [ValidateSet("hide","show")] - [string] + [string[]] $messageListVisibility = $sourceObject.messageListVisibility, - [parameter(Mandatory = $false, Position = 1, ValueFromPipelineByPropertyName = $true)] + [parameter(Mandatory = $false, Position = 3, ValueFromPipelineByPropertyName = $true)] [string] - $name = $sourceObject.Name + $Name = $sourceObject.Name ) Begin { if ($User -ceq 'me') { From 0579b052c99dfe1b3afab92302250298a3e88b1e Mon Sep 17 00:00:00 2001 From: dwrusse Date: Mon, 8 Oct 2018 22:26:49 -0400 Subject: [PATCH 05/10] Added Set-GSGmailMessageLabel function --- .../Public/Gmail/Set-GSGmailMessageLabel.ps1 | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 PSGSuite/Public/Gmail/Set-GSGmailMessageLabel.ps1 diff --git a/PSGSuite/Public/Gmail/Set-GSGmailMessageLabel.ps1 b/PSGSuite/Public/Gmail/Set-GSGmailMessageLabel.ps1 new file mode 100644 index 00000000..cdb5b467 --- /dev/null +++ b/PSGSuite/Public/Gmail/Set-GSGmailMessageLabel.ps1 @@ -0,0 +1,76 @@ +function Set-GSGmailMessageLabel { + <# + .SYNOPSIS + Updates Gmail label information for the specified labelid + + .DESCRIPTION + Updates Gmail label information for the specified labelid + + .PARAMETER LabelId + The unique Id of the label to update. Required. + + .PARAMETER User + The user to update label information for + + Defaults to the AdminEmail user + + .EXAMPLE + Set-GSGmailLabel -user user@domain.com -LabelId Label_798170282134616520 - + + Gets the Gmail labels of the AdminEmail user + #> + [cmdletbinding()] + Param + ( + [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] + [Alias("Id")] + [string[]] + $MessageId, + [parameter(Mandatory = $false, Position = 0, ValueFromPipelineByPropertyName = $true)] + [Alias("PrimaryEmail", "UserKey", "Mail")] + [ValidateNotNullOrEmpty()] + [string] + $User = $Script:PSGSuite.AdminEmail, + [parameter(Mandatory = $false, Position = 1, ValueFromPipelineByPropertyName = $true)] + [string[]] + $RemoveLabel, + [parameter(Mandatory = $false, Position = 2, ValueFromPipelineByPropertyName = $true)] + [string[]] + $AddLabel + ) + Begin { + if ($User -ceq 'me') { + $User = $Script:PSGSuite.AdminEmail + } + elseif ($User -notlike "*@*.*") { + $User = "$($User)@$($Script:PSGSuite.Domain)" + } + $serviceParams = @{ + Scope = 'https://mail.google.com' + ServiceType = 'Google.Apis.Gmail.v1.GmailService' + User = $User + } + $service = New-GoogleService @serviceParams + } + Process { + try { + foreach ($message in $MessageId) { + $updateObj = New-Object 'Google.Apis.Gmail.v1.Data.ModifyMessageRequest' -Property @{ + addLabelIds = [string[]]$AddLabel + removeLabelIds = [string[]]$RemoveLabel + } + $request = $service.Users.Messages.Modify($updateObj, $user, $message) + Write-Verbose "Updating label Ids on Message '$message' for user '$User'" + $request.Execute() #| Add-Member -MemberType NoteProperty -Name 'User' -Value $User -PassThru + } + } + catch { + if ($ErrorActionPreference -eq 'Stop') { + $PSCmdlet.ThrowTerminatingError($_) + } + else { + Write-Error $_ + } + } + } +} \ No newline at end of file From 67d63f77ebf1f335d8c8e659d4c1cd5d492be292 Mon Sep 17 00:00:00 2001 From: Nate Ferrell Date: Mon, 8 Oct 2018 22:03:38 -0500 Subject: [PATCH 06/10] !build Update-GSGmailLabel function update --- PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 | 86 -------------- PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 | 107 ++++++++++++++++++ 2 files changed, 107 insertions(+), 86 deletions(-) delete mode 100644 PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 create mode 100644 PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 diff --git a/PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 b/PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 deleted file mode 100644 index 0352a294..00000000 --- a/PSGSuite/Public/Gmail/Set-GSGmailLabel.ps1 +++ /dev/null @@ -1,86 +0,0 @@ -function Set-GSGmailLabel { - <# - .SYNOPSIS - Updates Gmail label information for the specified labelid - - .DESCRIPTION - Updates Gmail label information for the specified labelid - - .PARAMETER LabelId - The unique Id of the label to update. Required. - - .PARAMETER User - The user to update label information for - - Defaults to the AdminEmail user - - .EXAMPLE - Set-GSGmailLabel -user user@domain.com -LabelId Label_798170282134616520 - - - Gets the Gmail labels of the AdminEmail user - #> - [cmdletbinding()] - Param - ( - [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] - [Alias("Id")] - [string[]] - $LabelId, - [parameter(Mandatory = $false, Position = 0, ValueFromPipelineByPropertyName = $true)] - [Alias("PrimaryEmail", "UserKey", "Mail")] - [ValidateNotNullOrEmpty()] - [string] - $User = $Script:PSGSuite.AdminEmail, - [parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $false)] - [ValidateNotNullOrEmpty()] - [object[]] - $sourceObject = (Get-GSGmailLabel -user $user -LabelId $LabelId), - [parameter(Mandatory = $false, Position = 1, ValueFromPipelineByPropertyName = $true)] - [ValidateSet("labelHide","labelShow","labelShowIfUnread")] - [string[]] - $labelListVisibility = $sourceObject.labelListVisibility, - [parameter(Mandatory = $false, Position = 2, ValueFromPipelineByPropertyName = $true)] - [ValidateSet("hide","show")] - [string[]] - $messageListVisibility = $sourceObject.messageListVisibility, - [parameter(Mandatory = $false, Position = 3, ValueFromPipelineByPropertyName = $true)] - [string] - $Name = $sourceObject.Name - ) - Begin { - if ($User -ceq 'me') { - $User = $Script:PSGSuite.AdminEmail - } - elseif ($User -notlike "*@*.*") { - $User = "$($User)@$($Script:PSGSuite.Domain)" - } - $serviceParams = @{ - Scope = 'https://mail.google.com' - ServiceType = 'Google.Apis.Gmail.v1.GmailService' - User = $User - } - $service = New-GoogleService @serviceParams - } - Process { - try { - foreach ($label in $LabelId) { - $updateObj = New-Object 'Google.Apis.Gmail.v1.Data.Label' -Property @{ - Name = $name - LabelListVisibility = $labelListVisibility - MessageListVisibility = $messageListVisibility - } - $request = $service.Users.Labels.Update($updateObj, $User, $label) - Write-Verbose "Updating Label Id '$label' for user '$User'" - $request.Execute() | Add-Member -MemberType NoteProperty -Name 'User' -Value $User -PassThru - } - } - catch { - if ($ErrorActionPreference -eq 'Stop') { - $PSCmdlet.ThrowTerminatingError($_) - } - else { - Write-Error $_ - } - } - } -} \ No newline at end of file diff --git a/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 b/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 new file mode 100644 index 00000000..68f6abee --- /dev/null +++ b/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 @@ -0,0 +1,107 @@ +function Update-GSGmailLabel { + <# + .SYNOPSIS + Updates Gmail label information for the specified labelid + + .DESCRIPTION + Updates Gmail label information for the specified labelid + + .PARAMETER LabelId + The unique Id of the label to update. Required. + + .PARAMETER User + The user to update label information for + + Defaults to the AdminEmail user + + .EXAMPLE + Set-GSGmailLabel -User user@domain.com -LabelId Label_798170282134616520 - + + Gets the Gmail labels of the AdminEmail user + #> + [cmdletbinding(DefaultParameterSetName = "Fields")] + Param + ( + [parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true, ParameterSetName = "Fields")] + [Alias("Id")] + [string[]] + $LabelId, + [parameter(Mandatory = $false, Position = 1)] + [ValidateSet("labelHide","labelShow","labelShowIfUnread")] + [string] + $LabelListVisibility, + [parameter(Mandatory = $false, Position = 2)] + [ValidateSet("hide","show")] + [string] + $MessageListVisibility, + [parameter(Mandatory = $false, Position = 3)] + [string] + $Name, + [parameter(Mandatory = $false)] + [ValidateSet('#000000','#434343','#666666','#999999','#cccccc','#efefef','#f3f3f3','#ffffff','#fb4c2f','#ffad47','#fad165','#16a766','#43d692','#4a86e8','#a479e2','#f691b3','#f6c5be','#ffe6c7','#fef1d1','#b9e4d0','#c6f3de','#c9daf8','#e4d7f5','#fcdee8','#efa093','#ffd6a2','#fce8b3','#89d3b2','#a0eac9','#a4c2f4','#d0bcf1','#fbc8d9','#e66550','#ffbc6b','#fcda83','#44b984','#68dfa9','#6d9eeb','#b694e8','#f7a7c0','#cc3a21','#eaa041','#f2c960','#149e60','#3dc789','#3c78d8','#8e63ce','#e07798','#ac2b16','#cf8933','#d5ae49','#0b804b','#2a9c68','#285bac','#653e9b','#b65775','#822111','#a46a21','#aa8831','#076239','#1a764d','#1c4587','#41236d','#83334c')] + [string] + $BackgroundColor, + [parameter(Mandatory = $false)] + [ValidateSet('#000000','#434343','#666666','#999999','#cccccc','#efefef','#f3f3f3','#ffffff','#fb4c2f','#ffad47','#fad165','#16a766','#43d692','#4a86e8','#a479e2','#f691b3','#f6c5be','#ffe6c7','#fef1d1','#b9e4d0','#c6f3de','#c9daf8','#e4d7f5','#fcdee8','#efa093','#ffd6a2','#fce8b3','#89d3b2','#a0eac9','#a4c2f4','#d0bcf1','#fbc8d9','#e66550','#ffbc6b','#fcda83','#44b984','#68dfa9','#6d9eeb','#b694e8','#f7a7c0','#cc3a21','#eaa041','#f2c960','#149e60','#3dc789','#3c78d8','#8e63ce','#e07798','#ac2b16','#cf8933','#d5ae49','#0b804b','#2a9c68','#285bac','#653e9b','#b65775','#822111','#a46a21','#aa8831','#076239','#1a764d','#1c4587','#41236d','#83334c')] + [string] + $TextColor, + [parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, ParameterSetName = "Fields")] + [Alias("PrimaryEmail", "UserKey", "Mail")] + [ValidateNotNullOrEmpty()] + [string] + $User = $Script:PSGSuite.AdminEmail, + [parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "InputObject")] + [Google.Apis.Gmail.v1.Data.Label] + $InputObject + ) + Process { + switch ($PSCmdlet.ParameterSetName) { + InputObject { + $U = $InputObject.User + $labels = @($InputObject.Id) + } + Fields { + $U = $User + $labels = $LabelId + } + } + if ($U -ceq 'me') { + $U = $Script:PSGSuite.AdminEmail + } + elseif ($U -notlike "*@*.*") { + $U = "$($U)@$($Script:PSGSuite.Domain)" + } + $serviceParams = @{ + Scope = 'https://mail.google.com' + ServiceType = 'Google.Apis.Gmail.v1.GmailService' + User = $U + } + $service = New-GoogleService @serviceParams + try { + $body = New-Object 'Google.Apis.Gmail.v1.Data.Label' + foreach ($prop in $PSBoundParameters.Keys | Where-Object {$body.PSObject.Properties.Name -contains $_}) { + $body.$prop = $PSBoundParameters[$prop] + } + if ($PSBoundParameters.Keys -contains 'BackgroundColor' -or $PSBoundParameters.Keys -contains 'TextColor') { + $color = New-Object 'Google.Apis.Gmail.v1.Data.LabelColor' + foreach ($prop in $PSBoundParameters.Keys | Where-Object {$color.PSObject.Properties.Name -contains $_}) { + $color.$prop = $PSBoundParameters[$prop] + } + $body.Color = $color + } + foreach ($label in $labels) { + Write-Verbose "Updating Label Id '$label' for user '$U'" + $request = $service.Users.Labels.Patch($body, $U, $label) + $request.Execute() | Add-Member -MemberType NoteProperty -Name 'User' -Value $U -PassThru + } + } + catch { + if ($ErrorActionPreference -eq 'Stop') { + $PSCmdlet.ThrowTerminatingError($_) + } + else { + Write-Error $_ + } + } + } +} From aa8dc2f2658d038214ef5b831d522c8b38e6dcd5 Mon Sep 17 00:00:00 2001 From: Nate Ferrell Date: Mon, 8 Oct 2018 22:45:38 -0500 Subject: [PATCH 07/10] !build switched label colors to friendly names instead of hex values --- PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 | 78 +++++++++++++++++-- 1 file changed, 73 insertions(+), 5 deletions(-) diff --git a/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 b/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 index 68f6abee..da93c814 100644 --- a/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 +++ b/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 @@ -15,9 +15,9 @@ function Update-GSGmailLabel { Defaults to the AdminEmail user .EXAMPLE - Set-GSGmailLabel -User user@domain.com -LabelId Label_798170282134616520 - + Update-GSGmailLabel -User user@domain.com -LabelId Label_79 -BackgroundColor Black -TextColor Bermuda - Gets the Gmail labels of the AdminEmail user + Updates the specified Gmail label with new background and text colors #> [cmdletbinding(DefaultParameterSetName = "Fields")] Param @@ -38,11 +38,11 @@ function Update-GSGmailLabel { [string] $Name, [parameter(Mandatory = $false)] - [ValidateSet('#000000','#434343','#666666','#999999','#cccccc','#efefef','#f3f3f3','#ffffff','#fb4c2f','#ffad47','#fad165','#16a766','#43d692','#4a86e8','#a479e2','#f691b3','#f6c5be','#ffe6c7','#fef1d1','#b9e4d0','#c6f3de','#c9daf8','#e4d7f5','#fcdee8','#efa093','#ffd6a2','#fce8b3','#89d3b2','#a0eac9','#a4c2f4','#d0bcf1','#fbc8d9','#e66550','#ffbc6b','#fcda83','#44b984','#68dfa9','#6d9eeb','#b694e8','#f7a7c0','#cc3a21','#eaa041','#f2c960','#149e60','#3dc789','#3c78d8','#8e63ce','#e07798','#ac2b16','#cf8933','#d5ae49','#0b804b','#2a9c68','#285bac','#653e9b','#b65775','#822111','#a46a21','#aa8831','#076239','#1a764d','#1c4587','#41236d','#83334c')] + [ValidateSet('Amethyst','BananaMania','Bermuda','BilobaFlower','Black','BlueRomance','BrandyPunch','BurntSienna','Cadillac','Camelot','CeruleanBlue','ChathamsBlue','Concrete','CornflowerBlue','CreamCan','Cupid','DeepBlush','Desert','DoveGray','DustyGray','Eucalyptus','Flesh','FringyFlower','Gallery','Goldenrod','Illusion','Jewel','Koromiko','LightCornflowerBlue','LightMoonRaker','LightMountainMeadow','LightShamrock','LuxorGold','MandysPink','MediumPurple','Meteorite','MoonRaker','MountainMeadow','Oasis','OceanGreen','OldGold','Perano','PersianPink','PigPink','Pueblo','RedOrange','RoyalBlue','RoyalPurple','Salem','Salomie','SeaPink','Shamrock','Silver','Tabasco','Tequila','Thunderbird','TropicalBlue','TulipTree','Tundora','VistaBlue','Watercourse','WaterLeaf','White','YellowOrange')] [string] $BackgroundColor, [parameter(Mandatory = $false)] - [ValidateSet('#000000','#434343','#666666','#999999','#cccccc','#efefef','#f3f3f3','#ffffff','#fb4c2f','#ffad47','#fad165','#16a766','#43d692','#4a86e8','#a479e2','#f691b3','#f6c5be','#ffe6c7','#fef1d1','#b9e4d0','#c6f3de','#c9daf8','#e4d7f5','#fcdee8','#efa093','#ffd6a2','#fce8b3','#89d3b2','#a0eac9','#a4c2f4','#d0bcf1','#fbc8d9','#e66550','#ffbc6b','#fcda83','#44b984','#68dfa9','#6d9eeb','#b694e8','#f7a7c0','#cc3a21','#eaa041','#f2c960','#149e60','#3dc789','#3c78d8','#8e63ce','#e07798','#ac2b16','#cf8933','#d5ae49','#0b804b','#2a9c68','#285bac','#653e9b','#b65775','#822111','#a46a21','#aa8831','#076239','#1a764d','#1c4587','#41236d','#83334c')] + [ValidateSet('Amethyst','BananaMania','Bermuda','BilobaFlower','Black','BlueRomance','BrandyPunch','BurntSienna','Cadillac','Camelot','CeruleanBlue','ChathamsBlue','Concrete','CornflowerBlue','CreamCan','Cupid','DeepBlush','Desert','DoveGray','DustyGray','Eucalyptus','Flesh','FringyFlower','Gallery','Goldenrod','Illusion','Jewel','Koromiko','LightCornflowerBlue','LightMoonRaker','LightMountainMeadow','LightShamrock','LuxorGold','MandysPink','MediumPurple','Meteorite','MoonRaker','MountainMeadow','Oasis','OceanGreen','OldGold','Perano','PersianPink','PigPink','Pueblo','RedOrange','RoyalBlue','RoyalPurple','Salem','Salomie','SeaPink','Shamrock','Silver','Tabasco','Tequila','Thunderbird','TropicalBlue','TulipTree','Tundora','VistaBlue','Watercourse','WaterLeaf','White','YellowOrange')] [string] $TextColor, [parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, ParameterSetName = "Fields")] @@ -54,6 +54,74 @@ function Update-GSGmailLabel { [Google.Apis.Gmail.v1.Data.Label] $InputObject ) + Begin { + $colorDict = @{ + Amethyst = '#8e63ce' + BananaMania = '#fce8b3' + Bermuda = '#68dfa9' + BilobaFlower = '#b694e8' + Black = '#000000' + BlueRomance = '#c6f3de' + BrandyPunch = '#cf8933' + BurntSienna = '#e66550' + Cadillac = '#b65775' + Camelot = '#83334c' + CeruleanBlue = '#285bac' + ChathamsBlue = '#1c4587' + Concrete = '#f3f3f3' + CornflowerBlue = '#4a86e8' + LightCornflowerBlue = '#6d9eeb' + CreamCan = '#f2c960' + Cupid = '#fbc8d9' + DeepBlush = '#e07798' + Desert = '#a46a21' + DoveGray = '#666666' + DustyGray = '#999999' + Eucalyptus = '#2a9c68' + Flesh = '#ffd6a2' + FringyFlower = '#b9e4d0' + Gallery = '#efefef' + Goldenrod = '#fad165' + Illusion = '#f7a7c0' + Jewel = '#1a764d' + Koromiko = '#ffbc6b' + LightMountainMeadow = '#16a766' + LightShamrock = '#43d692' + LuxorGold = '#aa8831' + MandysPink = '#f6c5be' + MediumPurple = '#a479e2' + Meteorite = '#41236d' + MoonRaker = '#d0bcf1' + LightMoonRaker = '#e4d7f5' + MountainMeadow = '#149e60' + Oasis = '#fef1d1' + OceanGreen = '#44b984' + OldGold = '#d5ae49' + Perano = '#a4c2f4' + PersianPink = '#f691b3' + PigPink = '#fcdee8' + Pueblo = '#822111' + RedOrange = '#fb4c2f' + RoyalBlue = '#3c78d8' + RoyalPurple = '#653e9b' + Salem = '#0b804b' + Salomie = '#fcda83' + SeaPink = '#efa093' + Shamrock = '#3dc789' + Silver = '#cccccc' + Tabasco = '#ac2b16' + Tequila = '#ffe6c7' + Thunderbird = '#cc3a21' + TropicalBlue = '#c9daf8' + TulipTree = '#eaa041' + Tundora = '#434343' + VistaBlue = '#89d3b2' + Watercourse = '#076239' + WaterLeaf = '#a0eac9' + White = '#ffffff' + YellowOrange = '#ffad47' + } + } Process { switch ($PSCmdlet.ParameterSetName) { InputObject { @@ -85,7 +153,7 @@ function Update-GSGmailLabel { if ($PSBoundParameters.Keys -contains 'BackgroundColor' -or $PSBoundParameters.Keys -contains 'TextColor') { $color = New-Object 'Google.Apis.Gmail.v1.Data.LabelColor' foreach ($prop in $PSBoundParameters.Keys | Where-Object {$color.PSObject.Properties.Name -contains $_}) { - $color.$prop = $PSBoundParameters[$prop] + $color.$prop = $colorDict[$PSBoundParameters[$prop]] } $body.Color = $color } From c0ebf7c0436248fc8ada39dd978477cc2a495a47 Mon Sep 17 00:00:00 2001 From: Nate Ferrell Date: Mon, 8 Oct 2018 22:55:54 -0500 Subject: [PATCH 08/10] !build updated comment based help for Update-GSGmailLabel --- PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 b/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 index da93c814..3ed06aa9 100644 --- a/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 +++ b/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 @@ -7,17 +7,49 @@ function Update-GSGmailLabel { Updates Gmail label information for the specified labelid .PARAMETER LabelId - The unique Id of the label to update. Required. + The unique Id of the label to update + + .PARAMETER LabelListVisibility + The visibility of the label in the label list in the Gmail web interface. + + Acceptable values are: + * "labelHide": Do not show the label in the label list. + * "labelShow": Show the label in the label list. (Default) + * "labelShowIfUnread": Show the label if there are any unread messages with that label. + + .PARAMETER MessageListVisibility + The visibility of messages with this label in the message list in the Gmail web interface. + + Acceptable values are: + * "hide": Do not show the label in the message list. + * "show": Show the label in the message list. (Default) + + .PARAMETER Name + The display name of the label + + .PARAMETER BackgroundColor + The background color of the label + + .PARAMETER TextColor + The text color of the label .PARAMETER User The user to update label information for Defaults to the AdminEmail user + .PARAMETER InputObject + Pipeline input Label objects, used when applying updates to output of `Get-GSGmailLabel` + .EXAMPLE Update-GSGmailLabel -User user@domain.com -LabelId Label_79 -BackgroundColor Black -TextColor Bermuda Updates the specified Gmail label with new background and text colors + + .EXAMPLE + Get-GSGmailLabel | Where-Object {$_.LabelListVisibility -eq 'labelShowIfUnread'} | Update-GSGmailLabel -LabelListVisibility labelShow -BackgroundColor Bermuda -TextColor Tundora + + Updates all labels with LabelListVisibility of 'labelShowIfUnread' with new background and text colors and sets all of them to always show #> [cmdletbinding(DefaultParameterSetName = "Fields")] Param From bbdb5f1ca8dde5aeda6a626ec26160deb804e714 Mon Sep 17 00:00:00 2001 From: Nate Ferrell Date: Tue, 9 Oct 2018 00:18:51 -0500 Subject: [PATCH 09/10] !build updates for v2.15.2 release --- CHANGELOG.md | 124 ++++++++-------- .../Public/Gmail/Remove-GSGmailMessage.ps1 | 55 +++++--- .../Public/Gmail/Set-GSGmailMessageLabel.ps1 | 76 ---------- PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 | 76 ++++------ .../Gmail/Update-GSGmailMessageLabel.ps1 | 133 ++++++++++++++++++ README.md | 15 +- 6 files changed, 269 insertions(+), 210 deletions(-) delete mode 100644 PSGSuite/Public/Gmail/Set-GSGmailMessageLabel.ps1 create mode 100644 PSGSuite/Public/Gmail/Update-GSGmailMessageLabel.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index e212d9d9..b562e031 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,61 +1,61 @@ -# Changelog - - - -- [Changelog](#changelog) - - [2.15.2](#2152) - - [2.15.1](#2151) - - [2.15.0](#2150) - - [2.14.1](#2141) - - [2.14.0](#2140) - - [2.13.2](#2132) - - [2.13.1](#2131) - - [2.13.0](#2130) - - [2.12.1](#2121) - - [2.12.0](#2120) - - [2.11.0](#2110) - - [2.10.2](#2102) - - [2.10.1](#2101) - - [2.10.0](#2100) - - [2.9.0](#290) - - [2.8.1](#281) - - [2.8.0](#280) - - [2.7.2](#272) - - [2.7.1](#271) - - [2.7.0](#270) - - [2.6.3](#263) - - [2.6.2](#262) - - [2.6.1](#261) - - [2.6.0](#260) - - [2.5.4](#254) - - [2.5.3](#253) - - [2.5.2](#252) - - [2.5.1](#251) - - [2.5.0](#250) - - [2.4.0](#240) - - [2.3.0](#230) - - [2.2.1](#221) - - [2.2.0](#220) - - [2.1.5](#215) - - [2.1.3 / 2.1.4](#213--214) - - [2.1.2](#212) - - [2.1.1](#211) - - [2.1.0](#210) - - [2.0.3](#203) - - [2.0.2](#202) - - [2.0.1](#201) - - [2.0.0](#200) - - [New Functionality](#new-functionality) - - [Breaking Changes in 2.0.0](#breaking-changes-in-200) - - [Gmail Delegation Management Removed](#gmail-delegation-management-removed) - - [Functions Removed](#functions-removed) - - [Functions Aliased](#functions-aliased) - - - -#### 2.15.2 - -* Added `Set-GSGmailLabel` to configure various properties exposed by Users.Labels.Update() API. +# Changelog + +* [2.15.2](#2152) +* [2.15.1](#2151) +* [2.15.0](#2150) +* [2.14.1](#2141) +* [2.14.0](#2140) +* [2.13.2](#2132) +* [2.13.1](#2131) +* [2.13.0](#2130) +* [2.12.1](#2121) +* [2.12.0](#2120) +* [2.11.0](#2110) +* [2.10.2](#2102) +* [2.10.1](#2101) +* [2.10.0](#2100) +* [2.9.0](#290) +* [2.8.1](#281) +* [2.8.0](#280) +* [2.7.2](#272) +* [2.7.1](#271) +* [2.7.0](#270) +* [2.6.3](#263) +* [2.6.2](#262) +* [2.6.1](#261) +* [2.6.0](#260) +* [2.5.4](#254) +* [2.5.3](#253) +* [2.5.2](#252) +* [2.5.1](#251) +* [2.5.0](#250) +* [2.4.0](#240) +* [2.3.0](#230) +* [2.2.1](#221) +* [2.2.0](#220) +* [2.1.5](#215) +* [2.1.3 / 2.1.4](#213--214) +* [2.1.2](#212) +* [2.1.1](#211) +* [2.1.0](#210) +* [2.0.3](#203) +* [2.0.2](#202) +* [2.0.1](#201) +* [2.0.0](#200) + * [New Functionality](#new-functionality) + * [Breaking Changes in 2.0.0](#breaking-changes-in-200) + * [Gmail Delegation Management Removed](#gmail-delegation-management-removed) + * [Functions Removed](#functions-removed) + * [Functions Aliased](#functions-aliased) + +## 2.15.2 + +* [Pull Request #94](https://github.com/scrthq/PSGSuite/pull/94) **Thanks, [@dwrusse](https://github.com/dwrusse)!** + * Added `Update-GSGmailLabel` to enable updating of Gmail label properties + * Added `Update-GSGmailMessageLabel` enable updating of labels attached to Gmail messages +* [Issue #93](https://github.com/scrthq/PSGSuite/issues/93) + * Updated `Remove-GSGmailMessage` to include a `-Filter` parameter to allow removal of messages matching a filter in a single command + * Improved pipeline support for `Remove-GSGmailMessage` ## 2.15.1 @@ -67,10 +67,10 @@ ## 2.15.0 -- Updated Gmail Delegation functions to use the .NET SDK after Google announced delegation support for the Gmail API -- Cleaned up `Get-GSGmailDelegates` by removing the trailing `s` (now `Get-GSGmailDelegate`). Added the original function as an alias to the new function for backwards compatibility with scripts. -- Removed the `Raw` parameter from `Get-GSGmailDelegate` since it's no longer applicable. -- Enabled `Get-GSGmailDelegate` to perform both Get and List requests (previously only performed List requests) +* Updated Gmail Delegation functions to use the .NET SDK after Google announced delegation support for the Gmail API +* Cleaned up `Get-GSGmailDelegates` by removing the trailing `s` (now `Get-GSGmailDelegate`). Added the original function as an alias to the new function for backwards compatibility with scripts. +* Removed the `Raw` parameter from `Get-GSGmailDelegate` since it's no longer applicable. +* Enabled `Get-GSGmailDelegate` to perform both Get and List requests (previously only performed List requests) ## 2.14.1 diff --git a/PSGSuite/Public/Gmail/Remove-GSGmailMessage.ps1 b/PSGSuite/Public/Gmail/Remove-GSGmailMessage.ps1 index 4c46069c..eb538271 100644 --- a/PSGSuite/Public/Gmail/Remove-GSGmailMessage.ps1 +++ b/PSGSuite/Public/Gmail/Remove-GSGmailMessage.ps1 @@ -2,18 +2,16 @@ function Remove-GSGmailMessage { <# .SYNOPSIS Removes a Gmail message from the user - + .DESCRIPTION Removes a Gmail message from the user - - .PARAMETER User - The primary email of the user to remove the message from - Defaults to the AdminEmail user - .PARAMETER Id The Id of the message to remove - + + .PARAMETER Filter + The Gmail query to pull the list of messages to remove instead of passing the MessageId directly + .PARAMETER Method The method used to delete the message @@ -22,29 +20,38 @@ function Remove-GSGmailMessage { * "Delete": permanently deletes the message (NON-RECOVERABLE!) Default value is 'Trash' - + + .PARAMETER User + The primary email of the user to remove the message from + + Defaults to the AdminEmail user + .EXAMPLE Remove-GSGmailMessage -User joe -Id 161622d7b76b7e1e,1616227c34d435f2 Moves the 2 message Id's from Joe's inbox into their TRASH after confirmation #> - [cmdletbinding(SupportsShouldProcess = $true,ConfirmImpact = "High")] + [cmdletbinding(SupportsShouldProcess = $true,ConfirmImpact = "High",DefaultParameterSetName = "MessageId")] Param ( - [parameter(Mandatory = $false,ValueFromPipelineByPropertyName = $true)] - [Alias("PrimaryEmail","UserKey","Mail")] - [string] - $User = $Script:PSGSuite.AdminEmail, - [parameter(Mandatory = $true,ValueFromPipelineByPropertyName = $true)] - [Alias('MessageID')] + [parameter(Mandatory = $true,ValueFromPipelineByPropertyName = $true,ParameterSetName = "MessageId")] + [Alias('MessageId')] [String[]] $Id, + [parameter(Mandatory = $true, ParameterSetName = "Filter")] + [Alias('Query')] + [string] + $Filter, [parameter(Mandatory = $false)] [ValidateSet('Trash','Delete')] [String] - $Method = 'Trash' + $Method = 'Trash', + [parameter(Mandatory = $false,ValueFromPipelineByPropertyName = $true)] + [Alias("PrimaryEmail","UserKey","Mail")] + [string] + $User = $Script:PSGSuite.AdminEmail ) - Begin { + Process { if ($MyInvocation.InvocationName -eq 'Move-GSGmailMessageToTrash') { $Method = 'Trash' } @@ -60,10 +67,16 @@ function Remove-GSGmailMessage { User = $User } $service = New-GoogleService @serviceParams - } - Process { + $msgId = switch ($PSCmdlet.ParameterSetName) { + MessageId { + $Id + } + Filter { + (Get-GSGmailMessageList -Filter $Filter -User $User).Id + } + } try { - foreach ($mId in $Id) { + foreach ($mId in $msgId) { $request = switch ($Method) { Trash { $service.Users.Messages.Trash($User,$mId) @@ -93,4 +106,4 @@ function Remove-GSGmailMessage { } } } -} \ No newline at end of file +} diff --git a/PSGSuite/Public/Gmail/Set-GSGmailMessageLabel.ps1 b/PSGSuite/Public/Gmail/Set-GSGmailMessageLabel.ps1 deleted file mode 100644 index cdb5b467..00000000 --- a/PSGSuite/Public/Gmail/Set-GSGmailMessageLabel.ps1 +++ /dev/null @@ -1,76 +0,0 @@ -function Set-GSGmailMessageLabel { - <# - .SYNOPSIS - Updates Gmail label information for the specified labelid - - .DESCRIPTION - Updates Gmail label information for the specified labelid - - .PARAMETER LabelId - The unique Id of the label to update. Required. - - .PARAMETER User - The user to update label information for - - Defaults to the AdminEmail user - - .EXAMPLE - Set-GSGmailLabel -user user@domain.com -LabelId Label_798170282134616520 - - - Gets the Gmail labels of the AdminEmail user - #> - [cmdletbinding()] - Param - ( - [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] - [Alias("Id")] - [string[]] - $MessageId, - [parameter(Mandatory = $false, Position = 0, ValueFromPipelineByPropertyName = $true)] - [Alias("PrimaryEmail", "UserKey", "Mail")] - [ValidateNotNullOrEmpty()] - [string] - $User = $Script:PSGSuite.AdminEmail, - [parameter(Mandatory = $false, Position = 1, ValueFromPipelineByPropertyName = $true)] - [string[]] - $RemoveLabel, - [parameter(Mandatory = $false, Position = 2, ValueFromPipelineByPropertyName = $true)] - [string[]] - $AddLabel - ) - Begin { - if ($User -ceq 'me') { - $User = $Script:PSGSuite.AdminEmail - } - elseif ($User -notlike "*@*.*") { - $User = "$($User)@$($Script:PSGSuite.Domain)" - } - $serviceParams = @{ - Scope = 'https://mail.google.com' - ServiceType = 'Google.Apis.Gmail.v1.GmailService' - User = $User - } - $service = New-GoogleService @serviceParams - } - Process { - try { - foreach ($message in $MessageId) { - $updateObj = New-Object 'Google.Apis.Gmail.v1.Data.ModifyMessageRequest' -Property @{ - addLabelIds = [string[]]$AddLabel - removeLabelIds = [string[]]$RemoveLabel - } - $request = $service.Users.Messages.Modify($updateObj, $user, $message) - Write-Verbose "Updating label Ids on Message '$message' for user '$User'" - $request.Execute() #| Add-Member -MemberType NoteProperty -Name 'User' -Value $User -PassThru - } - } - catch { - if ($ErrorActionPreference -eq 'Stop') { - $PSCmdlet.ThrowTerminatingError($_) - } - else { - Write-Error $_ - } - } - } -} \ No newline at end of file diff --git a/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 b/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 index 3ed06aa9..81283873 100644 --- a/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 +++ b/PSGSuite/Public/Gmail/Update-GSGmailLabel.ps1 @@ -38,9 +38,6 @@ function Update-GSGmailLabel { Defaults to the AdminEmail user - .PARAMETER InputObject - Pipeline input Label objects, used when applying updates to output of `Get-GSGmailLabel` - .EXAMPLE Update-GSGmailLabel -User user@domain.com -LabelId Label_79 -BackgroundColor Black -TextColor Bermuda @@ -51,10 +48,10 @@ function Update-GSGmailLabel { Updates all labels with LabelListVisibility of 'labelShowIfUnread' with new background and text colors and sets all of them to always show #> - [cmdletbinding(DefaultParameterSetName = "Fields")] + [cmdletbinding()] Param ( - [parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true, ParameterSetName = "Fields")] + [parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)] [Alias("Id")] [string[]] $LabelId, @@ -77,14 +74,11 @@ function Update-GSGmailLabel { [ValidateSet('Amethyst','BananaMania','Bermuda','BilobaFlower','Black','BlueRomance','BrandyPunch','BurntSienna','Cadillac','Camelot','CeruleanBlue','ChathamsBlue','Concrete','CornflowerBlue','CreamCan','Cupid','DeepBlush','Desert','DoveGray','DustyGray','Eucalyptus','Flesh','FringyFlower','Gallery','Goldenrod','Illusion','Jewel','Koromiko','LightCornflowerBlue','LightMoonRaker','LightMountainMeadow','LightShamrock','LuxorGold','MandysPink','MediumPurple','Meteorite','MoonRaker','MountainMeadow','Oasis','OceanGreen','OldGold','Perano','PersianPink','PigPink','Pueblo','RedOrange','RoyalBlue','RoyalPurple','Salem','Salomie','SeaPink','Shamrock','Silver','Tabasco','Tequila','Thunderbird','TropicalBlue','TulipTree','Tundora','VistaBlue','Watercourse','WaterLeaf','White','YellowOrange')] [string] $TextColor, - [parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, ParameterSetName = "Fields")] + [parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)] [Alias("PrimaryEmail", "UserKey", "Mail")] [ValidateNotNullOrEmpty()] [string] - $User = $Script:PSGSuite.AdminEmail, - [parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "InputObject")] - [Google.Apis.Gmail.v1.Data.Label] - $InputObject + $User = $Script:PSGSuite.AdminEmail ) Begin { $colorDict = @{ @@ -155,52 +149,42 @@ function Update-GSGmailLabel { } } Process { - switch ($PSCmdlet.ParameterSetName) { - InputObject { - $U = $InputObject.User - $labels = @($InputObject.Id) - } - Fields { - $U = $User - $labels = $LabelId - } + if ($User -ceq 'me') { + $User = $Script:PSGSuite.AdminEmail } - if ($U -ceq 'me') { - $U = $Script:PSGSuite.AdminEmail - } - elseif ($U -notlike "*@*.*") { - $U = "$($U)@$($Script:PSGSuite.Domain)" + elseif ($User -notlike "*@*.*") { + $User = "$($User)@$($Script:PSGSuite.Domain)" } $serviceParams = @{ Scope = 'https://mail.google.com' ServiceType = 'Google.Apis.Gmail.v1.GmailService' - User = $U + User = $User } $service = New-GoogleService @serviceParams - try { - $body = New-Object 'Google.Apis.Gmail.v1.Data.Label' - foreach ($prop in $PSBoundParameters.Keys | Where-Object {$body.PSObject.Properties.Name -contains $_}) { - $body.$prop = $PSBoundParameters[$prop] - } - if ($PSBoundParameters.Keys -contains 'BackgroundColor' -or $PSBoundParameters.Keys -contains 'TextColor') { - $color = New-Object 'Google.Apis.Gmail.v1.Data.LabelColor' - foreach ($prop in $PSBoundParameters.Keys | Where-Object {$color.PSObject.Properties.Name -contains $_}) { - $color.$prop = $colorDict[$PSBoundParameters[$prop]] - } - $body.Color = $color - } - foreach ($label in $labels) { - Write-Verbose "Updating Label Id '$label' for user '$U'" - $request = $service.Users.Labels.Patch($body, $U, $label) - $request.Execute() | Add-Member -MemberType NoteProperty -Name 'User' -Value $U -PassThru + $body = New-Object 'Google.Apis.Gmail.v1.Data.Label' + foreach ($prop in $PSBoundParameters.Keys | Where-Object {$body.PSObject.Properties.Name -contains $_}) { + $body.$prop = $PSBoundParameters[$prop] + } + if ($PSBoundParameters.Keys -contains 'BackgroundColor' -or $PSBoundParameters.Keys -contains 'TextColor') { + $color = New-Object 'Google.Apis.Gmail.v1.Data.LabelColor' + foreach ($prop in $PSBoundParameters.Keys | Where-Object {$color.PSObject.Properties.Name -contains $_}) { + $color.$prop = $colorDict[$PSBoundParameters[$prop]] } + $body.Color = $color } - catch { - if ($ErrorActionPreference -eq 'Stop') { - $PSCmdlet.ThrowTerminatingError($_) + foreach ($label in $LabelId) { + try { + Write-Verbose "Updating Label Id '$label' for user '$User'" + $request = $service.Users.Labels.Patch($body, $User, $label) + $request.Execute() | Add-Member -MemberType NoteProperty -Name 'User' -Value $User -PassThru } - else { - Write-Error $_ + catch { + if ($ErrorActionPreference -eq 'Stop') { + $PSCmdlet.ThrowTerminatingError($_) + } + else { + Write-Error $_ + } } } } diff --git a/PSGSuite/Public/Gmail/Update-GSGmailMessageLabel.ps1 b/PSGSuite/Public/Gmail/Update-GSGmailMessageLabel.ps1 new file mode 100644 index 00000000..ae1500a3 --- /dev/null +++ b/PSGSuite/Public/Gmail/Update-GSGmailMessageLabel.ps1 @@ -0,0 +1,133 @@ +function Update-GSGmailMessageLabel { + <# + .SYNOPSIS + Updates Gmail label information for the specified message + + .DESCRIPTION + Updates Gmail label information for the specified message + + .PARAMETER MessageId + The unique Id of the message to update. + + .PARAMETER Filter + The Gmail query to pull the list of messages to update instead of passing the MessageId directly. + + .PARAMETER AddLabel + The label(s) to add to the message. This supports either the unique LabelId or the Display Name for the label + + .PARAMETER RemoveLabel + The label(s) to remove from the message. This supports either the unique LabelId or the Display Name for the label + + .PARAMETER User + The user to update message labels for + + Defaults to the AdminEmail user + + .EXAMPLE + Set-GSGmailLabel -user user@domain.com -LabelId Label_798170282134616520 - + + Gets the Gmail labels of the AdminEmail user + #> + [cmdletbinding(DefaultParameterSetName = "MessageId")] + Param + ( + [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = "MessageId")] + [Alias("Id")] + [string[]] + $MessageId, + [parameter(Mandatory = $true, ParameterSetName = "Filter")] + [Alias('Query')] + [string] + $Filter, + [parameter(Mandatory = $false)] + [string[]] + $AddLabel, + [parameter(Mandatory = $false)] + [string[]] + $RemoveLabel, + [parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)] + [Alias("PrimaryEmail", "UserKey", "Mail")] + [ValidateNotNullOrEmpty()] + [string] + $User = $Script:PSGSuite.AdminEmail + ) + Process { + if ($PSBoundParameters.Keys -notcontains 'AddLabel' -and $PSBoundParameters.Keys -notcontains 'RemoveLabel') { + throw "You must specify a value for either AddLabel or RemoveLabel!" + } + if ($User -ceq 'me') { + $User = $Script:PSGSuite.AdminEmail + } + elseif ($User -notlike "*@*.*") { + $User = "$($User)@$($Script:PSGSuite.Domain)" + } + $serviceParams = @{ + Scope = 'https://mail.google.com' + ServiceType = 'Google.Apis.Gmail.v1.GmailService' + User = $User + } + $service = New-GoogleService @serviceParams + $msgId = switch ($PSCmdlet.ParameterSetName) { + MessageId { + $MessageId + } + Filter { + (Get-GSGmailMessageList -Filter $Filter -User $User).Id + } + } + $userLabels = @{} + Get-GSGmailLabel -User $User -Verbose:$false | ForEach-Object { + $userLabels[$_.Name] = $_.Id + } + $body = New-Object 'Google.Apis.Gmail.v1.Data.ModifyMessageRequest' + if ($PSBoundParameters.Keys -contains 'AddLabel') { + $addLs = New-Object 'System.Collections.Generic.List[System.String]' + foreach ($label in $AddLabel) { + try { + $addLs.Add($userLabels[$label]) + } + catch { + if ($ErrorActionPreference -eq 'Stop') { + $PSCmdlet.ThrowTerminatingError($_) + } + else { + Write-Error $_ + } + } + } + $body.AddLabelIds = $addLs + } + if ($PSBoundParameters.Keys -contains 'RemoveLabel') { + $remLs = New-Object 'System.Collections.Generic.List[System.String]' + foreach ($label in $RemoveLabel) { + try { + $remLs.Add($userLabels[$label]) + } + catch { + if ($ErrorActionPreference -eq 'Stop') { + $PSCmdlet.ThrowTerminatingError($_) + } + else { + Write-Error $_ + } + } + } + $body.RemoveLabelIds = $remLs + } + foreach ($message in $msgId) { + try { + $request = $service.Users.Messages.Modify($body, $User, $message) + Write-Verbose "Updating Labels on Message '$message' for user '$User'" + $request.Execute() | Add-Member -MemberType NoteProperty -Name 'User' -Value $User -PassThru + } + catch { + if ($ErrorActionPreference -eq 'Stop') { + $PSCmdlet.ThrowTerminatingError($_) + } + else { + Write-Error $_ + } + } + } + } +} diff --git a/README.md b/README.md index 31d6f91b..d6c841bc 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,12 @@ Update-GSSheetValue Export-GSSheet #### 2.15.2 -* Added `Set-GSGmailLabel` to configure various properties exposed by Users.Labels.Update() API. +* [Pull Request #94](https://github.com/scrthq/PSGSuite/pull/94) **Thanks, [@dwrusse](https://github.com/dwrusse)!** + * Added `Update-GSGmailLabel` to enable updating of Gmail label properties + * Added `Update-GSGmailMessageLabel` enable updating of labels attached to Gmail messages +* [Issue #93](https://github.com/scrthq/PSGSuite/issues/93) + * Updated `Remove-GSGmailMessage` to include a `-Filter` parameter to allow removal of messages matching a filter in a single command + * Improved pipeline support for `Remove-GSGmailMessage` #### 2.15.1 @@ -141,10 +146,10 @@ Update-GSSheetValue Export-GSSheet #### 2.15.0 -- Updated Gmail Delegation functions to use the .NET SDK after Google announced delegation support for the Gmail API -- Cleaned up `Get-GSGmailDelegates` by removing the trailing `s` (now `Get-GSGmailDelegate`). Added the original function as an alias to the new function for backwards compatibility with scripts. -- Removed the `Raw` parameter from `Get-GSGmailDelegate` since it's no longer applicable. -- Enabled `Get-GSGmailDelegate` to perform both Get and List requests (previously only performed List requests) +* Updated Gmail Delegation functions to use the .NET SDK after Google announced delegation support for the Gmail API +* Cleaned up `Get-GSGmailDelegates` by removing the trailing `s` (now `Get-GSGmailDelegate`). Added the original function as an alias to the new function for backwards compatibility with scripts. +* Removed the `Raw` parameter from `Get-GSGmailDelegate` since it's no longer applicable. +* Enabled `Get-GSGmailDelegate` to perform both Get and List requests (previously only performed List requests) #### 2.14.1 From 4ef79ae05b79aa7dafaf9823f60cd8d8bfd59443 Mon Sep 17 00:00:00 2001 From: Nate Ferrell Date: Tue, 9 Oct 2018 00:27:33 -0500 Subject: [PATCH 10/10] !build reordered ServiceBuilder calls for better verbose output --- PSGSuite/Public/Gmail/Remove-GSGmailMessage.ps1 | 2 +- PSGSuite/Public/Gmail/Update-GSGmailMessageLabel.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/PSGSuite/Public/Gmail/Remove-GSGmailMessage.ps1 b/PSGSuite/Public/Gmail/Remove-GSGmailMessage.ps1 index eb538271..a0e5aee7 100644 --- a/PSGSuite/Public/Gmail/Remove-GSGmailMessage.ps1 +++ b/PSGSuite/Public/Gmail/Remove-GSGmailMessage.ps1 @@ -66,7 +66,6 @@ function Remove-GSGmailMessage { ServiceType = 'Google.Apis.Gmail.v1.GmailService' User = $User } - $service = New-GoogleService @serviceParams $msgId = switch ($PSCmdlet.ParameterSetName) { MessageId { $Id @@ -75,6 +74,7 @@ function Remove-GSGmailMessage { (Get-GSGmailMessageList -Filter $Filter -User $User).Id } } + $service = New-GoogleService @serviceParams try { foreach ($mId in $msgId) { $request = switch ($Method) { diff --git a/PSGSuite/Public/Gmail/Update-GSGmailMessageLabel.ps1 b/PSGSuite/Public/Gmail/Update-GSGmailMessageLabel.ps1 index ae1500a3..e0d643d5 100644 --- a/PSGSuite/Public/Gmail/Update-GSGmailMessageLabel.ps1 +++ b/PSGSuite/Public/Gmail/Update-GSGmailMessageLabel.ps1 @@ -66,7 +66,6 @@ function Update-GSGmailMessageLabel { ServiceType = 'Google.Apis.Gmail.v1.GmailService' User = $User } - $service = New-GoogleService @serviceParams $msgId = switch ($PSCmdlet.ParameterSetName) { MessageId { $MessageId @@ -75,6 +74,7 @@ function Update-GSGmailMessageLabel { (Get-GSGmailMessageList -Filter $Filter -User $User).Id } } + $service = New-GoogleService @serviceParams $userLabels = @{} Get-GSGmailLabel -User $User -Verbose:$false | ForEach-Object { $userLabels[$_.Name] = $_.Id