0

I currently have a series of Dyanmic Distribution Groups that I want to edit the recipient filter on. Our company is based by location number, which is a 4 digit number. This number is part of the display name of the dynamic distribution group...example webcontact_1234_DG....1234 would be the 4 digit center number. I am wanting to replace the recipient filter to have office -eq (1234) but have it pull the number from the display name. All display names are going to be the same number of characters before the 4 digit center number, for example, webcontact_1234_DG, webcontact_2345_DG, webcontact_3456_DG, etc.

I have a replace code but it changed the office location to null.

Here is the code that I am using:

$groups=Get-DynamicDistributionGroup -filter {alias -like "webcontact*"}
foreach ($group in $groups) {
     $locationcode=$($group.alias).tostring.replace("\\D", "")
     set-dynamicdistributiongroup $group -recipientfilter {((((office -eq $locationcode) -and
      (have the recipent filter here but can't display due to confidential information) -and
      (RecipientType -eq 'UserMailbox') -and
      (-not(RecipientTypeDetails -eq 'RoomMailbox')) -and
      (-not(RecipientTypeDetails -eq 'SharedMailbox'))))}
}
  • So you want to use the 4 digit value within the display name to change the office field of said contact? – SysEngineer Nov 12 '18 at 18:24
  • As an aside: It's best to [avoid the use of script blocks (`{ ... }`) as `-Filter` arguments](https://stackoverflow.com/a/44184818/45375). – mklement0 Nov 12 '18 at 18:40

2 Answers2

0

It's easiest to use the -split operator to extract the number (text) from your values[1]:

$locationcode = ($group.Alias -split '_')[1]

-split '_' returns the array of tokens that result when you split the input string by _ chars., and [1] returns the 2nd token, which is the desired location number.

Simple example:

PS> ('webcontact_3456_DG' -split '_')[1]
3456

Alternatively, a corrected version of your own attempt (see below) would use the -replace operator:

PS> 'webcontact_3456_DG' -replace '\D' # remove all non-digit chars.
3456

As for what you tried:

$($group.alias).tostring.replace("\\D", "")
  • The [string] type's .Replace() searches by literal strings, so the search for \\D in your names will fail, and no replacement will occur.

    • Additionally, note that PowerShell's escape character is ` (backtick), not \, and that \ therefore doesn't require escaping: in PowerShell "\\D" literally becomes \\D.
  • As an aside: There is generally no need to put $(...) around expressions.


[1] You could also use the [string] type's .Split() .NET method in this simple case, but I suggest using the far more flexible -split PS operator as a matter of habit.

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

This is the best answer I can come up with based on the given information. I assume you are having a problem getting that number out of your webcontact_1234_DG, I would use regex to get those numbers out and into another variable.

$locationcode = [regex]::Match($group.alias,'^[^_]+_([^_]+)_[^_]+$').Groups[1].Value

The above code will grab everything in between the two underscores.

Try that and let me know.

SysEngineer
  • 354
  • 1
  • 7