Get username from custom computer attribute and add location from user object

Martin Chabr 21 Reputation points
2021-03-08T18:08:22.487+00:00

I need a single powershell script combining ad-computer + get-user to get the missing user attribute. I dont now how to combine the two classed into one script.
The Computer object attribute: "extensionattribute1" already has the Username in it, and i need to add the "physicalDeliveryOfficeName" to the computer attrbiute:
extensionattribute2.

Something like:

get-adcomputer COMPUTER1 -Properties Name,extensionattribute1 | select Name,extensionattribute1,(IN HERE I WOULD NEED THE QUERY FOR USER ATTRIBUTE "physicalDeliveryOfficeName")

set-ADComputer -Replace @{extensionAttribute2 = "$($_.physicalDeliveryOfficeName)"}

I have seen this for the Manager attribute. I would need the "physicalDeliveryOfficeName":
@{n='Manager';e={(Get-ADUser $_.Manager).Name}}

I need to combine these parts together into one script so i can write the location into the empty computer attribute extensionattribute2.

Computer Object: NOW:

name,extensionattribute1,extensionattribute2
PC1,John Doe,"NOT SET"

NEEDED:

name,extensionattribute1,extensionattribute2
PC1,John Doe,Office location xyz

Thanks in advance

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,696 Reputation points
    2021-03-09T03:13:15.367+00:00

    A bit longer than one line, but it does have error checking:
    EDIT: Moved the $n = $_.Name inside the ForEach-Object block.

    Get-ADComputer COMPUTER1 -Properties Name, extensionattribute1 |
        ForEach-Object{
            $n = $_.Name  # needed to report computer name in Catch block
            $u = Get-ADUser -Filter {Name -eq $_.extensionattribute1} -Properties physicalDeliveryOfficeName
            if ($u){
                Try{
                    $p = $u.physicalDeliveryOfficeName
                    Set-ADComputer $_ -Replace @{extensionattribute2 = $p} -ErrorAction Stop
                }
                Catch{
                    Write-Host "Failed to update computer object '$n'"
                }
            }
            else{
                Write-Host "Failed to find user '$($_.extensionattribute1)"
            }
        }
    

1 additional answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,486 Reputation points Microsoft Vendor
    2021-03-09T10:18:50.99+00:00

    Hi,

    You can try something like below

    $computers = 'computer1','computer2'  
    $computers | Get-ADComputer -Properties extensionattribute1 | ForEach-Object{  
        $office = (Get-ADUser -Filter {Name -eq $_.extensionattribute1} -Properties physicalDeliveryOfficeName).physicalDeliveryOfficeName  
        Set-ADComputer -Identity $_ -Replace @{extensionattribute2 = $office}  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.