?
Solved

Create a dynamic distribution group using get-date on whencreated

Posted on 2011-09-29
16
Medium Priority
?
781 Views
Last Modified: 2012-08-14
I set a DDG filter using this Powershell command and it works fine:
Set-DynamicDistributionGroup $ddg2 -RecipientFilter {(whenCreated -ge '9/29/10') -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $null)}

But what I really want is a rolling whencreated Date to only include the accounts crated within the last year from the current date:
Set-DynamicDistributionGroup $ddg2 -RecipientFilter {(whenCreated -ge (get-date).addDay(-365d)) -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $null)}

I don't find anyway of getting the RecipientFilter to accept the get-date command.
0
Comment
Question by:uwsppm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
16 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 36895276
Hi, I'm not too well versed with Powershell yet, but can you change
(get-date).addDay(-365d)
to this
((get-date).adddays(-365)).ToShortDateString()

Overall though, I think you didn't add the "s" to AddDay

Rob.
0
 

Author Comment

by:uwsppm
ID: 36897389
Yeah - I realized i missed typing the "s" in the AddDays - but it's the get-date that's the problem as far as I can tell - it doesn't seem acceptable in a DDG filter.

Set-DynamicDistributionGroup : Cannot bind parameter 'RecipientFilter' to the target. Exception setting "RecipientFilter": "Invalid filter syntax. For a description of the filte
r parameter syntax see the command help.
"(whenCreated -ge ((get-date).adddays(-365))) -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $nu
ll)" at position 18."
At line:1 char:52

The syntax itself isn't a problem as it works fine with get-user:
Get-User -ResultSize unlimited | where {($_.whenCreated -ge ((get-date).adddays(-365)))}
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36898090
OK, well get-date returns a date object, so maybe turn it into a string, by putting single quotes around it, as that has worked in the first example where you specified the date in quotes.
Set-DynamicDistributionGroup $ddg2 -RecipientFilter {(whenCreated -ge '$(((get-date).adddays(-365)).ToShortDateString())' -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $null)}

Open in new window


We could also try using ToString(), but I think that's implicitly done anyway by ToShortDateString().
Set-DynamicDistributionGroup $ddg2 -RecipientFilter {(whenCreated -ge '$(((get-date).adddays(-365)).ToShortDateString().ToString())' -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $null)}

Open in new window


Regards,

Rob.
0
Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 65

Expert Comment

by:RobSampson
ID: 36898097
P.S. Note that when using any dot notation inside a string, you need to change
((get-date).adddays(-365)).ToShortDateString()
to
$(((get-date).adddays(-365)).ToShortDateString())

so that the entire formula, with its dot notation, it evaluated as an expression.

Rob.
0
 

Author Comment

by:uwsppm
ID: 36906788
Using the ToShortDateString in quotes results in this error:

Set-DynamicDistributionGroup : Cannot bind parameter 'RecipientFilter' to the target. Exception setting "RecipientFilter": "The value "$(((get-date).adddays(
-365)).ToShortDateString().ToString())" could not be converted to type System.Nullable`1[System.DateTime]."

I tried removing the ToStirng() and it results in the same error.

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36906845
OK, given that ToShortDateString already converts to a string, what happens if you use this:
Set-DynamicDistributionGroup $ddg2 -RecipientFilter {(whenCreated -ge $(((get-date).adddays(-365)).ToShortDateString()) -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $null))}

Open in new window


Regards,

Rob.
0
 

Author Comment

by:uwsppm
ID: 36906866
Rob, Thanks - but still no luck:
 Set-DynamicDistributionGroup $ddg2 -RecipientFilter {(whenCreated -ge $(((get-date).adddays(-365)).ToShortDateString())) -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $null)}
