【问题标题】:Powershell Assign MobilePhone property from Active Directory to variablePowershell 将 Active Directory 中的 MobilePhone 属性分配给变量
【发布时间】:2017-03-01 05:16:25
【问题描述】:

我写了一个小脚本,但我想知道这是否是使用 powershell 从 Active Directory 中提取 MobilePhone 属性的正确方法?

$csvfi = import-csv "C:\Users\\Documents\users.csv"

foreach($row in $csvfi)
{
    $cellphone = $row.Phone
    $fullname = $row.Name

    $adphone = (Get-ADUser -Filter "Name -eq '$fullname'" -Properties * | Select MobilePhone).MobilePhone

    Write-Host $fullname, $adphone
}

-Filter,然后-Properties *,然后管道到Select,然后从该对象获取.MobilePhone 属性似乎很麻烦。

附带说明一下,我只需要来自 AD 的原始手机号码 1-AAA-NNN-NNNN,这样我就可以将它与电子表格 users.csv 中的手机号码进行比较。

【问题讨论】:

  • 你对正确的定义是什么?这似乎有效。你还有什么期待?你为什么不想做-properties MobilePhone
  • 猜更短。还是通过我上面提到的所有选项的唯一方法?只是提取 1 个属性似乎很多。

标签: powershell active-directory


【解决方案1】:
$adphone = (Get-ADUser -Filter "Name -eq '$fullname'" -Properties MobilePhone).MobilePhone

【讨论】:

  • 这实际上也适用(Get-ADUser -Filter "Name -eq '$fullname'" -Properties *).MobilePhone,但我认为它的效率较低,因为它会引入所有属性?基本上使用* 只是为了直观地查看该对象的所有属性,如果您知道属性名称,那么只需显式使用它?
  • 正确,除非您显然出于某种原因需要所有属性。您还可以完全排除 -properties 并获取一组默认属性,但不包括手机 afaik。
  • 不,它没有。它为您提供 SamAccountName 和类似的基本属性。谢谢
猜你喜欢
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
  • 2016-12-24
相关资源
最近更新 更多