r/PowerShell • • Jun 23 '18

Create an Interactive HTML Report for Office 365 with PowerShell

http://thelazyadministrator.com/2018/06/22/create-an-interactive-html-report-for-office-365-with-powershell/
141 Upvotes

9 comments sorted by

6

u/nothingpersonalbro Jun 24 '18 edited Jun 24 '18

Minor critique, I would opt to change this method of creating objects:

$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name Name -Value $CompanyName
$obj | Add-Member -MemberType NoteProperty -Name "Techcnical Email" -Value $TechEmail
$obj | Add-Member -MemberType NoteProperty -Name "Self Service Password Reset" -value $SelfServePassword
$obj | Add-Member -MemberType NoteProperty -Name "Directory Sync" -value $DirSync
$obj | Add-Member -MemberType NoteProperty -Name "Password Sync" -value $PasswordSync
$obj | Add-Member -MemberType NoteProperty -Name "Last Directory Sync" -value $LastDirSync
$obj | Add-Member -MemberType NoteProperty -Name "Last Password Sync" -value $LastPasswordSync

To this:

$obj = [PSCustomObject]@{
    'Name'                        = $CompanyName
    'Techcnical Email'            = $TechEmail
    'Self Service Password Reset' = $SelfServePassword
    'Directory Sync'              = $DirSync
    'Password Sync'               = $PasswordSync
    'Last Directory Sync'         = $LastDirSync
    'Last Password Sync'          = $LastPasswordSync
}

There is also a lot of $Array += $obj going on. If speed is an issue, it might be worth looking into changing them from fixed arrays to a Generic.List.

Generic.List example:

# Create Generic.List
[System.Collections.Generic.List[PSCustomObject]]$Array = @()
# Or
$Array = New-Object -TypeName System.Collections.Generic.List[PSCustomObject]
# Add item
$Array.Add($obj)

Compare the speed of this:

# Fixed Array
(Measure-Command -Expression {
    $Array = @()
    foreach ($i in (1..10000)) {
        $Array += $i
    }
}).TotalMilliseconds

Versus this:

# Generic.List
(Measure-Command -Expression {
    [System.Collections.Generic.List[PSCustomObject]]$Array = @()
    foreach ($i in (1..10000)) {
        $Array.Add($i)
    }
}).TotalMilliseconds

2123.019 versus 34.581 milliseconds, pretty big difference. If you really want to have fun, watch how long it takes the first one to do 100,000 items. The Generic.List can shred through it in no time :)

3

u/TheLazyAdministrator Jun 24 '18

This is fantastic thanks for showing me this. I’m always looking for ways to improve!

2

u/TheLazyAdministrator Jun 25 '18

Hi I just wanted to give you an update - I will look into the PSCustom object items, but /u/jmn_lab was able to implement Generic.List

2

u/nothingpersonalbro Jun 25 '18

Cool! Has the creation time changed any? The biggest one may be $rpt at the end. You should be able to encapsulate all the commands in parentheses to add them to the Generic.List like this https://pastebin.com/7K2Dg6Fg

2

u/TheLazyAdministrator Jun 25 '18

Thanks I made that change as well I also removed getting last mailboxlogontimestamp as that was one of the slowest lookups in the script (pass the user in get-mailbox | get-mailboxstatistics ) and added boolean value of resetpasswordnextlogon

all these improvements has made runtime go from 1min 45sec to 1 minute flat.

thanks for all the help

4

u/ever0n Jun 23 '18

This is a really great report, thanks for sharing!

2

u/Lagrik Jun 23 '18

Awesome stuff!

2

u/pineapple_goat Jun 23 '18

This is effing brilliant.

1

u/errolfinn Jun 23 '18

Nice one thanks