question

MartinChabr-2377 avatar image
0 Votes"
MartinChabr-2377 asked ·

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

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
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered ·

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)"
         }
     }

· 4 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Rich,

thank you very much. The script works without the error checking (I have to remove the Variable on the second line). If this is a quick fix for you feel free to correct it :-) Thanks so much again.

Error message:
At line:2 char:6
+ $n = $_.Name
+ ~~
Expressions are only allowed as the first element of a pipeline.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline




0 Votes 0 ·

Doh! That's because it should go inside the ForEach-Object block. I'll fix that in the code I posted.

0 Votes 0 ·

Thanks so much again, how long does it take you to create such a script ?

0 Votes 0 ·
Show more comments
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

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.



· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That works unless the user name in extensionattribute1 is wrong or no longer present in the AD. The problem with using Get-ADUser with a filter is that it will never fail . . . it just doesn't return a user object.

It would be helpful to know what kind of value is present in that extension attribute. Is it a SID, GUID, DN, sAMAccount (i.e., a unique value), or is it just the name of the AD user?

0 Votes 0 ·