New-MSOLReport.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function New-MSOLReport
{
  <#
    .SYNOPSIS
    Creates a CSV-file with user data from Office 365
    .DESCRIPTION
    This function creates a CSV-file with user data fetched from Office 365, such as licensing status, mailbox size, etc.
    .EXAMPLE
    New-MSOLReport -outfile c:\temp\somefile.csv
    .EXAMPLE
    New-MSOLReport -outfile c:\temp\somefile.csv -Verbose
  #>

  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory=$true, Position=0)]
    [string]
    $outfile
  )
  $outpath = split-path $outfile
  if (!(test-path $outpath)) {new-item $outpath -ItemType Directory}
  if (!(get-module msonline)) {open-msolconnection}
  Write-Progress -Id 1 -Activity "Generating MSOL Report" -Status "Starting Inital Query" -PercentComplete 0
  $arrResults = @()
  $starttime = (get-date)
  $users = get-msoluser
  $total = ($users).count
  write-verbose "Getting $total mailboxes"
  Write-Progress -Id 1 -Status "Getting $total mailboxes" -PercentComplete 10 -Activity "Generating MSOL Report"
  $mbx = get-mailbox
  Write-Verbose "Getting mailbox statistics for $total mailboxes. This might take some time."
  Write-Progress -Id 1 -Status "Getting Statistics for $total mailboxes. This might take some time." -PercentComplete 20 -Activity "Generating MSOL Report"
  $statistics = ($mbx | get-mailboxstatistics)
  write-verbose "Starting processing of $total individual mailboxes"
  Write-Progress -Id 1 -Status "Processing individual users" -PercentComplete 60 -Activity "Generating MSOL Report"
  $current = 0
  forEach ( $user in $users ) {
    $current++
    $status = $user.displayname
    Write-Progress -Activity "Processing $total mailboxes" -Status "Processing mailbox $status" -PercentComplete ($current / $total*100) -ParentId 1
    Write-Verbose "Gathering statistics for $status"
    $stats = ($statistics | Where-Object {$_.Displayname -eq $user.DisplayName})
    $properties = @{'Name'=$user.DisplayName;
      'EmailAddress'=$user.UserPrincipalName;
      'Title'=$user.Title;
      'Licensed'=$user.IsLicensed;
      'MobilePhone'=$user.MobilePhone;
      'AlternateEmail'=$user.AlternateEmailAddress;
      'AlternatePhone'=$user.AlternateMobilePhones;
      'LastLogon'=$stats.LastLogonTime;
      'MailboxSize'=$stats.TotalItemSize;
    'DeletedItemSize'=$stats.TotalDeletedItemSize}
    $objResults = New-Object -TypeName PSObject -Property $properties
    $arrResults += $objResults
  }
  Write-Progress -Id 1 -Status "Writing $outfile" -PercentComplete 95 -Activity "Generating MSOL Report"
  $arrResults | Export-CSV -NoTypeInformation -Path $outfile
  $stoptime = (get-date)
  $time = (new-timespan -start $starttime -end $stoptime)
  write-output "Processed $total users in $time, exported to CSV file $outfile."
}