Forum Discussion
Disjoin and join windows machines remotely
Hi Najwan975,
the "Access is denied" error when attempting to restart the computer remotely can be caused by a number of factors.
The account in use to execute the script might lack the required permissions to restart the computer. Ensure the account holds administrative rights on the remote computer.
The firewall on the remote computer might be blocking the request. Temporarily disable the firewall to verify if this resolves the issue.
The remote settings on the computer might not be configured to permit remote restarts. Check this in the System Properties of the remote computer.
The registry entry "LocalAccountTokenFilterPolicy" dictates how local accounts are recognized during network logons. If set to 1, network logons like those used by the "Remove-Computer" cmdlet with a local account are authorized. If the value doesn't exist or is set to 0, network logons using local accounts are prohibited. Your script already sets this value to 1, but double-check it's correct.
As an alternative, use the "Enter-PSSession" cmdlet to establish a remote session with the computer and execute the commands there. Here's an example:
$Session = New-PSSession -ComputerName targetcomputer.domain.local -credentials domain\\username
Invoke-Command -Session $Session -ScriptBlock {
Remove-Computer -ComputerName "Computer01" -UnjoinDomaincredential "Domain01\\Admin01" -PassThru -Verbose -Restart
}
$Session = New-PSSession -ComputerName targetcomputer.domain.local -credentials domain\\username
Invoke-Command -Session $Session -ScriptBlock {
Add-Computer -ComputerName "Computer01" -LocalCredential "Computer01\\Administrator" -DomainName "Domain01" -Credential "Domain01\\Admin01" -Force -Verbose -Restart
}
This script establishes a remote session with the target computer and runs the "Remove-Computer" and "Add-Computer" cmdlets within that session.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)