Skip to content

Commit

Permalink
!deploy v2.23.1 with fixes for Get-GSUsageReport
Browse files Browse the repository at this point in the history
## 2.23.1

**This update changes the output of `Get-GSUsageReport` -- please review the output changes before updating if you have scripts that use that function!!**

* Fixed: `Get-GSUsageReport` wasn't displaying critical report information (such as the Entity info) due to Select-Object being hardcoded. Function has been updated to parse the resulting Parameters and Entity info out to the top-level object.
  * Added: `Flat` switch to specify that the parsed properties match what GAM returns, i.e. `'gmail:num_outbound_unencrypted_emails' = 6`. Normal behavior would be to parse that into an ordered dictionary, i.e. `gmail['num_outbound_unencrypted_emails'] = 6`, so that only `gmail` is seen from the top level object and all relevant report data is captured in the underlying dictionary.
  * Added: `Raw` switch to allow the raw UsageReportsValue to be returned instead of parsing it out.
  • Loading branch information
scrthq authored Mar 5, 2019
2 parents 0728bb5 + e21dc1d commit 929d347
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 52 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

* [Changelog](#changelog)
* [2.23.1](#2231)
* [2.23.0](#2230)
* [2.22.4](#2224)
* [2.22.3](#2223)
Expand Down Expand Up @@ -74,6 +75,14 @@

***

## 2.23.1

**This update changes the output of `Get-GSUsageReport` -- please review the output changes before updating if you have scripts that use that function!!**

* Fixed: `Get-GSUsageReport` wasn't displaying critical report information (such as the Entity info) due to Select-Object being hardcoded. Function has been updated to parse the resulting Parameters and Entity info out to the top-level object.
* Added: `Flat` switch to specify that the parsed properties match what GAM returns, i.e. `'gmail:num_outbound_unencrypted_emails' = 6`. Normal behavior would be to parse that into an ordered dictionary, i.e. `gmail['num_outbound_unencrypted_emails'] = 6`, so that only `gmail` is seen from the top level object and all relevant report data is captured in the underlying dictionary.
* Added: `Raw` switch to allow the raw UsageReportsValue to be returned instead of parsing it out.

## 2.23.0

* [Issue #152](https://github.com/scrthq/PSGSuite/issues/152)
Expand Down
2 changes: 1 addition & 1 deletion PSGSuite/PSGSuite.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'PSGSuite.psm1'

# Version number of this module.
ModuleVersion = '2.23.0'
ModuleVersion = '2.23.1'

# ID used to uniquely identify this module
GUID = '9d751152-e83e-40bb-a6db-4c329092aaec'
Expand Down
133 changes: 95 additions & 38 deletions PSGSuite/Public/Reports/Get-GSUsageReport.ps1
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
function Get-GSUsageReport {
<#
.SYNOPSIS
Retrieves a list of activities
Retrieves the usage report for the specified type.
Defaults to Customer Usage Report type.
.DESCRIPTION
Retrieves a list of activities
Retrieves the usage report for the specified type.
Defaults to Customer Usage Report type.
.PARAMETER Date
Represents the date for which the data is to be fetched
Represents the date for which the data is to be fetched. Defaults to 3 days before the current date.
.PARAMETER UserKey
Represents the profile id or the user email for which the data should be filtered
[User Usage Report] Represents the profile id or the user email for which the data should be filtered.
Use 'all' to retrieve the report for all users.
.PARAMETER EntityType
Type of object. Should be one of - gplus_communities
[Entity Usage Report] Type of object. Should be one of:
* gplus_communities
.PARAMETER EntityKey
Represents the key of object for which the data should be filtered
[Entity Usage Report] Represents the key of object for which the data should be filtered.
Use 'all' to retrieve the report for all users.
.PARAMETER Filters
Represents the set of filters including parameter operator value
.PARAMETER Parameters
Represents the application name, parameter name pairs to fetch in csv as app_name1:param_name1
.PARAMETER PageSize
Maximum number of results to return. Maximum allowed is 1000
.EXAMPLE
Get-GSUsageReport -Date (Get-Date).AddDays(-30)
Expand All @@ -35,23 +44,23 @@ function Get-GSUsageReport {
[cmdletbinding(DefaultParameterSetName = "Customer")]
Param
(
[parameter(Mandatory = $true,ParameterSetName = "Customer")]
[parameter(Mandatory = $true,ParameterSetName = "Entity")]
[parameter(Mandatory = $true,ParameterSetName = "User")]
[parameter(Mandatory = $false,ParameterSetName = "Customer")]
[parameter(Mandatory = $false,ParameterSetName = "Entity")]
[parameter(Mandatory = $false,ParameterSetName = "User")]
[DateTime]
$Date,
$Date = (Get-Date).AddDays(-3),
[parameter(Mandatory = $true,ParameterSetName = "User")]
[ValidateNotNullOrEmpty()]
[String]
$UserKey,
[parameter(Mandatory = $true,ParameterSetName = "Entity")]
[ValidateNotNullOrEmpty()]
[ValidateSet('gplus_communities')]
[String]
$EntityType,
[parameter(Mandatory = $true,ParameterSetName = "Entity")]
[parameter(Mandatory = $false,ParameterSetName = "Entity")]
[ValidateNotNullOrEmpty()]
[String]
$EntityKey,
$EntityKey = 'all',
[parameter(Mandatory = $false,ParameterSetName = "Entity")]
[parameter(Mandatory = $false,ParameterSetName = "User")]
[String[]]
Expand All @@ -64,16 +73,20 @@ function Get-GSUsageReport {
[ValidateRange(1,1000)]
[Alias("MaxResults")]
[Int]
$PageSize = "1000"
$PageSize = "1000",
[parameter(Mandatory = $false)]
[switch]
$Flat,
[parameter(Mandatory = $false)]
[switch]
$Raw
)
Begin {
$serviceParams = @{
Scope = 'https://www.googleapis.com/auth/admin.reports.usage.readonly'
ServiceType = 'Google.Apis.Admin.Reports.reports_v1.ReportsService'
}
$service = New-GoogleService @serviceParams
$props = @()
$props += (@{N = "Date";E = {$Date.ToString('yyyy-MM-dd')}})
}
Process {
try {
Expand All @@ -85,22 +98,18 @@ function Get-GSUsageReport {
Entity {
$request = $service.EntityUsageReports.Get($EntityType,$EntityKey,($Date.ToString('yyyy-MM-dd')))
$request.MaxResults = $PageSize
$props += (@{N = "EntityType";E = {$EntityType}})
$props += (@{N = "EntityKey";E = {$EntityKey}})
}
User {
if ($UserKey -ceq 'me') {
$UserKey = $Script:PSGSuite.AdminEmail
}
elseif ($UserKey -notlike "*@*.*") {
elseif ($UserKey -notlike "*@*.*" -and $UserKey -ne 'all') {
$UserKey = "$($UserKey)@$($Script:PSGSuite.Domain)"
}
$request = $service.UserUsageReport.Get($UserKey,($Date.ToString('yyyy-MM-dd')))
$request.MaxResults = $PageSize
$props += (@{N = "UserKey";E = {$UserKey}})
}
}
$props += '*'
foreach ($key in $PSBoundParameters.Keys | Where-Object {$_ -notin @('Date','UserKey','EntityKey','EntityType')}) {
switch ($key) {
Filters {
Expand All @@ -116,25 +125,73 @@ function Get-GSUsageReport {
}
}
}
$response = @()
$warnings = @()
[int]$i = 1
do {
$result = $request.Execute()
$response += $result.UsageReportsValue.Parameters | Select-Object $props
if ($Raw) {
$result.UsageReportsValue
}
else {
$result.UsageReportsValue | ForEach-Object {
$orig = $_
$orig | Add-Member -MemberType NoteProperty -Name CustomerId -Value $orig.Entity.CustomerId -Force -PassThru | Add-Member -MemberType NoteProperty -Name EntityType -Value $orig.Entity.Type -Force
switch ($PSCmdlet.ParameterSetName) {
Entity {
$orig | Add-Member -MemberType NoteProperty -Name EntityKey -Value $orig.Entity.EntityKey -Force
$orig | Add-Member -MemberType NoteProperty -Name CommunityName -Value $orig.Parameters[$orig.Parameters.Name.IndexOf('gplus:community_name')].StringValue -Force
}
User {
$orig | Add-Member -MemberType NoteProperty -Name Email -Value $orig.Entity.UserEmail -Force -PassThru | Add-Member -MemberType NoteProperty -Name UserEmail -Value $orig.Entity.UserEmail -Force -PassThru | Add-Member -MemberType NoteProperty -Name ProfileId -Value $orig.Entity.ProfileId -Force
}
}
foreach ($param in $orig.Parameters | Sort-Object Name) {
if ($null -ne $param.Name) {
$paramValue = if ($null -ne $param.StringValue) {
$param.StringValue
}
elseif ($null -ne $param.IntValue) {
$param.IntValue
}
elseif ($null -ne $param.DatetimeValue) {
$param.DatetimeValue
}
elseif ($null -ne $param.BoolValue) {
$param.BoolValue
}
elseif ($null -ne $param.MsgValue) {
$param.MsgValue
}
else {
$null
}
if ($Flat) {
$orig | Add-Member -MemberType NoteProperty -Name $param.Name -Value $paramValue -Force
}
else {
$pName = $param.Name -split ":"
if ($orig.PSObject.Properties.Name -notcontains $pName[0]) {
$orig | Add-Member -MemberType NoteProperty -Name $pName[0] -Value $([Ordered]@{}) -Force
}
$orig.$($pName[0])[$pName[1]] = $paramValue
}
}
}
$orig
}
}
$warnings += $result.Warnings
$request.PageToken = $result.NextPageToken
[int]$retrieved = ($i + $result.UsageReportsValue.Parameters.Count) - 1
Write-Verbose "Retrieved $retrieved report parameters..."
[int]$i = $i + $result.UsageReportsValue.Parameters.Count
[int]$retrieved = ($i + $result.UsageReportsValue.Count) - 1
Write-Verbose "Retrieved $retrieved entities for this report..."
[int]$i = $i + $result.UsageReportsValue.Count
}
until (!$result.NextPageToken)
if ($warnings) {
if ($warnings | Where-Object {$_.Code}) {
$warnings | ForEach-Object {
Write-Warning "[$($_.Code)] $($_.Message)"
}
}
return $response
}
catch {
if ($ErrorActionPreference -eq 'Stop') {
Expand All @@ -145,4 +202,4 @@ function Get-GSUsageReport {
}
}
}
}
}
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,10 @@ Update-GSSheetValue Export-GSSheet

[Full CHANGELOG here](https://github.com/scrthq/PSGSuite/blob/master/CHANGELOG.md)

#### 2.23.0

* [Issue #152](https://github.com/scrthq/PSGSuite/issues/152)
* Added full coverage of `Gmail.Settings.SendAs` resource (where signatures are managed with the newer Gmail API):
* Added: `Get-GSGmailSendAsAlias`
* Added: `Update-GSGmailSendAsAlias`
* Added: `Get-GSGmailSignature` (aliased to `Get-GSGmailSendAsAlias`)
* Added: `Update-GSGmailSignature` (aliased to `Update-GSGmailSendAsAlias` with some additional convenience parameters)
* Added: `Get-GSGmailSendAsSettings` (aliased to `Get-GSGmailSendAsAlias`)
* Added: `Update-GSGmailSendAsSettings` (aliased to `Update-GSGmailSendAsAlias`)
* Added: `Remove-GSGmailSendAsAlias`
* Added: `New-GSGmailSendAsAlias`
* Added: `Send-GSGmailSendAsConfirmation`
#### 2.23.1

**This update changes the output of `Get-GSUsageReport` -- please review the output changes before updating if you have scripts that use that function!!**

* Fixed: `Get-GSUsageReport` wasn't displaying critical report information (such as the Entity info) due to Select-Object being hardcoded. Function has been updated to parse the resulting Parameters and Entity info out to the top-level object.
* Added: `Flat` switch to specify that the parsed properties match what GAM returns, i.e. `'gmail:num_outbound_unencrypted_emails' = 6`. Normal behavior would be to parse that into an ordered dictionary, i.e. `gmail['num_outbound_unencrypted_emails'] = 6`, so that only `gmail` is seen from the top level object and all relevant report data is captured in the underlying dictionary.
* Added: `Raw` switch to allow the raw UsageReportsValue to be returned instead of parsing it out.

0 comments on commit 929d347

Please sign in to comment.