Powershell Office 365: Building a Hash Table and a Custom PSObject to Get-UserInfo
Here is a script that will quickly tell you some mailbox information and some mailbox statistics for an Exchange Online such as Office 365 mailbox. This will also work with Exchange Shell on Exchange 2013 on Premises.
The data is collated from 2 different cmdlets, Get-MsolUser and Get-MailboxStatistics. The data is then used to create an advanced function called Get-UserInfo.
This post serves as a demonstration in building a hash table and a custom PSObject, one of the most important aspects of PowerShell scripting. It also serves to demonstrate the principal of gathering output from various cmdlets and bringing them together as one.
It has been assumed that you have installed the PowerShell Module called MSOnline and authenticated to Office 365 via PowerShell in the usual way.
If you were to "Dot Source" the script using PowerShell.exe then you would run it with the following :
Get-UserInfo -UPN user@mail.com -Verbose -ErrorLog
This script example will only support one UserPrincipalName input although can be easily changed to accept multiple, CSV input and accross the pipeline if required. This is outwith the scope of this post.
Would you like to buy Alan a coffee?
Visit the AlanPs1 Ko-fi page
Here is the script:
function Get-UserInfo {
<#
.Synopsis
Making use of both Get-MsolUser and Get-MailboxStatistics to create Get-UserInfo
.DESCRIPTION
Get-UserInfo combines properties from both Get-MsolUser and Get-MailboxStatistics to create a custom PS Object.
This Custom PS Object provides information on mailbox, the user and mailbox statistics too.
.EXAMPLE
PS C:\> Get-UserInfo -UPN user@mail.com
Main Email : user@mail.com
Real Name : Test User
Location :
Has License : True
Has Archive : False
Has Clutter : False
No of Proxy Addresses : 3
.EXAMPLE
PS C:\> Get-UserInfo -UPN user@mail.com -Verbose -ErrorLog
VERBOSE: Error logging turned on
Main Email : user@mail.com
Real Name : Test User
Location :
Has License : True
No of Proxy Addresses : 6
Has Archive : False
Has Clutter : False
.INPUTS
String
.OUTPUTS
Custom PS Object
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
[String]$UPN,
[Switch]$ErrorLog,
[String]$LogFile = 'c:\temp\errorlog.txt'
)
Begin {
If($errorLog){
Write-Verbose 'Error logging turned on'
} Else {
Write-Verbose 'Error logging turned off'
}
Foreach($C in $ComputerName){
Write-Verbose "Computer: $Computer"
}
}
Process {
try {
$User = Get-MsolUser -UserPrincipalName $UPN -ErrorAction Stop
$Stats = Get-MailboxStatistics -Identity $UPN -ErrorAction Stop
$props = [ordered]@{
'Main Email'=$User.UserPrincipalName
'Real Name'=$User.DisplayName
'Location'=$User.City
'Has License'=$User.isLicensed
'No of Proxy Addresses'=($User.ProxyAddresses).Count
'Has Archive'=$Stats.IsArchiveMailbox
'Has Clutter'=$Stats.IsClutterEnabled
}
$Obj = New-Object -TypeName PSObject -Property $props
Write-Output $Obj
} catch {
}
}
End {}
}
Alan
Tweet