locked
Invoke-Command Script Block does not accept -RecipientFilter Paramter of New-DynamicDistributionGroup command RRS feed

  • Question

  • HI Guys,

    running into a block here.

    In one of our tool we are creating dynamic distribution lists both on prem and on O365 as we have session for both saved in their own variables. and then using invoke-command to run the new-dynamicdistributiongroup once with on prem session and once with O365 session. this works perfectly until it reaches creating dynamic DLs as script block does not accept -recipientFilter parameter value. I was wondering if anyone knows a work around or have faced such issue. here is the script example.

    (Invoke-Command -Session $on_prem_ses -ScriptBlock {New-DynamicDistributionGroup -Name $Using:SiteDL -OrganizationalUnit $Using:OU} -RecipientContainer $Container -RecipientFilter {((RecipientTypeDetails -eq 'UserMailbox') -or (RecipientTypeDetails -eq 'RemoteUserMailbox')) -and ((CustomAttribute7 -eq 'U') -or (CustomAttribute7 -eq '2')) -and (CustomAttribute4 -eq $CustomAttribute4) -and -not(UserAccountControl -eq '514')})
                        Write-Host "    O365 Creation ......" -ForegroundColor Green
    (Invoke-Command -Session $on_prem_ses -ScriptBlock {New-DynamicDistributionGroup -Name $Using:SiteDL} -RecipientFilter {((RecipientTypeDetails -eq 'UserMailbox') -or (RecipientTypeDetails -eq 'MailUser')) -and ((CustomAttribute7 -eq 'U') -or (CustomAttribute7 -eq '2')) -and (CustomAttribute4 -eq $CustomAttribute4) -and -not(UserAccountControl -eq '514') -and -not(ExchangeGuid -eq "00000000-0000-0000-0000-000000000000")})

    the error is; since -recipientfilter requires {}, Invoke-Command and ScriptBlock does not allow any other parameters that requires {}
    • Edited by Riaz Ansary Friday, October 13, 2017 5:08 PM
    Friday, October 13, 2017 4:18 PM

Answers

  • found the solutions after digging and researching this. 

    we need to build a script block within the script block here is the fix

    $FilterOnPrem = "((RecipientTypeDetails -eq 'UserMailbox') -or (RecipientTypeDetails -eq 'RemoteUserMailbox')) -and ((CustomAttribute7 -eq 'U') -or (CustomAttribute7 -eq '2')) -and (CustomAttribute4 -eq '$CustomAttribute4') -and -not(UserAccountControl -eq '514')}"
    $FilterOnline = "((RecipientTypeDetails -eq 'UserMailbox') -or (RecipientTypeDetails -eq 'MailUser')) -and ((CustomAttribute7 -eq 'U') -or (CustomAttribute7 -eq '2')) -and (CustomAttribute4 -eq '$CustomAttribute4') -and -not(UserAccountControl -eq '514') -and -not(ExchangeGuid -eq '00000000-0000-0000-0000-000000000000')"
                        (Invoke-Command -Session $on_prem_ses -ScriptBlock {New-DynamicDistributionGroup -Name $Using:SiteDL -OrganizationalUnit $Using:OU -RecipientContainer $Container -RecipientFilter ([scriptBlock]::Create($Using:FilterOnPrem))})
                        Write-Host "    O365 Creation ......" -ForegroundColor Green
                        (Invoke-Command -Session $on_prem_ses -ScriptBlock {New-DynamicDistributionGroup -Name $Using:SiteDL -RecipientFilter ([scriptBlock]::Create($Using:FilterOnline))})


    pay attention to:

    RecipientFilter ([scriptBlock]::Create($Using:FilterOnPrem))

    let me know if you have any questions. 

    credit to

    https://www.michev.info/Blog/Post/1687/using-variables-with-invoke-command-in-remote-powershell-sessions-in-exchange-online

    • Marked as answer by Riaz Ansary Friday, October 13, 2017 5:10 PM
    • Edited by Riaz Ansary Friday, October 13, 2017 5:13 PM
    Friday, October 13, 2017 5:10 PM

All replies

  • found the solutions after digging and researching this. 

    we need to build a script block within the script block here is the fix

    $FilterOnPrem = "((RecipientTypeDetails -eq 'UserMailbox') -or (RecipientTypeDetails -eq 'RemoteUserMailbox')) -and ((CustomAttribute7 -eq 'U') -or (CustomAttribute7 -eq '2')) -and (CustomAttribute4 -eq '$CustomAttribute4') -and -not(UserAccountControl -eq '514')}"
    $FilterOnline = "((RecipientTypeDetails -eq 'UserMailbox') -or (RecipientTypeDetails -eq 'MailUser')) -and ((CustomAttribute7 -eq 'U') -or (CustomAttribute7 -eq '2')) -and (CustomAttribute4 -eq '$CustomAttribute4') -and -not(UserAccountControl -eq '514') -and -not(ExchangeGuid -eq '00000000-0000-0000-0000-000000000000')"
                        (Invoke-Command -Session $on_prem_ses -ScriptBlock {New-DynamicDistributionGroup -Name $Using:SiteDL -OrganizationalUnit $Using:OU -RecipientContainer $Container -RecipientFilter ([scriptBlock]::Create($Using:FilterOnPrem))})
                        Write-Host "    O365 Creation ......" -ForegroundColor Green
                        (Invoke-Command -Session $on_prem_ses -ScriptBlock {New-DynamicDistributionGroup -Name $Using:SiteDL -RecipientFilter ([scriptBlock]::Create($Using:FilterOnline))})


    pay attention to:

    RecipientFilter ([scriptBlock]::Create($Using:FilterOnPrem))

    let me know if you have any questions. 

    credit to

    https://www.michev.info/Blog/Post/1687/using-variables-with-invoke-command-in-remote-powershell-sessions-in-exchange-online

    • Marked as answer by Riaz Ansary Friday, October 13, 2017 5:10 PM
    • Edited by Riaz Ansary Friday, October 13, 2017 5:13 PM
    Friday, October 13, 2017 5:10 PM
  • I have no idea what I'm looking at in that script but I know that whenever I use a -scriptblock I have to hand it variables with the -argumentlist switch.  (scoping issues)  I don't see that so I'm not sure how the $using vars are derived.

    thing-blaa -scriptblock {$var1comesfrom = $args[0] } -argumentlist $var1GettingPassedToTheBlock

    Sorry from the looks of it you're way beyond that tip but, what the heck, eh?


    • Edited by Fred B Friday, October 13, 2017 5:17 PM
    Friday, October 13, 2017 5:17 PM