Powershell calculated properties for multiple values

powershell

I am making an Outlook inventory powershell script.. I want to retrieve:

  • Account DisplayName
  • Account email
  • Account type (POP3, IAMP or Exchange)
  • Exchange mode (Online, Cached)

Code:

Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$namespace.Accounts |
   Select-Object DisplayName, SmtpAddress, UserName, AccountType, ExchangeConnectionMode |
   Sort-Object  -Property SmtpAddress | Format-Table

This do the job, but it would really cool if I could use calculated properties to show AccountType and ExchangeConnectionMode values as custom friendly name, instead of numeric value. For example:

AccountType
0 = Exchange
1 = IMAP
2 = POP3
Else = numeric value (real value)

ExchangeConnectionMode
700 = Cached
800 = Online
Else = N/A (or whatever similar text, for non exchange accounts)

I already used calculated properties before but only with math calcs and single value (without multiple values). I guess "foreach" or "if" will be needed… Can't figure it out how 🙁

Any clue?

Best Answer

Read and follow Using Calculated Properties and Working with Hash Tables.

Using Outlook's OlExchangeConnectionMode Enumeration and OlAccountType Enumeration, the following code snippet should do the job:

$AccntTypes = @{ '0' = 'Exchange'; 
                 '1' = 'IMAP';
                 '2' = 'POP3';
                 '3' = 'HTTP'; 
                 '4' = 'EAS';
                 '5' = 'unknown'}
$ExchConnModes = @{'0' = 'Exchange'; 
                   '800' = 'Online';
                   '700' = 'CachedConnectedFull';
                   '600' = 'CachedConnectedDrizzle'; 
  # (incomplete; please update from `OlExchangeConnectionMode` Enumeration)
                    }

Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$namespace.Accounts |
   Select-Object DisplayName, SmtpAddress, UserName, 
    @{Name="AccountType";
      Expression={$AccntTypes[[string]$_.AccountType]}} , 
    @{Name="ExchangeConnectionMode";
      Expression={$ExchConnModes[[string]$_.ExchangeConnectionMode]}} |
   Sort-Object  -Property SmtpAddress | Format-Table