无法使用 powershell direct 将服务器加入域

can't join a server to a domain using powershell direct

我想自动将服务器添加到实验室环境,当我尝试使用 power-shell direct 将服务器加入域时遇到问题:

Enter-PSSession -VMName S2D2 -Credential $cred
Add-Computer -DomainName "yp.org" -Credential  yp.org\administrator

给我这个错误:

Add-Computer : Computer 'WIN-ORGQD2DH4PU' failed to join domain 'yp.org' from its current workgroup 'WORKGROUP' with 
following error message: The specified domain either does not exist or could not be contacted.
    + CategoryInfo          : OperationStopped: (WIN-ORGQD2DH4PU:String) [Add-Computer], InvalidOperationException
    + FullyQualifiedErrorId : FailToJoinDomainFromWorkgroup,Microsoft.PowerShell.Commands.AddComputerCommand

ps:dns 正在运行:

    ping -n 2 ADDC  

Pinging ADDC [192.168.3.10] with 32 bytes of data:
Reply from 192.168.3.10: bytes=32 time=1ms TTL=128
Reply from 192.168.3.10: bytes=32 time<1ms TTL=128
来自 Ignite 2015 的

This demo 意味着您应该等待 DNS。

while (!(Test-Connection -Computername <# DNS IP address #> -BufferSize 16 -Count 1 -Quiet -ea SilentlyContinue)) {sleep -seconds 1}
Add-Computer 

在这里,我尝试重写该代码以供通用使用。

$VMName = "Old Virtual Machine"
$localCred = new-object -typename System.Management.Automation.PSCredential `
             -argumentlist "Administrator", (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force)
$domainCred = new-object -typename System.Management.Automation.PSCredential `
              -argumentlist "Ignite\Administrator", (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force)
$DnsIpAddress = "10.100.7.1"
$DomainName = "Ignite.demo"


Invoke-Command -VMName $VMName -Credential $localCred -ScriptBlock {
         param($VMName, $domainCred, $DnsIpAddress, $DomainName)
         Write-Output "[$($VMName)]:: Joining domain as `"$($env:computername)`""
         while (!(Test-Connection -Computername $DnsIpAddress -BufferSize 16 -Count 1 -Quiet -ea SilentlyContinue)) {sleep -seconds 1}
         Add-Computer -DomainName $DomainName -Credential $domainCred
         } -ArgumentList $VMName, $domainCred, $DnsIpAddress, $DomainName
}

$localCred 似乎是虚拟机的本地管理员(您将以其身份登录)。

$domainCred 是可以将计算机添加到域中的人。

您可能还会发现 this article 有用。

原始代码归功于 Sarah Cooley。

DNS 故障排除

您可以看到 this code 使用了哪些 DNS 服务器:

$net = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName localhost -Filter 'IPEnabled=True'

$props = @{
    DNSHostName = $net.DNSHostName
    DNSServerSearchOrder = $net.DNSServerSearchOrder -join ','
    Description = $net.Description
    IPAddress = $net.IPAddress -join ','
}

New-Object PsObject -Property $props | format-list

您可以通过 nslookup or dig 了解有关虚拟机 DNS 环境的更多信息。

如果您想提出有关 DNS 问题的新问题,可以尝试 Server Fault

我的问题是我的环境中有两个 DNS 域,我输入了错误的 IP DNS 服务器。现在开始工作