尝试使用Powershell If语句在Active Directory的Fax字段中添加或替换内容



我有一个导出为csv格式的用户列表。有些有传真号码,有些没有。为了在这里添加我需要的数字,我这样写:

$users = Import-Csv C: (My CSV path)
ForEach($user in $users) {
if(Get-ADUser $user.samaccountname -Properties fax -eq $null) {
Set-ADUser $user.samaccountname -add @{fax=$($user.fax)}
}
else {
Set-ADUser $user.samaccountname -Replace @{fax=$($user.fax)}
}
}
我得到的错误是:

-Properties:术语"-Properties"不被识别为cmdlet、函数、脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后再试一次

如有任何帮助,不胜感激。

  1. if(Get-ADUser $user.samaccountname -Properties fax -eq $null)-这很奇怪。

我认为你应该这样做

$user = Get-ADUser $user.samaccountname -Properties fax
if (($null -ne $user) -or ($null -eq $user.fax)) {
  1. AD默认没有fax属性,但otherfacsimiletelephonenumber存在。最好在设置时使用,因为您永远不知道cmdlet将如何准确地解释它。

  2. 为什么用if检查fax -eq $null?如果没有传真,你添加它,如果有,你替换它。为什么不替换always ?

  3. 使用管道

其中,您不应该检查user是否存在或fax是否存在,因为您覆盖了它,这会导致一行代码:

Import-Csv C:Csv.csv |
ForEach-Object {Set-ADUser -Identity $_.samaccountname -Replace @{'otherFacsimileTelephoneNumber'=$_.fax}}

最新更新