none
automation of creating transport rules - variable mismatch RRS feed

  • Question

  • hi,

    i am trying to automate the creation of transport rules in our company (exchange 2007/powershell 3.0)

    as a part of a script to create new AD-groups.

    what i want to achieve is to set a new transport rule to send a blindcopy to a mailbox (mailbox1) if a specific group (mailbox0) is addressed. the code i came up wiht is:

    $shrtTeam = "team" $shrtCollector = "collector"

    $project = "project001" # create transport rule - >in< $Condition1 = Get-TransportRulePredicate sentto $ADOteam = get-adgroup -filter * -Properties * | where { $_.name -eq $shrtTeam} | select -Property mail $Condition1.Addresses = @($ADOteam) $Action1 = Get-TransportRuleAction BlindCopyTo $ADOCollector = get-mailbox $shrtCollector | select userprincipalname $Action1.Addresses = @($ADOCollector) $IN_name = $project + " in" New-TransportRule -name $IN_name -Condition @($Condition1) -Action @($Action1)

    the error i get is a variable mismatch between Selected.Microsoft.ActiveDirectory.Management.ADGroup and Microsoft.Exchange.Data.Directory.Management.MailEnabledRecipient.

    can i somehow convert / extract the mailbox name to make this work?

    thank you for any ideas.

    Friday, January 15, 2016 9:22 AM

Answers

  • Hi Sha,

    not to worry, this is not an SID issue. It works just fine for me, but I'm running Exchange 2010 ...

    Hm, how about this then?

    # Old line
    $Condition1.Addresses = @($ADOteam)
    
    # New line
    $Condition1.Addresses = @((New-Object Microsoft.Exchange.Data.SmtpAddress($ADOteam)))

    In addition to the previous change. This should get you past the Type coercion step.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by SHA_mhz Monday, January 18, 2016 3:46 PM
    Friday, January 15, 2016 1:18 PM
  • Hi Sha,

    sure, it's a small fix:

    $ADOteam = get-adgroup -filter {name -eq $shrTeam} -Properties mail | select -expand mail

    Now what did I change?
    - Speeded it up by making the filter part of the query (you were retrieving all groups in your AD first and then filtering, rather than only getting what you need)
    - Speeded it up a bit more, by only retrieving the property we want
    - Converted your select to expand the mail property. That way you have an email address in $ADOteam, rather than an object with a 'mail' property.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by SHA_mhz Monday, January 18, 2016 3:46 PM
    Friday, January 15, 2016 10:51 AM
  • hi - thanx for your quick reply,

    this is the result:

       TypeName: Microsoft.Exchange.MessagingPolicies.Rules.Tasks.SentToPredicate
    
    Name      MemberType Definition
    ----      ---------- ----------
    Addresses Property   Microsoft.Exchange.Data.Directory.Management.MailEnabledRecipient[] Addresses {get;set;}

    is this what you get/expected?

    best

    marc

    • Marked as answer by SHA_mhz Monday, January 18, 2016 3:45 PM
    Monday, January 18, 2016 1:34 PM
  • hooray, now we got it! :)

    with your kind help, this is my finally working code:

    $shrtTeam = "test_001team"
    $shrtCollector = "test_001.collector" 
    
    # create transport rule - >in<
      $Condition1 = Get-TransportRulePredicate sentto
      $ADOteam = get-adgroup -filter {name -eq $shrtTeam} -Properties mail | select -expand mail
      $Condition1.Addresses = @(get-distributiongroup $ADOteam)
      $Action1 = Get-TransportRuleAction BlindCopyTo
      $ADOCollector = get-mailbox $shrtCollector | select -expand userprincipalname
      $Action1.Addresses = @(get-mailbox $ADOCollector)
      $IN_name = "foo"
      New-TransportRule -name $IN_name -Condition @($Condition1)  -Action @($Action1)

    thank you very much again. i got one or two glimpses of understanding ... ;)

    best regards

    marc



    • Marked as answer by SHA_mhz Monday, January 18, 2016 3:12 PM
    • Edited by SHA_mhz Monday, January 18, 2016 3:16 PM
    Monday, January 18, 2016 3:11 PM

