I am able to connect to domain controller via below scrip remotely, but after that i want to run disable-ADAccount ,Get-ADUser command but I am getting error

Get-ADUser : The server has rejected the client credentials.

How can I call active directory command in single script file?

$password = ConvertTo-SecureString "Password" -AsPlainText -Force
$cred= New-Object System.Management.Automation.PSCredential ("Lab\dcadmin", $password)
Enter-PSSession -ComputerName dc-01 -Port 5985 -Credential $cred

Try this: On a computer joined to the domain in question, right-click on the Powershell Start Menu icon or shortcut and choose "Run as administrator". Enter the credentials of a Domain Administrator account. Then try the Powershell commands. They should run in the context of a Domain Administrator account and you should have no authorization problems.

Note, if you are logged on to the domain-joined computer with a local admin account, this might not work, because when you choose "Run as administrator" it will run it as a local administrator without prompting for credentials. Either log on to the computer as a user who is not an administrator or as a Domain Administrator.

  • :) Big thanks to you ..its working. can i hard-code this setting? – kamlesh bhatt Jun 22 '15 at 19:14
  • I'm not aware of a way to code privilege elevation - which doesn't mean it's not possible, I just don't know. If you are scheduling this script or not running it manually in some way, then the scheduler or application that runs the script should have a way to elevate the shell. Are you aware of the runas command? That might help. – Todd Wilcox Jun 22 '15 at 19:16
  • 1
    i got your point i can do this :) – kamlesh bhatt Jun 22 '15 at 19:19
  • I love it when a plan comes together. – Todd Wilcox Jun 22 '15 at 19:26
  • Intermediate step: $Cred = Get-Credential ; Enter-PSSession -ComputerName 'dc-01' -Port 5985 -Credential $Cred # Just to see if it is the setting of credentials which goes wrong. – ErikE Jun 22 '15 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.