-1

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           
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nanji Mange
  • 2,155
  • 4
  • 29
  • 63

1 Answers1

0

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://learn.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
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • Thank you. Can you please provide me complete line how to I write server details. Actually server has URL, username and password. What should be sequence and do I need to separate it? Here is example of `server details: URL: 10.556.886.55, username: derdlgdeo, password: maron97` Can you please provide me example by putting my details in your answer? – Nanji Mange Aug 10 '18 at 10:39
  • @NanjiMange - In my knowledge, any server can't have the IP=`10.556.886.55`; please check the same. Also, you should never reveal any credentials to anyone. Please delete your previous comment! Regarding the exact command, please refer this link: https://social.technet.microsoft.com/Forums/en-US/e911fa82-061b-4a4d-bee5-c24eae4d8077/getaduser-credential-passthrough?forum=winserverpowershell. – Am_I_Helpful Aug 10 '18 at 10:53
  • @NanjiMange - As mentioned in that link, you should do: `$secpasswd = ConvertTo-SecureString "maron97" -AsPlainText -Force`, `$mycreds = New-Object System.Management.Automation.PSCredential ("derdlgdeo", $secpasswd)` , `Get-ADUser -Filter {SamAccountName -eq $Username} -Server 10.556.886.55 -Credential $mycreds` – Am_I_Helpful Aug 10 '18 at 10:55
  • Thanks. I have tried. I have a question. Previously, I was executing PowerShell script from one of my server. Now, I am trying to execute the script from my local system(windows 10- which doesn't have Active Directory) and I am getting this error `Import-Module : The specified module 'ActiveDirectory' was not loaded because no valid module file was found in any module directory.` Please guide – Nanji Mange Aug 10 '18 at 11:30
  • So, is that anything like I must execute PowerShell Script from Server? I know this are silly question but I am new here for PowerShell Script. – Nanji Mange Aug 10 '18 at 11:34
  • @NanjiMange - Please refer this link to solve your problem: https://stackoverflow.com/questions/19182497/import-module-the-specified-module-activedirectory-was-not-loaded-because-no; hope it helps. – Am_I_Helpful Aug 10 '18 at 12:15
  • @NanjiMange - You may like to upvote the answer as well, if it helped you. See how to upvote an answer -> https://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow You may like to accept the answer as well, if it helped you. See how to accept an answer -> https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work. Cheers. – Am_I_Helpful Aug 16 '18 at 09:43