jjquintero245
jjquintero245

Reputation: 49

Use Variable as Value

I have a question because I can't use Variable as values on hashtable on PowerShell.

I have two variables.

  1. To get the UserPrincipalName from AD and add to $alias

    $alias = (Get-ADUser -Server "Add Server" -filter * -SearchBase "Add OU").userprincipalname
    
  2. Then with the $alias I need to get the mailbox quota with Get-Mailbox and Get-MailboxStatistics in MB and make a percentage

    $mailbox = foreach ($user in $alias) {
        ($user | Get-MailboxStatistics |
            Select-Object @{name="TotalItemSize (MB)"; expression={
                [Math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)
            }})."TotalItemSize (MB)"
    }
    
    • Using this max. quota (100 GB on MB)

      $maxquota = 102400
      
    • To get the %

      $totalsize = foreach ($size in $mailbox) { 
          (($size * 100) / $maxquota)
      }
      

    Example of MailboxSize

    PS> $totalsize
    2.43220703125
    14.3363671875
    8.875205078125
    5.032177734375
    15.548349609375
    0.0112109375
    
  3. Then I want to make a hash table with $alias and $totalsize

    $test = foreach ($name in $alias) {
        foreach($total in $totalsize){}
        @{$name = $total}
    }
    
  4. The result should be

    Key - Name in $alias
    Value - Percentage in $total

    For example:

    David  - 4.50
    Juan   - 15.00
    Moises - 50
    

But when I test the hashtable I'm just receiving each name with all the values or all the names only with the first value

How can I get all the values like a table?

Upvotes: 0

Views: 917

Answers (2)

Shawn Esterman
Shawn Esterman

Reputation: 2342

You are setting $Test as an array of hashtables. Assign your $Hashtable and then add values to it with the Add method:

Param (
    $maxquota = 102400
)

$HashTable = @{}

$alias = (Get-ADUser -Server "Add Server" -filter * -SearchBase "Add OU").userprincipalname
$mailbox = Foreach ($user in $alias) {($user | Get-MailboxStatistics |  Select-Object @{name="TotalItemSize (MB)"; expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}})."TotalItemSize (MB)"}
$totalsize = foreach ($size in $mailbox) { 
    (($size * 100) / $maxquota)
}

$test = Foreach($name in $alias){
    $HashTable.Add($Name,$totalsize)
}

Upvotes: 3

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

The code in step 3 would not create a hashtable, but an array of hashtables. Also, it wouldn't know which mailbox size belongs to which user, because that information is lost after step 2.

Something like this should do what you want:

$test = @{}   # create new empty hashtable

foreach ($user in $alias) {
    $size = Get-MailboxStatistics -Identity $user |
            Select-Object @{n='TotalItemSize (MB)';e={...}} |
            Select-Object -Expand 'TotalItemSize (MB)'

    $test[$user] = ($size * 100) / $maxquota
}

Upvotes: 2

Related Questions