Trying to use a variable inside of a variable... failing at it.

Hello Everyone,
I’m crating a little script to always true up 3 users Blocked Senders Lists so that they are always the same. The intent of the script is to look at all three users, see who has the largest Blocked Senders List, and apply that list to the other two users. After it has been applied, check again to see if all the numbers match.

The issue I run into is when I try to use the variable that is “Biggest_Entry_Count”. It has variable of the largest variable inside, but when I use that to apply the entries to the other users, it fails.

Here is the code that I have written so far:

$person1 = 'person1@domain.com'
$person2 = 'person2@domain.com'
$person3 = 'person3@domain.com'

$person1_Entries = (Get-MailboxJunkEmailConfiguration -Identity $person1 | Select-Object -expandproperty BlockedSendersAndDomains)
$person2_Entries = (Get-MailboxJunkEmailConfiguration -Identity $person2 | Select-Object -expandproperty BlockedSendersAndDomains)
$person3_Entries = (Get-MailboxJunkEmailConfiguration -Identity $person3 | Select-Object -expandproperty BlockedSendersAndDomains)

$person1_Entries_Count = $person1_Entries.count
$person2_Entries_Count = $person2_Entries.count
$person3_Entries_Count = $person3_Entries.count

Write-Host "$person1 -- $person1_Entries_Count"
Write-Host "$person2 -- $person2_Entries_Count"
Write-Host "$person3 -- $person3_Entries_Count"

$Biggest_Entry_Count = '${0}' -f (Get-Variable -Name person1_Entries_count, person2_Entries_count, person3_Entries_count | Sort-Object -Property Value | Select-Object -Last 1 -ExpandProperty Name)
$Biggest_Entry = '${0}' -f ((Get-Variable -Name person1_Entries_count, person2_Entries_count, person3_Entries_count | Sort-Object -Property Value | Select-Object -Last 1 -ExpandProperty Name)).Trim("_Count")

Write-Host "Biggest Entry Variable: " $Biggest_Entry
Write-Host "Biggest Entry Count Variable: " $Biggest_Entry_Count


if ($Biggest_Entry_Count -gt $person1_Entries_Count) {
    Write-Host "Add Biggest Entry to person1"
    Set-MailboxJunkEmailConfiguration $person1 -BlockedSendersAndDomains $Biggest_Entry
}

if ($Biggest_Entry_Count -gt $person2_Entries_Countt) {
    Write-Host "Add Biggest Entry to person2"
    Set-MailboxJunkEmailConfiguration $person2 -BlockedSendersAndDomains $Biggest_Entry
}

if ($Biggest_Entry_Count -gt $person3_Entries_Count) {
    Write-Host "Add Biggest Entry to person3"
    Set-MailboxJunkEmailConfiguration $person3 -BlockedSendersAndDomains $Biggest_Entry
}

$person1_Entries = (Get-MailboxJunkEmailConfiguration -Identity $person1 | Select-Object -expandproperty BlockedSendersAndDomains)
$person2_Entries = (Get-MailboxJunkEmailConfiguration -Identity $person2 | Select-Object -expandproperty BlockedSendersAndDomains)
$person3_Entries = (Get-MailboxJunkEmailConfiguration -Identity $person3 | Select-Object -expandproperty BlockedSendersAndDomains)

$person1_Entries_Count = $person1_Entries.count
$person2_Entries_Count = $person2_Entries.count
$person3_Entries_Count = $person3_Entries.count

Write-Host "$person1 -- $person1_Entries_Count"
Write-Host "$person2 -- $person2_Entries_Count"
Write-Host "$person3 -- $person3_Entries_Count"

Here is the output I currently get:

person1@domain.com -- 6523
person2@domain.com -- 6493
person3@domain.com -- 6552
Biggest Entry Variable:  $person3_Entries
Biggest Entry Count Variable:  $person3_Entries_Count

Add Biggest Entry to person2
Property validation failed: BlockedSendersAndDomains
    + CategoryInfo          : NotSpecified: (:) [Set-MailboxJunkEmailConfiguration], PropertyValidationException
    + FullyQualifiedErrorId : [Server=MN2PR04MB6317,RequestId=f9526398-bcd1-4c5a-a114-2ac7d2536b91,TimeStamp=10/27/2020 4:28:18 PM] [FailureCategory=Cmdle
   t-PropertyValidationException] 9FB58158,Microsoft.Exchange.Management.StoreTasks.SetMailboxJunkEmailConfiguration
    + PSComputerName        : outlook.office365.com

person1@domain.com -- 6523
person2@domain.com -- 6493
person3@domain.com -- 6552

Thanks for any help you can provide!

  • Rob

Here is an different approach to try:

$persons = 'person1@domain.com', 'person2@domain.com', 'person3@domain.com'

$configs = foreach ( $person in $persons ) {
    Get-MailboxJunkEmailConfiguration -Identity $person | 
    Select *,
           @{Name='EntryCount';Expression={@($_.BlockedSendersAndDomains).Count}}
}
 
$Biggest_Entry_Count = $configs | 
                       Sort-Object -Property EntryCount -Descending | 
                       Select-Object -First 1


foreach ( $person in ($configs | Where-Object -FilterScript {$_.Identity -ne $Biggest_Entry_Count.Identity}) ) {
    Set-MailboxJunkEmailConfiguration -Identity $person -BlockedSendersAndDomains $Biggest_Entry.BlockedSendersAdnDomains -WhatIf
}

Do not have access to Exchange to test or see what is returned from the GET, but you just need to filter on the appropriate property and to perform the SET