0

I have a list of servers and each one has at least one email distribution list associated with it as a "stakeholder" we can contact for downtime etc.

When we do maintenance we email all the distribution lists of the implicated servers and there is duplication as many servers can all have the same one email contact (SERVER1, SERVER2). The other issue is that a single server could also have multiple email contacts (SERVER3), separated by a semicolon so they parse correctly when pasted into Outlook.

SERVER1     group1@somewhere.com
SERVER2     group1@somewhere.com
SERVER3     group1@somewhere.com ; group23@somewhereelse.com

Right now, from a big array from a spreadsheet, I do either a Get-Unique:

$spreadsheetDataObject |
    select contacts, patchcycle |
    where {($_.patchcycle -eq "SpectreSat")} |
    Sort-Object contacts |
    select contacts |
    Get-Unique

Or a -Unique off theSort-Objects`:

$spreadsheetDataObject |
    select contacts, patchcycle |
    where {($_.patchcycle -eq "SpectreSat")} |
    Sort-Object contacts -Unique |
    select contacts

And both get me the result:

group1@somewhere.com
group1@somewhere.com ; group23@somewhereelse.com

I would like to know the best way to unique "everything" so that the second line separated by the semi-colon would be split out and uniqued as well so the output looks like this:

group1@somewhere.com
group23@somewhereelse.com

What's the best way to do this and in how many steps? Split any email addresses separated by semicolon first into unique array elements and remove the ; (not sure how to do this) and then do a unique on the new array? Maybe a trim as well in case spaces are hiding around the ;.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1854377
  • 33
  • 2
  • 5
  • `$string -split ";"` will take a string, split it at every semicolon, and return an array with one portion per segment that it found, dropping the semicolons. For example, `$baz = "foo;bar" -split ";"` will return an array `$baz` where `$baz[0] -eq "foo"` and `$baz[1] -eq "bar"`. – Jeff Zeitlin Feb 05 '18 at 20:50
  • I've never used split, thanks for the tip. – user1854377 Feb 06 '18 at 18:09

1 Answers1

1

Put a ForEach-Object between Where-Object and Sort-Object where you split the contacts into individual addresses. Since you don't seem to need the patchcycle property after the Where-Object anymore you can simply pass the output to the pipeline without constructing new objects first:

... | Where-Object { $_.patchcycle -eq 'SpectreSat' } |
    ForEach-Object { $_.contacts -split '\s*;\s*' } |
    Sort-Object -Unique

The regular expression \s*;\s* automatically removes whitespace surrounding the semicolons.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Awesome! That appears to work like a charm! I was able to run it but it turns out I have some more whitespace in my email addresses that must be present outside the ";" that are still causing dupes. I will try to address that beforehand. Thanks! – user1854377 Feb 06 '18 at 18:09