简体   繁体   中英

How do I set the value of a variable in Powershell using Get-ADUser?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM