0

I'm trying to get my brain round something and on the verge of head explosion. I have a list of data listing usernames, and virtual machines.

for example:

username      servername
bob.lazar     abc001
bob.lazar     abc002
peter.bob     ght004
bob.lazar     abc006

I need to collate the user data so I can email them listing. if I loop through and email it would mail bob 3x times, but I need to email bob once with 3 server names. I assume I need to put the data into a multidimential array and loop through but just don't know where to begin.

1
  • 2
    $data | group username | % { do something } Commented Jul 6, 2016 at 12:49

1 Answer 1

2

I didn't fully understand the problem but I'm pretty sure you are looking for the GROUP cmdlet. Pipe a collection to it and specify what property you want to group things by and then all the elements are available as a GROUP property

JPS> dir |group extension |? Name -eq ".txt" | % Group


    Directory: C:\temp


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         6/2/2016   8:23 PM          57930 foo.txt
-a----       12/21/2015   8:00 PM           8265 ModuleSessions.ps1.t
-a----        3/15/2016   6:26 PM             51 num.txt
-a----         6/6/2015   6:55 PM           2390 t2.txt

Alternatively, you can use -AsHashtable to return a hashtable with the property as a key

JPS> (dir |group Extension -AsHashTable)[".txt"]


    Directory: C:\temp


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         6/2/2016   8:23 PM          57930 foo.txt
-a----       12/21/2015   8:00 PM           8265 ModuleSessions.ps1.txt
-a----        3/15/2016   6:26 PM             51 num.txt
-a----         6/6/2015   6:55 PM           2390 t2.txt

Experiment with those.

Sign up to request clarification or add additional context in comments.

2 Comments

I need to email the user in question a list of server names, I can look up the user against AD to get the email address, and email them all the server names that match.
I'm gathering I can use group, then use a foreach to loop through the names

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.