• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 14468
  • Last Modified:

Remove/rejoin computer to domain via Powershell script?

Hi,

I need to revert to an ESXI vmware snapshot of a vm all the time.
Regularely I get the message "trust relationship between this workstation and the domain failed".
I need to remove it from the domain, reboot add it to the domain reboot what is ennoying.

Please advise howto do this in an easier way, via Powershell or other?

J.
0
janhoedt
Asked:
janhoedt
3 Solutions
 
Peter HutchisonSenior Network Systems SpecialistCommented:
Use the Add-Computer command:
e.g.
$name = $Env:ComputerName
$cred = Get-Credential
Add-Computer -WorkgroupName WORKGROUP -ComputerName $name
Add-Computer -Domain domain-name -Credential $cred -ComputerName $name
0
 
janhoedtAuthor Commented:
Ok, but howto do this remotely?
0
 
GG VPCommented:
You can use Remove-Computer to remove the computer from domain,

http://technet.microsoft.com/en-us/library/hh849816%28v=wps.620%29.aspx

Parameter Set: Remote
Remove-Computer -UnjoinDomainCredential <PSCredential> [-ComputerName <String[]> ] [-Force] [-LocalCredential <PSCredential> ] [-PassThru] [-Restart] [-Workgroup <String> ] [ <CommonParameters>]

PS C:\> Remove-Computer -ComputerName Server01 -UnjoinDomainCredential Domain01\Admin01 -Workgroup Local -Restart -Force
0
 
oBdACommented:
Easier solution to prevent this from happening in the first place: create a GPO applied only to this VM (by security filtering or a separate GPO), in which you enable the "Domain member: Disable machine account password changes" policy (http://technet.microsoft.com/en-us/library/cc785826(v=ws.10).aspx).
Restore the snapshot, re-join the restored machine one last time to the domain (btw: you can save yourself the first reboot; remove from domain, close the dialogs, re-open the dialog, rejoin, reboot), open rsop.msc to make sure the policy is applied, then take a new base snapshot.
0
 
GG VPCommented:
You can use Add-Computer to join the computer to domain,

http://technet.microsoft.com/en-us/library/hh849798(v=wps.620).aspx

Syntax:

Parameter Set: Domain
Add-Computer [-DomainName] <String> -Credential <PSCredential> [-ComputerName <String[]> ] [-Force] [-LocalCredential <PSCredential> ] [-NewName <String> ] [-Options <JoinOptions> ] [-OUPath <String> ] [-PassThru] [-Restart] [-Server <String> ] [-UnjoinDomainCredential <PSCredential> ] [-Unsecure] [-Confirm] [-WhatIf] [ <CommonParameters>]

Open in new window


Example:

This command adds the Server01 computer to the Domain02 domain. It uses the LocalCredential parameter to specify a user account that has permission to connect to the Server01 computer

PS C:\> Add-Computer -ComputerName Server01 -LocalCredential Server01\Admin01 -DomainName Domain02 -Credential Domain02\Admin02 -Restart -Force

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now