How to add/update user data to LDAP Active Directo

2019-03-06 17:30发布

I want to add/update data of Active Directory located on another server. I have server details but I don't know how to do it. However, I know how to add/update data if I run PowerShell Script from same server.

Here is my code which work if I add/update data by PowerShell Script located to same server. Can anybody please suggest me how can I add/update data to Active Directory located on another server?

Code

# Import active directory module for running AD cmdlets
Import-Module activedirectory

#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv C:\it\powershell_create_bulk_users\bulk_users1_quote.csv

foreach ($User in $ADUsers)
{
    $Username   = $User.username
    $Password   = $User.password
    $Firstname  = $User.firstname
    $Lastname   = $User.lastname
    $OU         = $User.ou #This field refers to the OU the user account is to be created in
    $Password = $User.Password

    if (Get-ADUser -F {SamAccountName -eq $Username})
    {
         Write-Warning "A user account with username $Username already exist in Active Directory."
    }
    else
    {
        New-ADUser `
            -SamAccountName $Username `
            -UserPrincipalName "$Username" `
            -Name "$Firstname $Lastname" `
            -Path $OU `
            -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $True           
    }
}

1条回答
Ridiculous、
2楼-- · 2019-03-06 17:39

You need to include the -Server <string> parameter for connecting to another server before creating / validating the user.

Also, I think you meant -Filter as the parameter with Get-ADUser cmdlet, and not -F.

-Server

Specifies the Active Directory Domain Services instance to connect to, by providing one of the following values for a corresponding domain name or directory server. The service may be any of the following: Active Directory Lightweight Domain Services, Active Directory Domain Services or Active Directory Snapshot instance. ...

Get-ADUser -Filter {SamAccountName -eq $Username} -Server a.b.c.d ...
# reference from https://docs.microsoft.com/en-us/powershell/module/addsadministration/get-aduser?view=win10-ps

New-ADUser ... -Server a.b.c.d ... 
# reference from https://technet.microsoft.com/fr-fr/library/hh852238(v=wps.630).aspx
查看更多
登录 后发表回答