All replies

  • Hi Sha,

    sure, it's a small fix:

    $ADOteam = get-adgroup -filter {name -eq $shrTeam} -Properties mail | select -expand mail

    Now what did I change?
    - Speeded it up by making the filter part of the query (you were retrieving all groups in your AD first and then filtering, rather than only getting what you need)
    - Speeded it up a bit more, by only retrieving the property we want
    - Converted your select to expand the mail property. That way you have an email address in $ADOteam, rather than an object with a 'mail' property.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by SHA_mhz Monday, January 18, 2016 3:46 PM
    Friday, January 15, 2016 10:51 AM
  • wow - cool  thank you so much!
    Friday, January 15, 2016 11:06 AM
  • hmm .. still not working as it should. the result/variable looks like a mail address but can not be used here still ...

    i get the error:

    "Cannot convert the "test_c017Team@mycompany.com" value of type "System.String"
    to type "Microsoft.Exchange.Data.Directory.Management.MailEnabledRecipient"."

    any more ideas?

    thank you very much.

    do i need to dive into SIDs ?

    best regards sha_mhz

    Friday, January 15, 2016 11:27 AM
  • Hi Sha,

    not to worry, this is not an SID issue. It works just fine for me, but I'm running Exchange 2010 ...

    Hm, how about this then?

    # Old line
    $Condition1.Addresses = @($ADOteam)
    
    # New line
    $Condition1.Addresses = @((New-Object Microsoft.Exchange.Data.SmtpAddress($ADOteam)))

    In addition to the previous change. This should get you past the Type coercion step.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by SHA_mhz Monday, January 18, 2016 3:46 PM
    Friday, January 15, 2016 1:18 PM
  • than you very much - still can not convert variables,  still does not match ---

    code is like this now:

    # create transport rule - >in<
      $Condition1 = Get-TransportRulePredicate sentto
      $ADOteam = get-adgroup -filter {name -eq $shrtTeam} -Properties mail | select -expand mail
      $Condition1.Addresses = @((New-Object Microsoft.Exchange.Data.SmtpAddress($ADOteam)))
     # $Condition1.Addresses = @($ADOteam)
      $Action1 = Get-TransportRuleAction BlindCopyTo
      $ADOCollector = get-mailbox $shrtCollector | select userprincipalname
      $Action1.Addresses = @($ADOCollector)
      $IN_name = $kuerzel + " in"
      New-TransportRule -name $IN_name -Condition @($Condition1)  -Action @($Action1)


    error:

    Cannot convert the "admins@mycompany.com" value of type
    "Microsoft.Exchange.Data.SmtpAddress" to type "Microsoft.Exchange.Data.Directory.Management.MailEnabledRecipient"
    ...
    Cannot convert the "@{UserPrincipalName=mh@sauerbruchhutton.de}" value of type
    "Selected.Microsoft.Exchange.Data.Directory.Management.Mailbox" to type
    "Microsoft.Exchange.Data.Directory.Management.MailEnabledRecipient"."

    1 is it possible that this will not work in exchange 2007?

    2 any other ideas?

    (i know i still did not apply your solution to the $action.addresses1 line only to the $condition.addresses1 line)

    cheers

    marc

    p.s. silly me tried to cast a variable of type microsoft.exchange.date.direcory.management.mailenabledreripient - but of course there is no constuctor.

    • Edited by SHA_mhz Monday, January 18, 2016 12:37 PM
    Monday, January 18, 2016 12:30 PM
  • Hi Marc,

    try running this command on your ExchangeShell:

    Get-TransportRulePredicate sentto | Get-Member Addresses

    What definition do you receive? "Microsoft.Exchange.Data.SmtpAddress[] Addresses {get;set;}" ?

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Monday, January 18, 2016 12:57 PM
  • hi - thanx for your quick reply,

    this is the result:

       TypeName: Microsoft.Exchange.MessagingPolicies.Rules.Tasks.SentToPredicate
    
    Name      MemberType Definition
    ----      ---------- ----------
    Addresses Property   Microsoft.Exchange.Data.Directory.Management.MailEnabledRecipient[] Addresses {get;set;}

    is this what you get/expected?

    best

    marc

    • Marked as answer by SHA_mhz Monday, January 18, 2016 3:45 PM
    Monday, January 18, 2016 1:34 PM
  • Hi Marc,

    not exactly - they changed it between 2007 and 2010 - but I can work with it:

    # create transport rule - >in<
    $Condition1 = Get-TransportRulePredicate sentto
    $ADOteam = Get-ADGroup -Filter { name -eq $shrtTeam } -Properties mail | Select-Object -ExpandProperty mail
    $Condition1.Addresses = @(Get-DistributionGroup $ADOteam)
    # $Condition1.Addresses = @($ADOteam)
    $Action1 = Get-TransportRuleAction BlindCopyTo
    $ADOCollector = get-mailbox $shrtCollector
    $Action1.Addresses = @($ADOCollector)
    $IN_name = $kuerzel + " in"
    New-TransportRule -name $IN_name -Condition @($Condition1) -Action @($Action1)

    Basically, that type cannot handle string input and cannot handle smtp addresses (the 2010 wants smtp addresses). Passing the actual distribution group / mailbox object should work, as both inherit from MailEnabledRecipient.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Monday, January 18, 2016 2:07 PM
  • hooray, now we got it! :)

    with your kind help, this is my finally working code:

    $shrtTeam = "test_001team"
    $shrtCollector = "test_001.collector" 
    
    # create transport rule - >in<
      $Condition1 = Get-TransportRulePredicate sentto
      $ADOteam = get-adgroup -filter {name -eq $shrtTeam} -Properties mail | select -expand mail
      $Condition1.Addresses = @(get-distributiongroup $ADOteam)
      $Action1 = Get-TransportRuleAction BlindCopyTo
      $ADOCollector = get-mailbox $shrtCollector | select -expand userprincipalname
      $Action1.Addresses = @(get-mailbox $ADOCollector)
      $IN_name = "foo"
      New-TransportRule -name $IN_name -Condition @($Condition1)  -Action @($Action1)

    thank you very much again. i got one or two glimpses of understanding ... ;)

    best regards

    marc



    • Marked as answer by SHA_mhz Monday, January 18, 2016 3:12 PM
    • Edited by SHA_mhz Monday, January 18, 2016 3:16 PM
    Monday, January 18, 2016 3:11 PM
  • Marc - you should mark the posts that helped you get to your final destination as answers, not just your reply to them.


    Monday, January 18, 2016 3:23 PM
  • sorry - me noob ...;)
    Monday, January 18, 2016 3:46 PM
  • sorry - me noob ...;)

    No worries, welcome to the community.

    =]


    Monday, January 18, 2016 3:48 PM