Add Multiple values to existing Property

This topic contains 9 replies, has 4 voices, and was last updated by Profile photo of Joel Joel 1 year, 11 months ago.

  • Author
    Posts
  • #13367
    Profile photo of ceem88
    ceem88
    Participant

    Hi

    I have a Property that I would like to modifiy and keep the existing records:
    Get-Transportrule test | fl RecipientAddressContainsWords
    RecipientAddressContainsWords : {test, test2, test3}

    How can I add new multiple values without deleting the old ones?

    thanks for your help

  • #13377
    Profile photo of Don Jones
    Don Jones
    Keymaster

    It looks as if RecipientAddressContainsWords is a collection.

    $collection = Get-Transportrule test | select -expand RecipientAddressContainsWords
    $collection += "test4"

    That will not write the change back to wherever it came from, but it will add a fourth string to the collection.

  • #13397
    Profile photo of Don Jones
    Don Jones
    Keymaster

    I think I'm probably still not understanding.


    $collection = Get-Transportrule test | select -expand RecipientAddressContainsWords

    $collection now contains the current rules, yes?


    $collection += 'test.com','test2.com'

    Now you have added two items to the rules.


    set-transportrule test -RecipientAddressContainsWords $collection

    Please understand that I don't know what Set-TransportRule is doing. I'm also entirely unsure what you are trying to achieve. I know PowerShell, but I do not know Exchange very well. If you're not understanding what the Exchange command does, or what it needs, please post a new question and in the subject be sure to specify that you are asking about Exchange. That makes it easier for an appropriate person to look at your question.

    But I think the problem is your understanding of the command. The -RecipientAddressContainsWords can accept a collection of multiple values. What your command is doing is giving it one value at a time. So you are setting the rule to contain "test.com," then you are setting it to only contain "test2.com," and so on. Running the command multiple times is not cumulative. Each time you run it, it replaces the last time. To give it multiple values, you run the command ONCE (not in a ForEach), and give it ALL the values, as a collection of strings.

    I hope that helps.

    • #13398
      Profile photo of ceem88
      ceem88
      Participant

      Sorry about the confusion. I was trying to modify a Transportrule based an all Domains with the Type of Internal Relay
      You helped me a lot with your inputs – I was aware how the -RecipientAddressContainsWord worked but just couldn't figure it out how to do it cummulative.
      Since I'm using another variable with a lot of domains I'm now doing it like that:

      
      $domains = get-acceptedDomain | where{$_.DomainType-match"InternalRelay"} | select-object -expandproperty DomainName | select-object -expand Address
      $collection = Get-Transportrule test | select -expand RecipientAddressContainsWords
      
      $domains | % {$collection += $_}
      
      set-transportrule test -RecipientAddressContainsWords $collection

      Thanks again for your help!

  • #27641
    Profile photo of Joel
    Joel
    Participant

    I have a question that is similar so thought I'd just add to this thread instead of starting a new one.

    I am looking for a way to add recipient domains to an existing transport rule (Office 365) and for the new value to reflect the list of domains in a CSV value, which are alphabetical.

    Below is the script that I have cobbled together to change more than one rule. It works in that it adds the domain, but it adds it to the end of the existing values and I wonder if there is a way to achieve what I am asking by erasing and rewriting instead of just adding the new domain?

    #######################################################
    # Use this script to add new domains to the following Office 365 rules:
    #
    #TestRule1
    #TestRule2
    #
    # First, add the domain to C:\ListofDomains.csv file, then run this script.
    #
    #######################################################
    
    ##############################################
    #IMPORT THE CSV WITH THE NEW DOMAIN(S) ADDED TO IT
    ##############################################
    
    $CsvImport = Import-Csv "C:\ListofDomains.csv"
    
    #######################################################
    #CHANGE "TestRule1"
    #######################################################
    
    $Rule = Get-TransportRule "TestRule1"
    
    $RecipientDomains =$Rule.RecipientDomainIs
    
    foreach($row in $CsvImport)
    
    {
    $RecipientDomains +=$row.Domain
    $DomainsToAdd = $RecipientDomains | Select -Unique
    }
    
    Set-TransportRule "TestRule1" -RecipientDomainIs $DomainsToAdd
    
    #######################################################
    #CHANGE "TestRule2"
    #######################################################
    
    $Rule = Get-TransportRule "TestRule2"
    
    $RecipientDomains =$Rule.RecipientDomainIs
    
    foreach($row in $CsvImport)
    
    {
    $RecipientDomains +=$row.Domain
    $DomainsToAdd = $RecipientDomains | Select -Unique
    }
    
    Set-TransportRule "TestRule2" -RecipientDomainIs $DomainsToAdd
    

    Thank you in advance for your help.

  • #27665
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    $DomainsToAdd = $RecipientDomains | Select -Unique | Sort-Object

    ?

  • #27768
    Profile photo of Joel
    Joel
    Participant

    Thanks, that seems to add the newly added item from the CSV into the values alphabetically, just like I asked.

    Now, I'm trying to figure out how to just replace the entire existing value with the current CSV list. So, in essence, the script would replace the existing list of domains with the full list in the CSV. Is there an easy way to do this?

    Without the csv, I could use...

    Get-TransportRule "Test" | Set-TransportRule -RecipientDomainIs {aaa.com}, {bbb.com}, {ccc.com}

    But it is much harder to continue to add domains alphabetically that way than it would be to use a csv and a script.

    Thank you!

  • #27784
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    So you just need to set RecipientDomains to domain list contained in csv or text file ?
    for example
    domains.txt

    aaa.com
    ccc.com
    bbb.com

    $domains = Get-Content domains.txt | Sort-Object # loading and sorting
    Get-TransportRule 'Test' | Set-TransportRule -RecipientDomains $domains

    or domains.csv

    Domain, other info
    aaa.com, bebebe
    ccc.com, mememe
    bbb.com, quququ

    $domains = Import-CSV domains.csv -Delilimter ',' | Select -ExpandProperty Domain | Sort-Object # loading and sorting
    Get-TransportRule 'Test' | Set-TransportRule -RecipientDomains $domains

  • #27932
    Profile photo of Joel
    Joel
    Participant

    Well, it looks like I was making this WAAAY harder than it should be. Thank you so much! This is exactly what I needed.

  • #13395
    Profile photo of ceem88
    ceem88
    Participant

    I need to specify my need.
    I have the following variable:
    $domain = test.com, test2.com
    I tried to execute it with the following command but it keeps deleting the last record:
    `$domain | % {set-transportrule test -RecipientAddressContainsWords $_}
    So how do I manage to do that?

    Thanks for your help

You must be logged in to reply to this topic.