0

I'm trying to update Active Directory with info from a csv file generated by one of our client-facing systems. I'm storing the info, an ID#, in the facsimileTelephoneNumber AD Attribute (don't ask why). Here's where it's getting tricky. If there is already a value in that attribute that is different from the one that is stored in the csv file, I don't want to change it. I'm setting the value from the csv file to variable $ID, and trying to set the value from AD to variable $CurrentID. From there, I'm comparing the two to see if they're equal. Importing the $ID from the csv is working fine, no problem there. But setting the $CurrentID using Get-ADuser isn't doing what I expect it to.

$CurrentID = Get-ADUser -Identity $Username -Properties facsimileTelephoneNumber

is what I'm using to try and set that. But when I compare the 2 variables using:

if ($CurrentID -eq $ID) {Execute this code}

it always tests false, so nothing happens. When I call the variables to see what's in them, I get the correct data from $ID, but $Current ID, the one I pull using Get-ADUser, returns a whole block of attributes, DistinguishedName, Enabled, facsimileTelephoneNumber (which is the one I want), GivenName, Name, ObjectClass, ObjectGUID, SAMAccountName, SID, Surname, and UserPrincipalName. I think that's why the equal test is failing, but I don't know how to fix it. I'm very new to Powershell scripting, so be kind.

JamesMay
  • 3
  • 3
  • You need the **value** of that property, not a user object with properties of which one is the one you are after, `$CurrentID = (Get-ADUser -Identity $Username -Properties facsimileTelephoneNumber).facsimileTelephoneNumber` – Theo Feb 22 '22 at 18:50
  • What kind of value is `$ID`? Is it just the new value for `facsimileTelephoneNumber`? Or does it contain several properties? – Gabriel Luci Feb 22 '22 at 18:51
  • That's because you have to narrow the value at runtime or, you can compare the property using dot notation. `$CurrentID` returns an *ADObject* as you could see, and in order for you to reference a property, you can use the dot operator to do so: `$Current.facsimileTelephoneNumber`. Where is your `$username` coming from? – Abraham Zinala Feb 22 '22 at 18:52

1 Answers1

0

but $Current ID, the one I pull using Get-ADUser, returns a whole block of attributes

That's normal. Get-ADUser returns an object with all the properties. To compare just the one property, use $CurrentID.whatever. For example:

if ($CurrentID.facsimileTelephoneNumber -eq $ID) {Execute this code}

If $ID is also an object with properties, then you might need to do the same for that:

if ($CurrentID.facsimileTelephoneNumber -eq $ID.facsimileTelephoneNumber) {Execute this code}

But I can't tell you that for sure without knowing what type of variable $ID is.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
  • That was it. I didn't understand the difference in an AD object and the individual properties contained within it. Thanks very much for the help. – JamesMay Feb 24 '22 at 02:06