Set-DynamicDistributionGroup : Cannot bind parameter 'RecipientFilter' to the target. Exception setting "RecipientFilter": "Invalid filter syntax. For a desc
ription of the filter parameter syntax see the command help.
"(whenCreated -ge $(((get-date).adddays(-365)).ToShortDateString())) -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customat
tribute2 -like '*AS*')) -and (alias -ne $null)" at position 19."
At line:1 char:52
+ Set-DynamicDistributionGroup $ddg2 -RecipientFilter <<<<  {(whenCreated -ge $(((get-date).adddays(-365)).ToShortDateString())) -and ((customattribute2 -lik
e '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $null)}
    + CategoryInfo          : WriteError: (:) [Set-DynamicDistributionGroup], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.Exchange.Management.RecipientTasks.SetDynamicDistributionGroup
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36906904
LOL!  OK, fine....try this:
Set-DynamicDistributionGroup $ddg2 -RecipientFilter {(whenCreated -ge `'$(((get-date).adddays(-365)).ToShortDateString())`' -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $null))}

Open in new window


Or, would you settle for a two liner?  Hopefully this works.

$oneyearago = $(((get-date).adddays(-365)).ToShortDateString())
Set-DynamicDistributionGroup $ddg2 -RecipientFilter {(whenCreated -ge $oneyearago -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $null))}

Open in new window


or
$oneyearago = $(((get-date).adddays(-365)).ToShortDateString())
Set-DynamicDistributionGroup $ddg2 -RecipientFilter {(whenCreated -ge '$oneyearago' -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $null))}

Open in new window


or
$oneyearago = $(((get-date).adddays(-365)).ToShortDateString())
Set-DynamicDistributionGroup $ddg2 -RecipientFilter {(whenCreated -ge `'$oneyearago`' -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $null))}

Open in new window


or
$oneyearago = "`'$(((get-date).adddays(-365)).ToShortDateString())`'"
Set-DynamicDistributionGroup $ddg2 -RecipientFilter {(whenCreated -ge $oneyearago -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $null))}

Open in new window


Regards,

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36906905
Sorry for all the attempts, I just can't test it, so I'm running a bit blind.

Rob.
0
 

Author Comment

by:uwsppm
ID: 36907004
Set-DynamicDistributionGroup $ddg2 -RecipientFilter {(whenCreated -ge `'$(((get-date).adddays(-365)).ToShortDateString())`') -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $null)}
Set-DynamicDistributionGroup : Cannot bind parameter 'RecipientFilter' to the target. Exception setting "RecipientFilter": "Invalid filter syntax. For a desc
ription of the filter parameter syntax see the command help.
"(whenCreated -ge `'$(((get-date).adddays(-365)).ToShortDateString())`') -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (cust
omattribute2 -like '*AS*')) -and (alias -ne $null)" at position 18."
At line:1 char:52
+ Set-DynamicDistributionGroup $ddg2 -RecipientFilter <<<<  {(whenCreated -ge `'$(((get-date).adddays(-365)).ToShortDateString())`') -and ((customattribute2
-like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*')) -and (alias -ne $null)}
    + CategoryInfo          : WriteError: (:) [Set-DynamicDistributionGroup], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.Exchange.Management.RecipientTasks.SetDynamicDistributionGroup

The twoliners would work fine if this was code - but since it's a filter for a DDG I need it to be a one-liner.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36907208
Is it applied as a filter to a GUI in Exchange?  I guess it's possible that when it's applied as a filter, cmdlets cannot be used.....

Try this:
Set-DynamicDistributionGroup $ddg2 -RecipientFilter {(whenCreated -ge `'$(((get-date).adddays(-365)).ToShortDateString())`') -and (alias -ne $null) -and ((customattribute2 -like '*CLS*') -or (customattribute2 -like '*FA*') -or (customattribute2 -like '*AS*'))}

Open in new window


If I echo the command in Powershell as Write-Host however, I cannot get it to evaluate the get-date expression.  I think that's why the filter is unable to parse the query string.

Ultimately, it looks like the environment you're running the cmdlet in is unable to recognise it.

I will see if I can contact someone else to chime in who may know a bit more about that.

Rob.
0
 

Author Comment

by:uwsppm
ID: 36932574
Rob has done a lot of thought on this, but I was still waiting to see if he could get anything from his sources as the problem is unresolved.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36937149
Sorry...I'm still waiting on some clarification from some others...hopefully won't be too long.

Rob.
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 36937391
@Rob, I just saw your message on the other post.

I look over this question and have done a few tests. I do not think this will work inside the RecipientFilter. I am not saying this can not be done I just could not figure it out inside the filter. I think your best option would be to use the two liners Rob posted before and run those as a separate script on a scheduled task to update this dynamic group everyday or however often you need it updated. Sorry I could not be more help.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 1000 total points
ID: 36938248
Cheers KenMcF, that was my latest guess on the situation as well.  As I said, I don't the filter is able to evaluate the Powershell command.  Sorry uwsppm, it looks like you will need to use a workaround.

Regards,

Rob.
0
 

Author Closing Comment

by:uwsppm
ID: 36938540
Herculian effort. Tx.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Unified and professional email signatures help maintain a consistent company brand image to the outside world. This article shows how to create an email signature in Exchange Server 2010 using a transport rule and how to overcome native limitations …
In this video we show how to create a Shared Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Sha…
To show how to create a transport rule in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Rules tab.:  To cr…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question