简体   繁体   English

Powershell 帮助更新 Active Directory 属性

[英]Powershell help updating Active Directory attributes

Get-ADUser -Filter 'l -like "Atlanta"' -SearchBase 'OU=atl,DC=ego,DC=ga,DC=com' -Properties DisplayName | % {Set-ADUser $_ @{GMLocation='ATL'} }

I am trying to update the GMLocation of all users that have a location of Atlanta with the code above.我正在尝试使用上面的代码更新具有亚特兰大位置的所有用户的GMLocation Can someone help me figure out why it does not work?有人可以帮我弄清楚为什么它不起作用吗?

Set-ADUser expects you to use -Replace <hashtable> or -Add <hashtable> : Set-ADUser希望您使用-Replace <hashtable>-Add <hashtable>

Get-ADUser -Filter "l -like 'Atlanta'" -SearchBase 'OU=atl,DC=ego,DC=ga,DC=com' |
Set-ADUser -Replace @{ GMLocation = 'ATL' }

You're also using -Filter "l -like 'Atlanta'" but no wildcards for -like , if you're looking for an exact match of Atlanta then use -eq for equality: -Filter "l -eq 'Atlanta'" .您还使用-Filter "l -like 'Atlanta'"但没有通配符-like ,如果您正在寻找Atlanta的精确匹配,则使用-eq表示相等: -Filter "l -eq 'Atlanta'" . If instead, you're looking for partial match you should use -like but add wildcards ( * ) : -Filter "l -like '*Atlanta*'" .相反,如果您正在寻找部分匹配,您应该使用-like但添加通配符 ( * )-Filter "l -like '*Atlanta*'"

If you want to add error handling to your code, you can use a try { } catch { } statement:如果要在代码中添加错误处理,可以使用try { } catch { }语句:

$ErrorActionPreference = 'Stop'

$param = @{
    Properties = 'GMLocation'
    SearchBase = 'OU=atl,DC=ego,DC=ga,DC=com'
    Filter = "l -like 'Atlanta'"
}
Get-ADUser @param | ForEach-Object {
    $adusrSplat = @{ Identity = $_ }
    if($_.GMLocation) {
        # If the user already have this Attribute Set
        # Replace it
        $adusrSplat['Replace'] = @{ GMLocation = 'ATL' }
    }
    else {
        # Else, Add this new Attribute for the User
        $adusrSplat['Add'] = @{ GMLocation = 'ATL' }
    }

    try {
        Set-ADUser @adusrSplat
    }
    catch {
        Write-Warning $_.Exception.Message
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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