添加计算机命令无法处理错误:RPC服务器不可用

时间:2018-05-23 16:08:30

标签: powershell dns

只是测试为我们的IT团队创建一个新的PS脚本,这将从域远程删除计算机,然后使用Add-Computer命令重新添加它。

3行代码是:

$Hostname = Read-Host "Please enter the computers hostname that you wish to remove and re-add to the domain"
Remove-Computer -ComputerName "$Hostname" -UnjoinDomainCredential "DOMAIN\$env:USERNAME" -PassThru -Verbose - Restart
Add-Computer -ComputerName "$Hostname" -LocalCredential "$Hostname\ukitadmin" -Credential "DOMAIN\$env:USERNAME" -DomainName "DOMAIN" -Force -Verbose -Restart

只是为了清除空气。 env:USERNAME命令正在使用我运行PS ISE的帐户,因此将成为我的管理员帐户。

任何帮助都会受到大力赞赏。

由于 布伦丹

1 个答案:

答案 0 :(得分:0)

使用第二个命令重新启动计算机,因此当第三个命令尝试运行时,计算机不可用。

您可以使用代码执行此操作,它应该等待计算机完成重新启动

我从remove-computer命令中删除了-restart

$Hostname = Read-Host "Please enter the computers hostname that you wish to remove and re-add to the domain"

Remove-Computer -ComputerName $Hostname -UnjoinDomainCredential "DOMAIN\$env:USERNAME" -PassThru -Verbose 

Restart-Computer -ComputerName $Hostname -Wait

Add-Computer -ComputerName $Hostname -LocalCredential "$Hostname\ukitadmin" -Credential "DOMAIN\$env:USERNAME" -DomainName "DOMAIN" -Force -Verbose -Restart