Add-Computer fails by not finding the domain
<p>I'm working an a script to autoconfigure my VMs. The idea is, that the VM automatic configures its netadapter and the joins a domain via default credentials. It's supposed to work with 2012r2, so PoShv4 is in use.</p>

<p>It works perfect until it comes to the point of working with <code>Add-Computer</code></p>

<pre><code>$vmName = (Get-ItemProperty –path 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters').VirtualMachineName
$vmName = $vmName -replace '\w+\s+(.+)' , '$1'

$ip = $vmName -replace '\D+(\d+)' , '$1'

switch -Regex ($vmName) {
    'dc' {$Ip = '10.18.0.{0}0' -f $ip}
    'srv' {$Ip = '10.18.1.{0}0' -f $ip}
    'CL' {$Ip = '10.18.2.{0}0' -f $ip}
}

#Configure IP-Settings
$netadapter = Get-NetAdapter -Name Ethernet

$netadapter | Set-NetIPInterface -DHCP Disabled | Out-Null

$netadapter | New-NetIPAddress -IPAddress $ip -PrefixLength 16 -DefaultGateway 10.18.0.10 | Out-Null

Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses 10.18.0.10 | Out-Null

#Create Credentials for Domainjoin
$username = 'domain\administrator'
$password = ConvertTo-SecureString 'Passw0rd' -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

Start-Sleep -Seconds 5

#Check if Domainjoin possible
if ($vmName -eq 'DC1') 
{
    Install-WindowsFeature AD-Domain-services -IncludeAllSubFeature -IncludeManagementTools
    Rename-Computer $vmName -Restart
}
elseif ($vmName -match 'DC')
{
    Install-WindowsFeature AD-Domain-services -IncludeAllSubFeature -IncludeManagementTools
 -DomainCredential $cred -NewName $vmName -Restart
}
else 
{
    $null = [net.dns]::Resolve('domain.tld')
    Add-Computer -DomainName 'domain.tld' -DomainCredential $cred -NewName $vmName -Restart -Server 'dc1.domain.tld'
}
</code></pre>

<p><code>Add-Computer</code> fails with the following error:</p>

<pre><code>Add-Computer : Der angegebene Servername "dc1.domain.tld" kann nicht aufgelöst werden.
In C:\VMAutoConfig.ps1:42 Zeichen:5
+     Add-Computer -DomainName 'domain.tld' -DomainCredential $cred -NewName $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (dc1.domain.tld:String) [Add-Computer], InvalidOperationException
    + FullyQualifiedErrorId : AddressResolutionException,Microsoft.PowerShell.Commands.AddComputerCommand
</code></pre>

<p>Any known issues with that?</p>

<p>Timedelay of up to 5 secs between netadapter config and <code>Add-Computer</code> does not help at all.</p>

<p>If I run the script a second time, everthing's ok and works.</p>