SCRT-HQ / PSGSuite

Powershell module for Google / G Suite API calls wrapped in handy functions. Authentication is established using a service account via P12 key to negate the consent popup and allow for greater handsoff automation capabilities
https://psgsuite.io/
Apache License 2.0
235 stars 67 forks source link

Cannot update email address of group #344

Closed dresken closed 2 years ago

dresken commented 3 years ago

Describe the bug Cannot update email address of group. Looks like unreleased version may include updated Set-GSGroupSettings (but there's not an updated Update-GSGroupSettings) - however this drops the -Email parameter entirely. I haven't been able to locate a replacement for this functionality in PSGSuite. The API doco with https://developers.google.com/admin-sdk/groups-settings/v1/reference/groups say Email gets changed with Directory API (which I'm guessing means there should be a Update-GSGroup)

To Reproduce Steps to reproduce the behavior:

  1. Update-GSGroupSettings -Identity $groupId -Email new_email@example.com
  2. Returns error Update-GSGroupSettings : Exception calling "Execute" with "0" argument(s): "Google.Apis.Requests.RequestError Invalid Value [400] Errors [ Message[Invalid Value] Location[ - ] Reason[invalid] Domain[global] ]

Expected behavior Group Email address is updated to the new value

Environment (please complete the following information):

Buenno commented 3 years ago

I don't really have the time to write this to spec or generate a PR, and it's a bit...hacky, but this works for updating the group email address. Add the function to the psd1 file and then add the new function name to the module manifest function export list (psd1). Reimport the module once complete and you should be good to go.

I really should have used a parameter set for the -NewEmail parameter as Powershell is throw an error if you use the new function and don't specify that parameter. I'll try and sort this next week and generate a PR.

`function Update-GSGroup { <# .SYNOPSIS Updates a new Google Group

.DESCRIPTION
Updates a new Google Group

.PARAMETER Email
Current email address of group

.PARAMETER NewEmail
The updated email address of group

.PARAMETER Name
The updated name of the group

.PARAMETER Description
The updated description of the group

.EXAMPLE
Update-GSGroup -Email appdev -Name "Application Developers" -Description "App Dev team members" -NewEmail appdevelopers

Updates a group named "Application Developers" with the email "appdevelopers@domain.com" and description "App Dev team members"
#>
[OutputType('Google.Apis.Admin.Directory.directory_v1.Data.Group')]
[cmdletbinding()]
Param
(
    [parameter(Mandatory = $true)]
    [String]
    $Email,
    [parameter(Mandatory = $false)]
    [String]
    $Name,
    [parameter(Mandatory = $false)]
    [String]
    $Description,
    [parameter(Mandatory = $false)]
    [String]
    $NewEmail
)
Begin {
    $serviceParams = @{
        Scope       = 'https://www.googleapis.com/auth/admin.directory.group'
        ServiceType = 'Google.Apis.Admin.Directory.directory_v1.DirectoryService'
    }
    $service = New-GoogleService @serviceParams
}
Process {
    try {
        Resolve-Email ([ref]$Email)
        Write-Verbose "Updating group '$Email'"
        $body = New-Object 'Google.Apis.Admin.Directory.directory_v1.Data.Group'
        foreach ($prop in $PSBoundParameters.Keys | Where-Object {$body.PSObject.Properties.Name -contains $_}) {
            switch ($prop) {
                Email {
                    $body.Email = $NewEmail
                }
                Default {
                    $body.$prop = $PSBoundParameters[$prop]
                }
            }
        }
        $request = $service.Groups.Patch($body, $Email)
        $request.Alt = "Json"
        $request.Execute()
    }
    catch {
        if ($ErrorActionPreference -eq 'Stop') {
            $PSCmdlet.ThrowTerminatingError($_)
        }
        else {
            Write-Error $_
        }
    }
}

}

Export-ModuleMember -Function 'Update-GSGroup'`

FISHMANPET commented 2 years ago

Closed via #353