Search code examples
powershellexchange-server

Iterate thru SubjectOrBodyContainsWords of an Exchange transport rule


I want to create a PowerShell script to extract a text file with a list of words specified in an Exchange Server 2016 transport rule. I'm stuck with handling the obtained list.

To get the list, I do this within Exchange Management Shell:

$SubjectOrBodyContainsWords = Get-TransportRule "My rule name" | Select-Object -Property SubjectOrBodyContainsWords

I verify that the list is correct using this:

$FormatEnumerationLimit = 10000
$SubjectOrBodyContainsWords | Format-Table -HideTableHeaders | Out-String -width 10000

The output looks like (Just an example, actual list is much much bigger):

{unsubscribe, mailing, blabla}

Now I want to iterate the list to do something with each item. I tried something like this (Just a simple example):

$I = 10;
foreach ($A in $SubjectOrBodyContainsWords)
{
    $I++;
    $I; 
    $A;
}

The problem is that it doesn't loop all the items. It looks like there is only one item.

What am I doing wrong?

Thanks.


Solution

  • I don't have access to my Exchange server at the moment but just try the following suggestions. Just ask for and expand the property.

    (Get-TransportRule "My rule name").SubjectOrBodyContainsWords
    
    # Or
    
    Get-TransportRule "My rule name" | 
    Select-Object -ExpandProperty SubjectOrBodyContainsWords
    

    Since this returns an array, you need to expand that listing.

    Or you can do this to turn into a list to work with...

    "{unsubscribe, mailing, blabla}" -replace '\s|{|}' -split ',' |  foreach {
        # Code to do something with each item
        $PSItem }
    
    # Results
    unsubscribe
    mailing
    blabla
    

    ... and

    Potential duplicate of this use case

    Exchange Shell - SubjectOrBodyContainsWords