Question

I'm trying to add a computer to a domain using the Add-Computer cmdlet, but the cmdlet fails half the time with the following error:

The requested name is valid, but no data of the requested type was found

I am nearly positive this is due to the fact that there are two NICs in the PC but only one of them is in use. If anyone has any idea how to get around this limitation with the path of least resistance it would be greatly appreciated.

Please keep in mind this is going in a script for a large deployment of PCs, so manually disabling the secondary NIC is not truly an option unless it is automated.

Here's the code I'm using:

# Convert our plaintext password to a secure string and create the domain join credential
$securePassword = ConvertTo-SecureString $txtPassword -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential( `
    ($txtDomainToJoin + "\" + $txtPrivilegedUser), $securePassword)

# Now we'll begin attempting to join        
try
{
    # Try to see if the PC is already on the domain
    try
    {
        $computer = Get-AdComputer -Credential $credential -identity $txtNewHostname
    }
    catch
    {
        # ErrorAction isn't configured for Get-ADComputer, therefore, we need to silently fail.
    }

    # If the PC is in fact on the domain, notify the user and error out
    if(($computer | Select DistinguishedName) -ne $null)
    {
        [System.Windows.MessageBox]::Show( `
            "Computer already exists in the domain.  Please remove the computer from AD before continuing or domain join will fail.", `
            "Warning", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Warning) | Out-Null
        return $false
    }

    # Finally, let's try to join the domain
    Add-Computer -Credential $credential -DomainName $txtIntendedDomain `
        -OUPath $cboOU -Force -ErrorAction Stop
}
catch
{
    # Notify the user if there is an issue
    [System.Windows.MessageBox]::Show("Error joining domain. $($_.Exception.Message)", "Error", `
        [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Error) | Out-Null
    return $false
}

[System.Windows.MessageBox]::Show("Domain joined successfully.  Press OK to reboot.", "Update success", `
    [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Information) | Out-Null
return $true

Thanks!

Was it helpful?

Solution

Since none of the PCs the script would be running on are going to have more than two NICs, here's how I solved the issue:

# Add-Computer will fail 50% of the time if there are more than 2 connected adapters enabled
$netAdapters = (Get-WmiObject Win32_NetworkAdapter | Where-Object {$_.NetConnectionStatus -eq 2})

# So disable one NIC
if($netAdapters.Count -gt 1)
{
    ($netAdapters | Select-Object -Last 1).Disable() | Out-Null
}

Simple enough for my use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top