【问题标题】:Updating AD User Object Manager Attribute With Contact DN使用联系人 DN 更新 AD 用户对象管理器属性
【发布时间】:2017-12-09 16:36:51
【问题描述】:

合并后我有两个森林。有些人的经理住在对面的森林里。为了解决这个问题,我们在每个森林中为对面森林的所有用户设置了联系人。我正在尝试根据我在经理电子邮件地址上匹配的 csv 导入来更新多个用户的经理属性。我的脚本可以匹配经理联系人的 DN,但由于某种原因不会将其添加到广告 userobject manager 属性中,说明它找不到明确存在的对象的 DN。

如果我使用 ldap 过滤器运行一个简单的 get-adobject,它会返回经理联系人的 DN:

PS C:\temp> Get-ADObject -ldapfilter "(&(objectclass=contact)(name=$fname*)(name=*$lname))" -SearchBase "OU=station,OU=CONTACTS,DC=workplace,DC=COM" |select distinguishedname

distinguishedname                                        
-----------------                                        
CN=Nick Hill,OU=station,OU=Contacts,DC=workplace,DC=com

但是,尝试将此 DN 添加到用户管理器属性时,下面的脚本会出错。令人困惑的是,它声称无法找到的 DN 根据上面的命令清楚地存在。

以下脚本错误:

set-aduser : Identity info provided in the extended attribute: 'Manager' could not be resolved. Reason: 'Cannot find an object with identity: 'CN=Nick Hill,OU=station,OU=Contacts,DC=workplace,DC=com' under: 'DC=workplace,DC=com'.'.

$users = import-csv test1.csv

FOREACH ($user in $users)
 {
    $username = $user.UserName
    $employeeid = $user.employeeid
    $city = $user.city
    $country = $user.country
    $department = $user.department
    $division = $user.division
    $office = $user.location
    $state = $user.state
    $postalcode = $user.postal_code
    $manageremail = $user.manageremail
    $manager = get-aduser -f "mail -eq '$($manageremail)'"

    FUNCTION LocalManager 
    {
     get-aduser -f {mail -eq $username} |set-aduser -Manager $manager
    }

    FUNCTION RemoteManager 
    {
     $data = $manageremail.split("@")
     $name = $data[0]
     $namesplit = $name.split(".")
     $fname = $namesplit[0]
     $lname = $namesplit[1]
     $rmanager = Get-ADObject -SearchBase 'OU=station,OU=Contacts,DC=workplace,DC=com' -ldapfilter "(&(objectclass=contact)(name=$fname*)(name=*$lname))" 
     get-aduser -f {mail -eq $username} |set-aduser -Manager "$rmanager"
    }


    IF ($manager -eq $null)
    {
     RemoteManager
    }
    Else
    {
     Localmanager
    }

 }

【问题讨论】:

  • 将| Select-Object -ExpandProperty manager 添加到$manager 分配的末尾。

标签: powershell active-directory


【解决方案1】:

我在自己的脚本中遇到了类似的错误来处理跨域用户群。我已经导出了一些旧的 decom'd 用户帐户,并正在导入它们(带有适当的通用信息)以填充我们的测试/开发环境。

不幸的是,当我尝试将这些帐户创建为 AD 中的新用户并使用不同域的管理员时,我发现以下问题:

Set-ADUser : 服务器不愿意处理请求 在行:1 字符:1 + 设置 ADUser -Identity $user.SamAccountName -Manager $user.Manager -Ser ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (user.name:ADUser) [Set-ADUser], > ADInvalidOperationException + FullyQualifiedErrorId : > ActiveDirectoryServer:8245,Microsoft.ActiveDirectory.Management.Commands.SetADUser

因此,这是试图将用户设置为经理的 DN。

function create-testaccts {

[CmdletBinding()]
param(
    [Parameter(Mandatory=$True,Position=1)]
    [string] $rootPath ,

    [Parameter(Mandatory=$True,Position=2)]
    [string] $userList ,

    [Parameter(Mandatory=$True,Position=3)]
    [string] $pw ,

    [Parameter(Mandatory=$True,Position=4)]
    [string] $OU = $(throw "Please specify a query.")

    )

$newUsers = import-csv $userList
$password = $pw | ConvertTo-SecureString -AsPlainText -Force

foreach ($user in $newUsers){

    $profPath = $rootpath + $user.samaccountname

    try {
        write-host -fore Cyan "Creating the user profile path - $profPath"
        new-item $profPath -ItemType Directory -Force -ErrorAction stop | Out-Null
    }# END OF TRY
    Catch [System.Management.Automation.ActionPreferenceStopException] {
        write-host -fore Yellow "caught a StopExecution Exception - Home directory creation " 
        $error[0]
    }# END OF CATCH

    try {
        Write-Host -Fore Cyan "Creating the user object in AD -" $user.Name 
        # Name              - Name
        # Givenname         - Firstname
        # Surname           - Lastname
        # Password          - AccountPassword Specific to new-aduser
        # SamAccountName    - same in both command/attribute name used userlogon and samaccount
        # Manager           - same in both command/attribute name
        # ProfilePath       - same in both command/attribute name
        # HomeDirectory     - same in both command/attribute name
        # HomeDrive         - same in both command/attribute name
        # Enabled           - False - same in both command/attribute name
        # UserPrincipalName - same in both command/attribute name
        # Server

        $name = $user.Name

        New-ADUser -Name "$name" `
                    -GivenName $user.givenname `
                    -Surname $user.surname `
                    -DisplayName $user.displayname `
                    -SamAccountName $user.SamAccountName `
                    -Path $ou `
                    -AccountPassword $Password `
                    -ProfilePath $user.profilepath `
                    -HomeDirectory $user.HomeDirectory `
                    -HomeDrive $user.homedrive `
                    -Enabled $False `
                    -UserPrincipalName $user.UserPrincipalName `
                    -Server domain.local `
                    -Credential $creds `
                    -ErrorAction Stop

                    #-Manager $user.Manager `
    }# END OF TRY
    Catch [System.Management.Automation.ActionPreferenceStopException] {
        Write-Host -fore Yellow "caught a StopExecution Exception - Account Creation" 
        $error[0]
    }# END OF CATCH

}#END FOREACH NEW USERS
} #END OF FUNCTION (CREATE-TESTACCTS)

当我尝试将它与受信任的域一起使用时,由于在本地域中找不到管理器 DN,它会失败。我尝试了多种方法,但似乎无法找出它为什么这样做并且似乎不会链接。

但是我找到了一种解决方法,我可以在没有管理人员字段的情况下创建用户,然后使用以下链接/命令设置用户:

https://social.technet.microsoft.com/Forums/office/en-US/ade19ad5-ecfd-48af-987b-5958983676b6/active-directory-update-the-manager-field-when-the-manager-is-in-a-different-domain?forum=ITCG

Set-ADUser -Identity $ADUser_Domain1 -Credential $DomainAdmin_Domain1 -Server $Domain1 -Replace @{manager = $ManagerDN_Domain2}

这有效,我不知道为什么替换有效,但似乎需要通过凭据。我什至尝试过传递域 2 凭据。

总的来说,这非常令人困惑,我觉得如果本地会话凭据在域之间具有权限,它应该能够毫无问题地查找它。任何额外的帮助或解释都会非常有帮助!

【讨论】:

    猜你喜欢
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 2012-09-06
    • 2019-12-10
    • 2012-11-08
    • 1970-01-01
    相关资源
    最近更新 更多