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

尝试使用Powershell If语句添加或替换Active Directory传真字段中的内容,powershell,if-statement,active-directory,Powershell,If Statement,Active Directory,我有一个导出到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)}

我有一个导出到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'不能识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试

任何帮助都将不胜感激

  • if(Get ADUser$user.samaccountname-Properties fax-eq$null)
    -这是wierd
  • 我想你应该这样做

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

  • 为什么使用
    if
    检查
    fax-eq$null
    ?如果没有传真,则添加传真,如果有,则替换传真。为什么不总是更换

  • 使用管道


  • 其中,您不应检查用户是否存在或传真是否存在,因为您将覆盖它,这将导致一行:

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

    使用
    if((Get ADUser$user.samaccountname-Properties fax).fax){}
    处理
    $null
    案例。感谢您的回复!我尝试使用If语句的原因是,如果用户的传真字段中已有内容,AD Powershell不允许我使用-Replace。我的想法是检查传真字段中是否有内容,如果没有,我将使用-Add cmdlet,如果有,我将使用-Replace cmdlet。在修改并执行了上面的命令之后,我能够运行一个测试列表,并替换任何人的传真字段中的内容。对于其他传真字段中没有内容的用户,我得到了以下错误:Set ADUser:replace