public/Get-O365AllReports.ps1

<#
.Synopsis
This PowerShell cmdlet will generate all available reports from the command 'Get-O365Reports' and save them to a single ZIP archive.
  
.Description
This will generate all available reports from the command 'Get-O365Reports' to the location you specify and save it in CSV or HTML format.
You can opt to create both report file types as well by setting the '-Formatting' parameter to ALL. Once complete, you will see a display
of the status for the reports being generated, as well as a text output in green or yellow as reports are started or completed. Once all
reports are generated, they are compressed in a ZIP archive file in the location you specify (or the default location if you choose to not
set the'-SavePath' parameter.
  
.Example
Get-O365AllReports -SavePath "C:\Reports" -Formatting CSV
 
Description
-----------
This example with generate all reports in CSV format only. It will check if you are connected to Office 365 and then run the necessary commands
to run all available reports in the function 'Get-O365Reports'. All CSV files generated will be compressed into a single ZIP archive in the in
the C:\Reports folder as specified in the example above.
 
.Example
Get-O365AllReports -SavePath "C:\Reports" -Formatting HTML
 
Description
-----------
This example with generate all reports in HTML format only. It will check if you are connected to Office 365 and then run the necessary commands
to run all available reports in the function 'Get-O365Reports'. All CSV files generated will be compressed into a single ZIP archive in the in
the C:\Reports folder as specified in the example above.
 
.Example
Get-O365AllReports -Formatting All
 
Description
-----------
This example will generate all reports in both CSV and HTML format. It will check if you are connected to Office 365 and then run the necessary commands
to run all available reports in the function 'Get-O365Reports'. All files generated will be compressed into a single ZIP archive in the in
the C:\Reports folder since no default location was set. The default save location is $env:HOMEDRIVE\Reports which is typically C:\ on most systems.
 
 
#>


Function Get-O365AllReports{
    [cmdletbinding()]
    Param (
    [Parameter(Position=0,mandatory=$true)] [string]$Formatting,
    [Parameter(Position=1,mandatory=$false)] [string]$SavePath,
    [Parameter(Position=2,mandatory=$false)] [string]$ReportColor
       )
    # End of Parameters
    Process {
            $datestring = (Get-Date).ToString("s").Replace(":","-")
            $modpath = (Get-Module -ListAvailable O365Tools).path.ToString()
            $CSSLoc = $modpath -replace ("O365tools.psd1","style\")
            switch ($ReportColor)
              {
              Green    {$stylesheet = "$CSSLoc"+"greenTable.css"}
              Blue     {$stylesheet = "$CSSLoc"+"blueTable.css"}
              Red      {$stylesheet = "$CSSLoc"+"redTable.css"}
              default  {$stylesheet = "$CSSLoc"+"bluetable.css"}
              }
            
            $css = get-content $stylesheet 

            #If paramater -SavePath isn't passed, it will be set to the the home drive in a folder named Reports (Typically this will be C:\Reports)
            IF ($SavePath -eq "") { $SavePath = "$env:HOMEDRIVE"+"\Reports" }

            # Hard coding the report names availalbe with variable $AllReports
            $AllReports = "DBMembers","MailboxSize","ArchiveSize","MailboxHold","LastLogin","ActiveSync","MailboxInfo","DBGroups","Forwarding","UserLicense"
            $count = 0
            $count2 = 0

            foreach($i in $AllReports) 
                {
                    $count +=1

                    switch ($Formatting)
                    {
                    CSV           {     
                                        Write-Progress -activity "Generating Reports . . ." -status "Working on report group: $count of $($AllReports.count)" -percentComplete (($count / $AllReports.Count)  * 100)
                                        write-host "$i is the current report"

                                        If ($i -eq "DBMembers")
                                            {                                            
                                            $Groupname = Get-DistributionGroup -Filter {name -like "*"}

                                            foreach ($group in $Groupname)
                                                {
                                                Write-Progress -activity "Generating Reports . . ." -status "Scanned: $count2 of $($groupname.Count)" -percentComplete (($count2 / $groupname.Count)  * 100)
                                                Get-O365Reports -ReportName DBMembers -Formatting csv -DBAlias $group -SavePath $SavePath
                                                $count2 +=1
                                                }
                                            }

                                        Else
                                            {
                                            Get-O365Reports -ReportName $i -Formatting csv -SavePath $SavePath
                                            }
                                    }
                    
                    HTML          {
                                        Write-Progress -activity "Generating Reports . . ." -status "Working on report group: $count of $($AllReports.Count)" -percentComplete (($count / $AllReports.Count)  * 100)
                                        
                                        If ($i -eq "DBMembers")
                                            {                                            
                                            $Groupname = Get-DistributionGroup -Filter {name -like "*"}

                                            foreach ($group in $Groupname)
                                                {
                                                Write-Progress -activity "Generating Reports . . ." -status "Scanned: $count2 of $($groupname.Count)" -percentComplete (($count2 / $groupname.Count)  * 100)
                                                Get-O365Reports -ReportName DBMembers -Formatting html -DBAlias $group -SavePath $SavePath -ReportColor $ReportColor
                                                $count2 +=1
                                                }
                                            }

                                        Else
                                            {
                                            Get-O365Reports -ReportName $i -Formatting html -SavePath $SavePath
                                            }
                                    }

                    ALL          {     
                                        Write-Progress -activity "Generating Reports . . ." -status "Working on report group: $count of $($AllReports.count)" -percentComplete (($count / $AllReports.Count)  * 100)
                                        write-host "$i report creation in progress" -ForegroundColor Yellow

                                        If ($i -eq "DBMembers")
                                            {                                            
                                            $Groupname = Get-DistributionGroup -Filter {name -like "*"}

                                            foreach ($group in $Groupname)
                                                {
                                                Write-Progress -activity "Generating Reports . . ." -status "Scanned: $count2 of $($groupname.Count)" -percentComplete (($count2 / $groupname.Count)  * 100)
                                                Get-O365Reports -ReportName DBMembers -Formatting csv -DBAlias $group -SavePath $SavePath
                                                
                                                $CSVloc = "$SavePath"+"\"+"$i"+"_Reports"+"\CSV"
                                                $HTMLloc = "$SavePath"+"\"+"$i"+"_Reports"+"\HTML"

                                                # Subfolder for filetype getting created if it doesn't already exist
                                                IF (-NOT (Test-Path $CSVloc -PathType 'Container')) { New-Item $CSVloc -ItemType Directory -Force }
                                                Else{
                                                    IF (-NOT (Test-Path $HTMLloc -PathType 'Container')) { New-Item $HTMLloc -ItemType Directory -Force }
                                                    }

                                                $file = gci $CSVloc | sort LastWriteTime | select -last 1
                                                $filename = "$CSVloc"+"\"+"$file"
                                                $file = "$HTMLloc"+"\"+"$file"                                                                                               
                                                $fileHTML = $file.trim("csv")
                                                $fileHTML = "$fileHTML"+"html"
                                                Import-Csv $filename | ConvertTo-Html -Head $css -Body "<h1>$i Report</h1>`n<h5>Generated on $(Get-Date)</h5>" | Out-File -FilePath $fileHTML
                                                write-host "$i report complete! File saved to: $fileHTML" -ForegroundColor Green
                                                $count2 +=1
                                                }
                                            }

                                        Else
                                            {
                                            Get-O365Reports -ReportName $i -Formatting csv -SavePath $SavePath
                                            
                                            $CSVloc = "$SavePath"+"\"+"$i"+"_Reports"+"\CSV"
                                            $HTMLloc = "$SavePath"+"\"+"$i"+"_Reports"+"\HTML"

                                            # Subfolder for filetype getting created if it doesn't already exist (CSV and HTML Folder)
                                            IF (-NOT (Test-Path $CSVloc -PathType 'Container')) { New-Item $CSVloc -ItemType Directory -Force }
                                            Else{ IF (-NOT (Test-Path $HTMLloc -PathType 'Container')) { New-Item $HTMLloc -ItemType Directory -Force } }
                                                                                                                                       
                                            $file = gci $CSVloc | sort LastWriteTime | select -last 1
                                            $filename = "$CSVloc"+"\"+"$file"
                                            $file = "$HTMLloc"+"\"+"$file"
                                            $fileHTML = $file.trim("csv")
                                            $fileHTML = "$fileHTML"+"html"
                                            Import-Csv $filename | ConvertTo-Html -Head $css -Body "<h1>$i Report</h1>`n<h5>Generated on $(Get-Date)</h5>" | Out-File -FilePath $fileHTML
                                            write-host "$i report complete! File saved to: $fileHTML" -ForegroundColor Green
                                            }
                                    }
                    }
                }               
                $source = Get-ChildItem -Filter "*_Reports" -Path $SavePath
                $source = Get-ChildItem -Filter "*_Reports" -Path $SavePath -Directory
                $ZipFile = "$SavePath"+"\"+"$Formatting"+"Reports_$datestring.zip"
                $source | Compress-Archive -DestinationPath $ZipFile -update
                $source | Remove-Item -Force -Recurse
                Write-Host "******************************" -ForegroundColor Green
                Write-Host "All Reports Complete. ZIP file containing all reports located: $ZipFile" -ForegroundColor Green
                Write-Host "******************************" -ForegroundColor Green
                Invoke-Item $ZipFile
            } #End Process
            }