Remote Join/ReJoin a PC to Domain

0

First time post but long time lurker. I am trying to use my domain pc to join/rejoin other pcs that have fallen off the domain. I have two test computers I am working with and it doesn't work with either. One is join and working on the domain and the other has fallen off the domain. Both are networked hardline to the domain for this process. I keep running into this issue when running my script. Can someone possibly point me in the right direction to resolve my error? THANKS!!

Remove-Computer : Cannot establish the WMI connection to the computer 'JoinTest' with the following error message: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

$CorpDomain = "Domain.com\"
$ComputerName = Read-Host -Prompt 'Target Computer Name?'
$AdminCreds = Read-Host -Prompt 'Admin account username'
$DomainAdmin = $CorpDomain+$AdminCreds
$LocalCreds = $ComputerName+"\Administrator"

Remove-Computer -ComputerName $ComputerName -UnjoinDomaincredential 
$DomainAdmin  -PassThru -Verbose -Restart


Add-Computer -ComputerName $ComputerName -LocalCredential $LocalCreds - 
DomainName "Domain.com" -Credential $DomainAdmin -Force -Verbose -Restart

#Allow time for reboot
Start-Sleep -Seconds 30

Get-ADComputer $ComputerName -Properties *
powershell
asked on Stack Overflow Nov 7, 2018 by Seabear

1 Answer

0

If the PC's are not already in the domain but all have PS remoting enabled and share the same local Administrator username/password, you can use:

$localUser = "Administrator"
$localPassword = "YourPasswordHere"
$localCreds = New-Object -TypeName System.Management.Automation.PSCredential -Argumentlist $localUser, $localPassword

$domainUser = "Domain\Username"
$domainPasswrod = "YourPasswordHere"
$domainCreds = New-Object -TypeName System.Management.Automation.PSCredential -Argumentlist $domainUser, $domainPassword

[array]$computers = @("pc01", "pc02")

$scriptBlock = {
    Add-Computer -ComputerName $env:COMPUTERNAME -LocalCredential $args[0] -DomainName "Domain.com" -Credential $args[1] -Force -Restart
}
Invoke-Command -ComputerName $computers -ScriptBlock $scriptBlock -ArgumentList $localCreds, $domainCreds

You can use PowerShell remoting with commands like Invoke-Command where you specify the remote computer to execute the command you want (i.e. Add-Computer). Obviously you need PS remoting enabled (catch 22) before you can use this method.

answered on Stack Overflow Nov 7, 2018 by Jason Shave - MSFT

User contributions licensed under CC BY-SA 3.0