diff --git a/PSGSuite/Private/LicenseHelpers.ps1 b/PSGSuite/Private/LicenseHelpers.ps1 new file mode 100644 index 00000000..503973ea --- /dev/null +++ b/PSGSuite/Private/LicenseHelpers.ps1 @@ -0,0 +1,120 @@ +function Get-LicenseSkus { + ConvertFrom-Json '{"Google-Drive-storage-1TB": {"product": "Google-Drive-storage", "displayName": "Google Drive Storage 1TB", "aliases": ["drive1tb", "1tb", "googledrivestorage1tb"]}, "Google-Apps-For-Government": {"product": "Google-Apps", "displayName": "G Suite Government", "aliases": ["gafg", "gsuitegovernment", "gsuitegov"]}, "Google-Vault": {"product": "Google-Vault", "displayName": "Google Vault", "aliases": ["vault", "googlevault"]}, "Google-Drive-storage-8TB": {"product": "Google-Drive-storage", "displayName": "Google Drive Storage 8TB", "aliases": ["drive8tb", "8tb", "googledrivestorage8tb"]}, "Google-Drive-storage-400GB": {"product": "Google-Drive-storage", "displayName": "Google Drive Storage 400GB", "aliases": ["drive400gb", "400gb", "googledrivestorage400gb"]}, "Google-Drive-storage-16TB": {"product": "Google-Drive-storage", "displayName": "Google Drive Storage 16TB", "aliases": ["drive16tb", "16tb", "googledrivestorage16tb"]}, "1010340002": {"product": "101034", "displayName": "G Suite Business Archived", "aliases": ["gsbau", "businessarchived", "gsuitebusinessarchived"]}, "1010340001": {"product": "101034", "displayName": "G Suite Enterprise Archived", "aliases": ["gseau", "enterprisearchived", "gsuiteenterprisearchived"]}, "Google-Drive-storage-50GB": {"product": "Google-Drive-storage", "displayName": "Google Drive Storage 50GB", "aliases": ["drive50gb","50gb", "googledrivestorage50gb"]}, "Google-Apps": {"product": "Google-Apps", "displayName": "G Suite Free/Standard", "aliases": ["standard", "free"]}, "Google-Drive-storage-4TB": {"product": "Google-Drive-storage", "displayName": "Google Drive Storage 4TB", "aliases": ["drive4tb", "4tb", "googledrivestorage4tb"]}, "Google-Drive-storage-2TB": {"product": "Google-Drive-storage", "displayName": "Google Drive Storage 2TB", "aliases": ["drive2tb", "2tb", "googledrivestorage2tb"]}, "Google-Apps-For-Postini": {"product": "Google-Apps", "displayName": "G Suite Message Security", "aliases": ["gams", "postini", "gsuitegams", "gsuitepostini", "gsuitemessagesecurity"]}, "Google-Apps-Unlimited": {"product": "Google-Apps", "displayName": "G Suite Business", "aliases": ["gau", "gsb", "unlimited", "gsuitebusiness"]}, "Google-Drive-storage-200GB": {"product": "Google-Drive-storage", "displayName": "Google Drive Storage 200GB", "aliases": ["drive200gb", "200gb", "googledrivestorage200gb"]}, "1010010001": {"product": "101001", "displayName": "Cloud Identity", "aliases": ["identity", "cloudidentity"]}, "Google-Coordinate": {"product": "Google-Coordinate", "displayName": "Google Coordinate", "aliases": ["coordinate", "googlecoordinate"]}, "1010330002": {"product": "101033", "displayName": "Google Voice Premier", "aliases": ["gvpremier", "voicepremier", "googlevoicepremier"]}, "1010330003": {"product": "101033", "displayName": "Google Voice Starter", "aliases": ["gvstarter", "voicestarter", "googlevoicestarter"]}, "1010330004": {"product": "101033", "displayName": "Google Voice Standard", "aliases": ["gvstandard", "voicestandard", "googlevoicestandard"]}, "Google-Vault-Former-Employee": {"product": "Google-Vault", "displayName": "Google Vault Former Employee", "aliases": ["vfe", "googlevaultformeremployee"]}, "Google-Apps-For-Business": {"product": "Google-Apps", "displayName": "G Suite Basic", "aliases": ["gafb", "gafw", "basic", "gsuitebasic"]}, "1010060001": {"product": "Google-Apps", "displayName": "Drive Enterprise", "aliases": ["d4e", "driveenterprise", "drive4enterprise"]}, "1010020020": {"product": "Google-Apps", "displayName": "G Suite Enterprise", "aliases": ["gae", "gse", "enterprise", "gsuiteenterprise"]}, "Google-Apps-Lite": {"product": "Google-Apps", "displayName": "G Suite Lite", "aliases": ["gal", "gsl", "lite", "gsuitelite"]}, "1010050001": {"product": "101005", "displayName": "Cloud Identity Premium", "aliases": ["identitypremium", "cloudidentitypremium"]}, "1010310002": {"product": "101031", "displayName": "G Suite Enterprise for Education", "aliases": ["gsefe", "e4e", "gsuiteenterpriseeducation"]}, "1010310003": {"product": "101031", "displayName": "G Suite Enterprise for Education Student", "aliases": ["gsefes", "e4es", "gsuiteenterpriseeducationstudent"]}, "Google-Chrome-Device-Management": {"product": "Google-Chrome-Device-Management", "displayName": "Google Chrome Device Management", "aliases": ["chrome", "cdm", "googlechromedevicemanagement"]}, "Google-Drive-storage-20GB": {"product": "Google-Drive-storage", "displayName": "Google Drive Storage 20GB", "aliases": ["drive20gb", "20gb", "googledrivestorage20gb"]}}' +} + +function Get-LicenseProducts { + ConvertFrom-Json '{"Google-Vault": "Google Vault", "Google-Drive-storage": "Google Drive Storage", "Google-Coordinate": "Google Coordinate", "101034": "G Suite Archived", "101033": "Google Voice", "Google-Apps": "G Suite", "101031": "G Suite Enterprise for Education", "101006": "Drive Enterprise", "Google-Chrome-Device-Management": "Google Chrome Device Management", "101005": "Cloud Identity Premium", "101001": "Cloud Identity Free"}' +} + +function Get-LicenseProductHash { + Param ( + [Parameter(Position = 0)] + [String] + $Key + ) + $_result = @{'Cloud-Identity' = '101001'} + $_full = Get-LicenseProducts + $_products = $_full.PSObject.Properties.Name | Sort-Object -Unique + foreach ($_id in $_products) { + $_friendlyname = $_full.$_id -replace '\W+','-' + $_result[$_id] = $_id + $_result[$_friendlyname] = $_id + } + if ($Key) { + $_result[$Key] + } + else { + $_result + } +} + +function Get-LicenseSkuHash { + Param ( + [Parameter(Position = 0)] + [String] + $Key + ) + $_result = @{} + $_full = Get-LicenseSkus + $_licenses = $_full.PSObject.Properties.Name | Sort-Object -Unique + foreach ($_sku in $_licenses) { + $_displayName = $_full.$_sku.displayName -replace '\W+','-' + $_result[$_displayName] = $_sku + foreach ($_alias in $_full.$_sku.aliases) { + $_result[$_alias] = $_sku + } + } + if ($Key) { + $_result[$Key] + } + else { + $_result + } +} + +function Get-LicenseSkuToProductHash { + Param ( + [Parameter(Position = 0)] + [String] + $Key + ) + $_result = @{} + $_full = Get-LicenseSkus + $_licenses = $_full.PSObject.Properties.Name | Sort-Object -Unique + foreach ($_sku in $_licenses) { + $_displayName = $_full.$_sku.displayName -replace '\W+','-' + $_result[$_sku] = $_full.$_sku.product + $_result[$_displayName] = $_full.$_sku.product + foreach ($_alias in $_full.$_sku.aliases) { + $_result[$_alias] = $_full.$_sku.product + } + } + if ($Key) { + $_result[$Key] + } + else { + $_result + } +} + +function Get-LicenseSkuFromDisplayName { + Param ( + [Parameter(Position = 0)] + [String] + $Key + ) + $_result = @{} + $_full = Get-LicenseSkus + $_licenses = $_full.PSObject.Properties.Name | Sort-Object -Unique + foreach ($_sku in $_licenses) { + $_displayName = $_full.$_sku.displayName -replace '\W+','-' + $_result[$_displayName] = $_sku + } + if ($Key) { + $_result[$Key] + } + else { + $_result + } +} + +function Get-LicenseProductFromDisplayName { + Param ( + [Parameter(Position = 0)] + [String] + $Key + ) + $_result = @{'Cloud-Identity' = '101001'} + $_full = Get-LicenseProducts + $_products = $_full.PSObject.Properties.Name | Sort-Object -Unique + foreach ($_id in $_products) { + $_friendlyname = $_full.$_id -replace '\W+','-' + $_result[$_friendlyname] = $_id + } + if ($Key) { + $_result[$Key] + } + else { + $_result + } +} diff --git a/PSGSuite/Public/Licensing/Get-GSUserLicense.ps1 b/PSGSuite/Public/Licensing/Get-GSUserLicense.ps1 index 7c821e27..6f60421d 100644 --- a/PSGSuite/Public/Licensing/Get-GSUserLicense.ps1 +++ b/PSGSuite/Public/Licensing/Get-GSUserLicense.ps1 @@ -12,7 +12,7 @@ function Get-GSUserLicense { .PARAMETER License The license SKU to retrieve information for. If excluded, searches all license SKUs - .PARAMETER ProductID + .PARAMETER ProductId The product Id to list licenses for .PARAMETER PageSize @@ -30,59 +30,65 @@ function Get-GSUserLicense { [cmdletbinding(DefaultParameterSetName = "List")] Param ( - [parameter(Mandatory = $false,Position = 0,ValueFromPipeline = $true,ValueFromPipelineByPropertyName = $true,ParameterSetName = "Get")] - [Alias("PrimaryEmail","UserKey","Mail")] + [parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = "Get")] + [Alias("PrimaryEmail", "UserKey", "Mail")] [ValidateNotNullOrEmpty()] [String[]] $User, - [parameter(Mandatory = $false)] - [Alias("SkuId")] - [ValidateSet("Cloud-Identity","Cloud-Identity-Premium","Drive-Enterprise","G-Suite-Enterprise","Google-Apps-Unlimited","Google-Apps-For-Business","Google-Apps-For-Postini","Google-Apps-Lite","Google-Drive-storage-20GB","Google-Drive-storage-50GB","Google-Drive-storage-200GB","Google-Drive-storage-400GB","Google-Drive-storage-1TB","Google-Drive-storage-2TB","Google-Drive-storage-4TB","Google-Drive-storage-8TB","Google-Drive-storage-16TB","Google-Vault","Google-Vault-Former-Employee","1010020020","1010060001","1010010001","1010050001")] - [string] - $License, - [parameter(Mandatory = $false,ParameterSetName = "List")] - [ValidateSet("Google-Apps","Google-Drive-storage","Google-Vault","Cloud-Identity","Cloud-Identity-Premium")] - [string[]] - $ProductID = @("Google-Apps","Google-Drive-storage","Google-Vault","Cloud-Identity","Cloud-Identity-Premium"), - [parameter(Mandatory = $false,ParameterSetName = "List")] + [parameter(Mandatory = $false, ParameterSetName = "List")] [Alias("MaxResults")] - [ValidateRange(1,1000)] + [ValidateRange(1, 1000)] [Int] $PageSize = 1000, - [parameter(Mandatory = $false,ParameterSetName = "List")] + [parameter(Mandatory = $false, ParameterSetName = "List")] [Alias('First')] [Int] $Limit = 0 ) + DynamicParam { + $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary + + # License + $_licenses = (Get-LicenseSkuHash).Keys | Sort-Object -Unique + $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] + $attribute = New-Object System.Management.Automation.ParameterAttribute + $attribute.Mandatory = $false + $attributeCollection.Add($attribute) + $attribute = New-Object System.Management.Automation.AliasAttribute('SkuId') + $attributeCollection.Add($attribute) + $attribute = New-Object System.Management.Automation.ValidateSetAttribute($_licenses) + $attributeCollection.Add($attribute) + $Name = 'License' + $dynParam = New-Object System.Management.Automation.RuntimeDefinedParameter($Name, [string], $attributeCollection) + $paramDictionary.Add($Name, $dynParam) + + # ProductId + $_products = (Get-LicenseProductHash).Keys | Sort-Object -Unique + $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] + $attribute = New-Object System.Management.Automation.ParameterAttribute + $attribute.Mandatory = $false + $attribute.ParameterSetName = 'List' + $attributeCollection.Add($attribute) + $attribute = New-Object System.Management.Automation.ValidateSetAttribute($_products) + $attributeCollection.Add($attribute) + $Name = 'ProductId' + $dynParam = New-Object System.Management.Automation.RuntimeDefinedParameter($Name, [string[]], $attributeCollection) + $paramDictionary.Add($Name, $dynParam) + # return the collection of dynamic parameters + return $paramDictionary + } Begin { $serviceParams = @{ Scope = 'https://www.googleapis.com/auth/apps.licensing' ServiceType = 'Google.Apis.Licensing.v1.LicensingService' } $service = New-GoogleService @serviceParams - $productHash = @{ - 'Cloud-Identity' = '101001' # Cloud-Identity - '1010010001' = '101001' # Cloud-Identity - 'Cloud-Identity-Premium' = '101005' # Cloud-Identity-Premium - '1010050001' = '101005' # Cloud-Identity-Premium - '1010020020' = 'Google-Apps' # G-Suite-Enterprise - '1010060001' = 'Google-Apps' # Drive-Enterprise - 'G-Suite-Enterprise' = 'Google-Apps' - 'Google-Apps-Unlimited' = 'Google-Apps' - 'Google-Apps-For-Business' = 'Google-Apps' - 'Google-Apps-For-Postini' = 'Google-Apps' - 'Google-Apps-Lite' = 'Google-Apps' - 'Google-Vault' = 'Google-Vault' - 'Google-Vault-Former-Employee' = 'Google-Vault' - 'Google-Drive-storage-20GB' = 'Google-Drive-storage' - 'Google-Drive-storage-50GB' = 'Google-Drive-storage' - 'Google-Drive-storage-200GB' = 'Google-Drive-storage' - 'Google-Drive-storage-400GB' = 'Google-Drive-storage' - 'Google-Drive-storage-1TB' = 'Google-Drive-storage' - 'Google-Drive-storage-2TB' = 'Google-Drive-storage' - 'Google-Drive-storage-4TB' = 'Google-Drive-storage' - 'Google-Drive-storage-8TB' = 'Google-Drive-storage' - 'Google-Drive-storage-16TB' = 'Google-Drive-storage' + $License = $PSBoundParameters['License'] + $ProductId = if ($PSBoundParameters.ContainsKey('ProductId')) { + $PSBoundParameters['ProductId'] + } + else { + (Get-LicenseProductFromDisplayName).Keys | Where-Object {$_ -ne 'Cloud-Identity'} | Sort-Object } } Process { @@ -91,50 +97,19 @@ function Get-GSUserLicense { Get { foreach ($U in $User) { $response = $null - if ($U -ceq 'me') { - $U = $Script:PSGSuite.AdminEmail - } - elseif ($U -notlike "*@*.*") { - $U = "$($U)@$($Script:PSGSuite.Domain)" - } + Resolve-Email ([ref]$U) if ($PSBoundParameters.ContainsKey('License')) { Write-Verbose "Getting License SKU '$License' for User '$U'" - switch ($License) { - "G-Suite-Enterprise" { - $License = "1010020020" - } - "Drive-Enterprise" { - $License = "1010060001" - } - "Cloud-Identity" { - $License = "1010010001" - } - "Cloud-Identity-Premium" { - $License = "1010050001" - } - } - $request = $service.LicenseAssignments.Get($productHash[$License],$License,$U) + $License = Get-LicenseSkuFromDisplayName $License + $request = $service.LicenseAssignments.Get((Get-LicenseSkuToProductHash $License), $License, $U) $request.Execute() } else { - foreach ($license in (@("G-Suite-Enterprise","Google-Apps-Unlimited","Google-Apps-For-Business","Google-Vault","Google-Vault-Former-Employee","Cloud-Identity","Cloud-Identity-Premium","Drive-Enterprise","Google-Apps-For-Postini","Google-Apps-Lite","Google-Drive-storage-20GB","Google-Drive-storage-50GB","Google-Drive-storage-200GB","Google-Drive-storage-400GB","Google-Drive-storage-1TB","Google-Drive-storage-2TB","Google-Drive-storage-4TB","Google-Drive-storage-8TB","Google-Drive-storage-16TB") | Sort-Object)) { + foreach ($License in (Get-LicenseSkuFromDisplayName).Keys | Sort-Object) { Write-Verbose "Getting License SKU '$License' for User '$U'" - switch ($License) { - "G-Suite-Enterprise" { - $License = "1010020020" - } - "Drive-Enterprise" { - $License = "1010060001" - } - "Cloud-Identity" { - $License = "1010010001" - } - "Cloud-Identity-Premium" { - $License = "1010050001" - } - } + $License = Get-LicenseSkuFromDisplayName $License try { - $request = $service.LicenseAssignments.Get($productHash[$License],$License,$U) + $request = $service.LicenseAssignments.Get((Get-LicenseSkuToProductHash $License), $License, $U) $response = $request.Execute() } catch { @@ -154,44 +129,26 @@ function Get-GSUserLicense { } List { if ($License) { - $ProductID = $productHash[$License] + $ProductID = Get-LicenseSkuToProductHash $License } $total = 0 - try { - $overLimit = $false - foreach ($prodId in $ProductID) { + $overLimit = $false + foreach ($prodId in $ProductID) { + $origProdId = $prodId + try { if (-not $overLimit) { - Write-Verbose "Retrieving licenses for product '$prodId'" - switch ($prodId) { - "Cloud-Identity" { - $prodId = "101001" - } - "Cloud-Identity-Premium" { - $prodId = "101005" - } - } + Write-Verbose "Retrieving licenses for product '$origProdId'" + $prodId = Get-LicenseProductHash $prodId if ($License) { - switch ($License) { - "G-Suite-Enterprise" { - $License = "1010020020" - } - "Drive-Enterprise" { - $License = "1010060001" - } - "Cloud-Identity" { - $License = "1010010001" - } - "Cloud-Identity-Premium" { - $License = "1010050001" - } - } - $request = $service.LicenseAssignments.ListForProductAndSku($prodId,$License,$Script:PSGSuite.Domain) + $origLicense = $License + $License = Get-LicenseSkuFromDisplayName $License + $request = $service.LicenseAssignments.ListForProductAndSku($prodId, $License, $Script:PSGSuite.Domain) } else { - $request = $service.LicenseAssignments.ListForProduct($prodId,$Script:PSGSuite.Domain) + $request = $service.LicenseAssignments.ListForProduct($prodId, $Script:PSGSuite.Domain) } if ($Limit -gt 0 -and $PageSize -gt $Limit) { - Write-Verbose ("Reducing PageSize from {0} to {1} to meet limit with first page" -f $PageSize,$Limit) + Write-Verbose ("Reducing PageSize from {0} to {1} to meet limit with first page" -f $PageSize, $Limit) $PageSize = $Limit } $request.MaxResults = $PageSize @@ -204,10 +161,10 @@ function Get-GSUserLicense { $request.PageToken = $result.NextPageToken [int]$retrieved = ($i + $result.Items.Count) - 1 if ($License) { - Write-Verbose "Retrieved $retrieved licenses for product '$prodId' & sku '$License'..." + Write-Verbose "Retrieved $retrieved licenses for product '$origProdId' & sku '$origLicense'..." } else { - Write-Verbose "Retrieved $retrieved licenses for product '$prodId'..." + Write-Verbose "Retrieved $retrieved licenses for product '$origProdId'..." } if ($Limit -gt 0 -and $total -eq $Limit) { Write-Verbose "Limit reached: $Limit" @@ -215,7 +172,7 @@ function Get-GSUserLicense { } elseif ($Limit -gt 0 -and ($total + $PageSize) -gt $Limit) { $newPS = $Limit - $total - Write-Verbose ("Reducing PageSize from {0} to {1} to meet limit with next page" -f $PageSize,$newPS) + Write-Verbose ("Reducing PageSize from {0} to {1} to meet limit with next page" -f $PageSize, $newPS) $request.MaxResults = $newPS } [int]$i = $i + $result.Items.Count @@ -223,16 +180,21 @@ function Get-GSUserLicense { until ($overLimit -or !$result.NextPageToken) } } - Write-Verbose "Retrieved $total total licenses" - } - catch { - if ($ErrorActionPreference -eq 'Stop') { - $PSCmdlet.ThrowTerminatingError($_) - } - else { - Write-Error $_ + catch { + if ($_.Exception.Message -notmatch 'Invalid productId') { + if ($ErrorActionPreference -eq 'Stop') { + $PSCmdlet.ThrowTerminatingError($_) + } + else { + Write-Error $_ + } + } + else { + Write-Verbose "Retrieved $retrieved licenses for product '$origProdId'..." + } } } + Write-Verbose "Retrieved $total total licenses" } } } diff --git a/PSGSuite/Public/Licensing/Remove-GSUserLicense.ps1 b/PSGSuite/Public/Licensing/Remove-GSUserLicense.ps1 index 18b17bd1..1ae8b0fd 100644 --- a/PSGSuite/Public/Licensing/Remove-GSUserLicense.ps1 +++ b/PSGSuite/Public/Licensing/Remove-GSUserLicense.ps1 @@ -2,16 +2,16 @@ function Remove-GSUserLicense { <# .SYNOPSIS Removes a license assignment from a user - + .DESCRIPTION Removes a license assignment from a user. Useful for restoring a user from a Vault-Former-Employee to an auto-assigned G Suite Business license by removing the Vault-Former-Employee license, for example. - + .PARAMETER User The user's current primary email address - + .PARAMETER License The license SKU to remove from the user - + .EXAMPLE Remove-GSUserLicense -User joe -License Google-Vault-Former-Employee @@ -24,51 +24,44 @@ function Remove-GSUserLicense { [Alias("PrimaryEmail","UserKey","Mail")] [ValidateNotNullOrEmpty()] [string[]] - $User, - [parameter(Mandatory = $true,Position = 1)] - [Alias("SkuId")] - [ValidateSet("G-Suite-Enterprise","Google-Apps-Unlimited","Google-Apps-For-Business","Google-Apps-For-Postini","Google-Apps-Lite","Google-Drive-storage-20GB","Google-Drive-storage-50GB","Google-Drive-storage-200GB","Google-Drive-storage-400GB","Google-Drive-storage-1TB","Google-Drive-storage-2TB","Google-Drive-storage-4TB","Google-Drive-storage-8TB","Google-Drive-storage-16TB","Google-Vault","Google-Vault-Former-Employee","1010020020")] - [string] - $License + $User ) + DynamicParam { + $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary + + # License + $_licenses = (Get-LicenseSkuHash).Keys | Sort-Object -Unique + $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] + $attribute = New-Object System.Management.Automation.ParameterAttribute + $attribute.Mandatory = $true + $attribute.Position = 1 + $attributeCollection.Add($attribute) + $attribute = New-Object System.Management.Automation.AliasAttribute('SkuId') + $attributeCollection.Add($attribute) + $attribute = New-Object System.Management.Automation.ValidateSetAttribute($_licenses) + $attributeCollection.Add($attribute) + $Name = 'License' + $dynParam = New-Object System.Management.Automation.RuntimeDefinedParameter($Name, [string], $attributeCollection) + $paramDictionary.Add($Name, $dynParam) + + return $paramDictionary + } Begin { $serviceParams = @{ Scope = 'https://www.googleapis.com/auth/apps.licensing' ServiceType = 'Google.Apis.Licensing.v1.LicensingService' } $service = New-GoogleService @serviceParams - $productHash = @{ - '1010020020' = 'Google-Apps' - 'G-Suite-Enterprise' = 'Google-Apps' - 'Google-Apps-Unlimited' = 'Google-Apps' - 'Google-Apps-For-Business' = 'Google-Apps' - 'Google-Apps-For-Postini' = 'Google-Apps' - 'Google-Apps-Lite' = 'Google-Apps' - 'Google-Vault' = 'Google-Vault' - 'Google-Vault-Former-Employee' = 'Google-Vault' - 'Google-Drive-storage-20GB' = 'Google-Drive-storage' - 'Google-Drive-storage-50GB' = 'Google-Drive-storage' - 'Google-Drive-storage-200GB' = 'Google-Drive-storage' - 'Google-Drive-storage-400GB' = 'Google-Drive-storage' - 'Google-Drive-storage-1TB' = 'Google-Drive-storage' - 'Google-Drive-storage-2TB' = 'Google-Drive-storage' - 'Google-Drive-storage-4TB' = 'Google-Drive-storage' - 'Google-Drive-storage-8TB' = 'Google-Drive-storage' - 'Google-Drive-storage-16TB' = 'Google-Drive-storage' - } + $License = $PSBoundParameters['License'] } Process { try { foreach ($U in $User) { - if ($U -notlike "*@*.*") { - $U = "$($U)@$($Script:PSGSuite.Domain)" - } + Resolve-Email ([ref]$U) if ($PSCmdlet.ShouldProcess("Revoking license '$License' from user '$U'")) { Write-Verbose "Revoking license '$License' from user '$U'" - if ($License -eq "G-Suite-Enterprise") { - $License = "1010020020" - } - $request = $service.LicenseAssignments.Delete($productHash[$License],$License,$U) + $License = Get-LicenseSkuFromDisplayName $License + $request = $service.LicenseAssignments.Delete((Get-LicenseSkuToProductHash $License),$License,$U) $request.Execute() Write-Verbose "License revoked for user '$U'" } @@ -83,4 +76,4 @@ function Remove-GSUserLicense { } } } -} \ No newline at end of file +} diff --git a/PSGSuite/Public/Licensing/Set-GSUserLicense.ps1 b/PSGSuite/Public/Licensing/Set-GSUserLicense.ps1 index b44c5a85..7984f9ec 100644 --- a/PSGSuite/Public/Licensing/Set-GSUserLicense.ps1 +++ b/PSGSuite/Public/Licensing/Set-GSUserLicense.ps1 @@ -25,53 +25,46 @@ function Set-GSUserLicense { [Alias("PrimaryEmail","UserKey","Mail")] [ValidateNotNullOrEmpty()] [String[]] - $User, - [parameter(Mandatory = $true,Position = 1)] - [Alias("SkuId")] - [ValidateSet("G-Suite-Enterprise","Google-Apps-Unlimited","Google-Apps-For-Business","Google-Apps-For-Postini","Google-Apps-Lite","Google-Drive-storage-20GB","Google-Drive-storage-50GB","Google-Drive-storage-200GB","Google-Drive-storage-400GB","Google-Drive-storage-1TB","Google-Drive-storage-2TB","Google-Drive-storage-4TB","Google-Drive-storage-8TB","Google-Drive-storage-16TB","Google-Vault","Google-Vault-Former-Employee","1010020020")] - [string] - $License + $User ) + DynamicParam { + $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary + + # License + $_licenses = (Get-LicenseSkuHash).Keys | Sort-Object -Unique + $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] + $attribute = New-Object System.Management.Automation.ParameterAttribute + $attribute.Mandatory = $true + $attribute.Position = 1 + $attributeCollection.Add($attribute) + $attribute = New-Object System.Management.Automation.AliasAttribute('SkuId') + $attributeCollection.Add($attribute) + $attribute = New-Object System.Management.Automation.ValidateSetAttribute($_licenses) + $attributeCollection.Add($attribute) + $Name = 'License' + $dynParam = New-Object System.Management.Automation.RuntimeDefinedParameter($Name, [string], $attributeCollection) + $paramDictionary.Add($Name, $dynParam) + + return $paramDictionary + } Begin { $serviceParams = @{ Scope = 'https://www.googleapis.com/auth/apps.licensing' ServiceType = 'Google.Apis.Licensing.v1.LicensingService' } $service = New-GoogleService @serviceParams - $productHash = @{ - '1010020020' = 'Google-Apps' - 'G-Suite-Enterprise' = 'Google-Apps' - 'Google-Apps-Unlimited' = 'Google-Apps' - 'Google-Apps-For-Business' = 'Google-Apps' - 'Google-Apps-For-Postini' = 'Google-Apps' - 'Google-Apps-Lite' = 'Google-Apps' - 'Google-Vault' = 'Google-Vault' - 'Google-Vault-Former-Employee' = 'Google-Vault' - 'Google-Drive-storage-20GB' = 'Google-Drive-storage' - 'Google-Drive-storage-50GB' = 'Google-Drive-storage' - 'Google-Drive-storage-200GB' = 'Google-Drive-storage' - 'Google-Drive-storage-400GB' = 'Google-Drive-storage' - 'Google-Drive-storage-1TB' = 'Google-Drive-storage' - 'Google-Drive-storage-2TB' = 'Google-Drive-storage' - 'Google-Drive-storage-4TB' = 'Google-Drive-storage' - 'Google-Drive-storage-8TB' = 'Google-Drive-storage' - 'Google-Drive-storage-16TB' = 'Google-Drive-storage' - } + $License = $PSBoundParameters['License'] } Process { try { foreach ($U in $User) { - if ($U -notlike "*@*.*") { - $U = "$($U)@$($Script:PSGSuite.Domain)" - } + Resolve-Email ([ref]$U) Write-Verbose "Setting license for $U to $License" - if ($License -eq "G-Suite-Enterprise") { - $License = "1010020020" - } + $License = Get-LicenseSkuFromDisplayName $License $body = New-Object 'Google.Apis.Licensing.v1.Data.LicenseAssignmentInsert' -Property @{ UserId = $U } - $request = $service.LicenseAssignments.Insert($body,$productHash[$License],$License) + $request = $service.LicenseAssignments.Insert($body,(Get-LicenseSkuToProductHash $License),$License) $request.Execute() | Add-Member -MemberType NoteProperty -Name 'User' -Value $U -PassThru } } diff --git a/PSGSuite/Public/Licensing/Update-GSUserLicense.ps1 b/PSGSuite/Public/Licensing/Update-GSUserLicense.ps1 index daef1c90..51164f95 100644 --- a/PSGSuite/Public/Licensing/Update-GSUserLicense.ps1 +++ b/PSGSuite/Public/Licensing/Update-GSUserLicense.ps1 @@ -25,51 +25,44 @@ function Update-GSUserLicense { [Alias("PrimaryEmail","UserKey","Mail")] [ValidateNotNullOrEmpty()] [String[]] - $User, - [parameter(Mandatory = $false)] - [Alias("SkuId")] - [ValidateSet("G-Suite-Enterprise","Google-Apps-Unlimited","Google-Apps-For-Business","Google-Apps-For-Postini","Google-Apps-Lite","Google-Drive-storage-20GB","Google-Drive-storage-50GB","Google-Drive-storage-200GB","Google-Drive-storage-400GB","Google-Drive-storage-1TB","Google-Drive-storage-2TB","Google-Drive-storage-4TB","Google-Drive-storage-8TB","Google-Drive-storage-16TB","Google-Vault","Google-Vault-Former-Employee","1010020020")] - [string] - $License + $User ) + DynamicParam { + $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary + + # License + $_licenses = (Get-LicenseSkuHash).Keys | Sort-Object -Unique + $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] + $attribute = New-Object System.Management.Automation.ParameterAttribute + $attribute.Mandatory = $false + $attribute.Position = 1 + $attributeCollection.Add($attribute) + $attribute = New-Object System.Management.Automation.AliasAttribute('SkuId') + $attributeCollection.Add($attribute) + $attribute = New-Object System.Management.Automation.ValidateSetAttribute($_licenses) + $attributeCollection.Add($attribute) + $Name = 'License' + $dynParam = New-Object System.Management.Automation.RuntimeDefinedParameter($Name, [string], $attributeCollection) + $paramDictionary.Add($Name, $dynParam) + + return $paramDictionary + } Begin { $serviceParams = @{ Scope = 'https://www.googleapis.com/auth/apps.licensing' ServiceType = 'Google.Apis.Licensing.v1.LicensingService' } $service = New-GoogleService @serviceParams - $productHash = @{ - '1010020020' = 'Google-Apps' - 'G-Suite-Enterprise' = 'Google-Apps' - 'Google-Apps-Unlimited' = 'Google-Apps' - 'Google-Apps-For-Business' = 'Google-Apps' - 'Google-Apps-For-Postini' = 'Google-Apps' - 'Google-Apps-Lite' = 'Google-Apps' - 'Google-Vault' = 'Google-Vault' - 'Google-Vault-Former-Employee' = 'Google-Vault' - 'Google-Drive-storage-20GB' = 'Google-Drive-storage' - 'Google-Drive-storage-50GB' = 'Google-Drive-storage' - 'Google-Drive-storage-200GB' = 'Google-Drive-storage' - 'Google-Drive-storage-400GB' = 'Google-Drive-storage' - 'Google-Drive-storage-1TB' = 'Google-Drive-storage' - 'Google-Drive-storage-2TB' = 'Google-Drive-storage' - 'Google-Drive-storage-4TB' = 'Google-Drive-storage' - 'Google-Drive-storage-8TB' = 'Google-Drive-storage' - 'Google-Drive-storage-16TB' = 'Google-Drive-storage' - } + $License = $PSBoundParameters['License'] } Process { try { foreach ($U in $User) { - if ($U -notlike "*@*.*") { - $U = "$($U)@$($Script:PSGSuite.Domain)" - } + Resolve-Email ([ref]$U) Write-Verbose "Setting license for $U to $License" - if ($License -eq "G-Suite-Enterprise") { - $License = "1010020020" - } + $License = Get-LicenseSkuFromDisplayName $License $body = Get-GSUserLicense -User $U - $request = $service.LicenseAssignments.Update($body,$productHash[$License],$License,$U) + $request = $service.LicenseAssignments.Update($body,(Get-LicenseSkuToProductHash $License),$License,$U) $request.Execute() } }