0
votes

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

1 Answers

1
votes

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*