使用Powershell重新加入域的脚本

时间:2011-11-28 19:58:22

标签: powershell dns vmware

抱歉我的英文。

我在运行Windows 7的VMWare ESXi上安装了虚拟机。所有这些计算机都在域中,但有时它们会离开域,所以我需要重新加入它。我写了一个简单的脚本来做到这一点:

$credential = New-Object System.Management.Automation.PsCredential("mydomain\user", (ConvertTo-SecureString "password" -AsPlainText -Force))
Add-Computer -WorkGroupName TEMP -Credential $credential
Add-Computer -DomainName "mydomain" -Credential $credential
Restart-Computer

但它不起作用;我有这个错误:

Add-Computer : This command cannot be executed on target computer('') due to following error: An existing connection was forcibly closed by the remote host.
At C:\Tools\Re-Join.ps1:2 char:13
+ Add-Computer <<<<  -WorkGroupName TEMP -Credential $credential
+ CategoryInfo          : InvalidOperation: (:) [Add-Computer], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.AddComputerCommand<br>

如果我手动加入'workgroup'然后运行脚本一切正常。

2 个答案:

答案 0 :(得分:1)

这是我用来加入域名的脚本:

$computer = Get-WmiObject Win32_ComputerSystem
$computer.JoinDomainOrWorkGroup("cloud.com" , "PASSWORD HERE", "USER", $null, 3)
Restart-Computer -Force

答案 1 :(得分:1)

您可以在加入之前添加取消加入,在重新加入域之前无需重新启动:

$computer.UnJoinDomainOrWorkGroup("PASSWORD HERE", "USER", 0)