0

I'm attempting to write a Powershell script that allows me to do the following:

  1. Use Get-ADGroupMember to get users that are apart of a specific group
  2. Use info from step one in Get-ADUser to get user info in lastname, firstname format
  3. Use string from step 2 in Get-ADComputer to search the description field of all computers to find computers that have that string within its description field.

Here is what I was trying (thought it would work in my head):

Get-ADGroupMember 'Group Name' | Get-ADUser -Properties givenName, sn | select givenName, sn | Get-ADComputer -filter 'description -like "$sn,$givenName"' -property description | select Name*

Bold text works, I know Italics text wouldn't work but thats the format of how I'd think it would work

Let me know if I made any since, definitely a Powershell newbie

TLDR: trying to get Names of users and their computer name's based on a search of specific AD group

1 Answer 1

1

At that point in the pipeline, you're no longer directly using the output object of Get-ADUser as the input object of Get-ADComputer. That's where the ForEach-Object cmdlet comes in. It takes a scriptblock that allows you the define the behavior for each item in the pipeline:

Get-ADGroupMember 'Group Name' | 
    Get-ADUser -Properties givenName, sn | 
    ForEach-Object -Process {
        $sn = $_.sn
        $givenName = $_.givenName

        Get-ADComputer -Filter 'description -like "$sn,$givenName"' -property description
    } | select Name*
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for that. However, I noticed that I could only get the above to work by manually adding a known value in "$sn,$givenName". For example I know one user in a group, so I put their last name "LastName*". This allowed the command to work by only showing that users computer name. Any thoughts on why it's not excepting the $sn and $givenName?
@Chris not sure as I didn't actually run the code against AD, but check to make sure your property names are correct.
Hmm, I'll have to dig into this. I added an echo line within the ForEach (echo "$sn, $givenName") and they are printing correctly. Unfortunately, when they're being passed into -like "$sn, $givenName*", (added wildcard just to see if it made a difference) nothing is being found. I'll let you know what if I figure it out. Appreciate the help
@Chris mind your spacing; the code sample you gave in the question has no space after the comma, in the comment though you do have one, make sure it's correct..?

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.