Home > Net >  Using powershell for a google report
Using powershell for a google report

Time:02-12

I'm new to using powershell, I'm trying to use the PSGSuite module to get a report for all users for the past 30 days. What I've got so far is the following


$REQUESTEDDATE = Read-Host -Prompt 'Enter the month for the desired report month in numerical form. e.g Jan 1 Feb 2 Mar 3'
$REPORTDATE = (Get-Date -Month ($REQUESTEDDATE-(-1)) -Hour 0 -Minute 0 -Second 0)
$MonthAgo = $REPORTDATE.AddMonths(-1)
$FIRSTDAYOFMONTH=GET-DATE $MonthAgo -Day 1
$LASTDAYOFMONTH=GET-DATE $FIRSTDAYOFMONTH.AddMonths(1).AddSeconds(-1)
$Times = $FIRSTDAYOFMONTH..$LASTDAYOFMONTH.day | Foreach-Object {
$currentdate = Get-Date -Day $_ -Month $LASTDAYOFMONTH.Month -Year $LASTDAYOFMONTH.Year 
$GMAIL = Get-GSUsageReport -Date $currentdate -UserKey xxx -flat

}

This is now throwing a "Invalid cast from 'DateTime' to 'Int32' error. There's probably a much easier way to do this, but I'm more of the hardware/networking side thrown onto this while the dev team is working on different projects, so any help is appreciated.

CodePudding user response:

First thing is that I hate all those CAPITALS in your code, so if you don't mind I have changed that.

Next, you can simply run through the dates for as long as the running date is less than the final date (day 1 of the requested date) using a while loop:

$requestedDate = Read-Host -Prompt 'Enter the month for the desired report month in numerical form. e.g Jan 1 Feb 2 Mar 3'
$reportDate    = (Get-Date -Month $requestedDate -Day 1).Date   # end date set at midnight on day 1
$runningDate   = $reportDate.AddMonths(-1)                      # start at day 1, one month ago

$result = while ($runningDate -lt $reportDate) {
    # perform your command and capture the output in variable $result
    Get-GSUsageReport -Date $runningDate -UserKey xxx -flat
    # increment the running date
    $runningDate = $runningDate.AddDays(1)  
}

# show the result
$result
  • Related