如何使用 Get-ADUser 在 Powershell 中设置变量的值?
我正在尝试使用我们面向客户的系统之一生成的 csv 文件中的信息来更新 Active Directory。我将信息(ID#)存储在 facsimileTelephoneNumber AD 属性中(不要问为什么)。这就是事情变得棘手的地方。如果该属性中已有一个值与 csv 文件中存储的值不同,我不想更改它。我将 csv 文件中的值设置为变量 $ID,并尝试将 AD 中的值设置为变量 $CurrentID。从那里,我比较两者,看看它们是否相等。从 csv 导入 $ID 工作正常,没有问题。但是使用 Get-ADuser 设置 $CurrentID 并没有达到我的预期。
$CurrentID = Get-ADUser -Identity $Username -Properties facsimileTelephoneNumber
这就是我用来尝试设置的。但是当我使用以下方法比较两个变量时:
if ($CurrentID -eq $ID) {Execute this code}
它总是测试错误,所以什么也没有发生。当我调用变量来查看其中的内容时,我从 $ID 中获取了正确的数据,但是 $Current ID(我使用 Get-ADUser 提取的那个)返回一整块属性,DistinguishedName、Enabled、facsimileTelephoneNumber(这是我想要的一个)、GivenName、Name、ObjectClass、ObjectGUID、SAMAccountName、SID、Surname 和 UserPrincipalName。我认为这就是平等测试失败的原因,但我不知道如何解决它。我对 Powershell 脚本非常陌生,所以请友善一点。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

这是正常的。
获取 ADUser
返回一个具有所有属性的对象。要仅比较一个属性,请使用$CurrentID.whatever
。例如:如果
$ID
也是一个具有属性的对象,那么您可能需要为此执行相同的操作:但在不知道变量
类型的情况下,我无法确定地告诉您这一点$ID
是。That's normal.
Get-ADUser
returns an object with all the properties. To compare just the one property, use$CurrentID.whatever
. For example:If
$ID
is also an object with properties, then you might need to do the same for that:But I can't tell you that for sure without knowing what type of variable
$ID
is.