Senior Systems Engineer
Senior Systems Engineer

Reputation: 1139

Calling hashtable inside calculated property

I want to fix the one line which maps the values of a number to a string in this Hashtable.

$mailboxType = @{
    "0"               = "UserMailbox"
    "1073741824"   = "Shared Mailbox"
    "6"               = "MailUser"
    "7"               = "Room"
    "8"               = "Equipment"
    "-2147483642"  = "RemoteUserMailbox"
    "-1073741818 " = "RemoteUserMailbox-Hybrid Delegate"
}

Get-MsolUser -All |
Where-Object { ($_.blockcredential -eq $true) -and ($_.isLicensed -eq $true) } |
Select-Object DisplayName,
              UserPrincipalName,
              @{ n = 'Mailbox Type'; e = { $mailboxType[$_.CloudExchangeRecipientDisplayType].GetEnumerator().Value.ToString() } },
              isLicensed,
              BlockCredential,
              Licenses,
              LicenseAssignmentDetails,
              WhenCreated | Format-Table -AutoSize

The script is working fine it is just the Hashtable calling is not working.

The problem is the column Mailbox Type is always empty. But when I just use the default CloudExchangeRecipientDisplayType, the column value is a meaningless decimal number like in https://cloudrun.co.uk/office365/recipi ... directory/.

Upvotes: 0

Views: 124

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25001

I am not so sure that your hash table has the correct values that correspond to the correct recipient type, but to syntactically do what you want to do, you need to match your key types in your $mailboxType declaration and your lookup:

$mailboxType = @{
    "0"               = "UserMailbox"
    "1073741824"   = "Shared Mailbox"
    "6"               = "MailUser"
    "7"               = "Room"
    "8"               = "Equipment"
    "-2147483642"  = "RemoteUserMailbox"
    "-1073741818 " = "RemoteUserMailbox-Hybrid Delegate"
}

Get-MsolUser -All |
    Where-Object { ($_.blockcredential -eq $true) -and ($_.isLicensed -eq $true) } |
        Select-Object DisplayName,
              UserPrincipalName,
              @{ n = 'Mailbox Type'; e = { $mailboxType[[string]$_.CloudExchangeRecipientDisplayType]}}

Notice how your keys are defined as strings by use of quoting "". So when you do the lookup, you need to use string typed keys. The other option is to not use quotes around your numbers at all.

$mailboxType = @{
    0            = "UserMailbox"
    1073741824   = "Shared Mailbox"
    6            = "MailUser"
    7            = "Room"
    8            = "Equipment"
    -2147483642  = "RemoteUserMailbox"
    -1073741818 = "RemoteUserMailbox-Hybrid Delegate"
}

Get-MsolUser -All |
    Where-Object { ($_.blockcredential -eq $true) -and ($_.isLicensed -eq $true) } |
        Select-Object DisplayName,
              UserPrincipalName,
              @{ n = 'Mailbox Type'; e = { $mailboxType[$_.CloudExchangeRecipientDisplayType]}}

Upvotes: 1

Related Questions