Determine domain of the computer

I am looking for an output from the computerlist to determine if that computer exist in AD, but along with that I also need to pull out the Domain and the ping result.

Domain =$domain - This gives me a wrong result and not sure where to put in this. I am missing something simple, but not able to resolve it

$Computers= get-content "C:\temp\Computers.txt"
$data = @()
foreach ($computer in $Computers)
{
$DN=Foreach ($domain in $Domains)
{
try
{
Get-ADComputer -Identity $computer -Server $domain | select -expandproperty DistinguishedName

}

Catch
{out-null}
}

If ($DN) {
$data += [PSCustomObject]@{
Name = $computer
DN = $DN
Domain =$domain
}
}

else{
$data += [PSCustomObject]@{
Name = $computer
DN = "Doesnt exist in AD"
Domain = "NA"
}
}
}

$data |export-csv C:\temp\Oulist.csv -NoTypeInformation

The domain is contained in the DistinguishedName, but try changing you loop a bit like this and see if it works (untested):

$Domains = "Domain1', "Domain2"
$Computers= get-content "C:\temp\Computers.txt"

$results = foreach ($computer in $Computers){
    foreach ($domain in $Domains){

        $computer = Get-ADComputer -Filter {SamAccountName -eq $computer} -Server $domain | 
                    Select @{Name='DN';Expression={$_.DistinguishedName}},
                           Name,
                           @{Name='Domain';Expression={$domain}}
    }

    if (!$computer) {
        [PSCustomObject]@{
            Name   = $computer
            DN     = $null
            Domain = $null
        }
    }
}

$results

[quote quote=211977]The domain is contained in the DistinguishedName, but try changing you loop a bit like this and see if it works (untested):

PowerShell
23 lines
<textarea class="ace_text-input" wrap="off" autocorrect="off" autocapitalize="off" spellcheck="false" style="opacity: 0; height: 18px; width: 7px; left: 51px; top: 0px;"></textarea>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$Domains = "Domain1', "Domain2"
$Computers= get-content "C:\temp\Computers.txt"
$results = foreach ($computer in $Computers){
foreach ($domain in $Domains){
$computer = Get-ADComputer -Filter {SamAccountName -eq $computer} -Server $domain |
Select @{Name='DN';Expression={$_.DistinguishedName}},
Name,
@{Name='Domain';Expression={$domain}}
}
if (!$computer) {
[PSCustomObject]@{
Name = $computer
DN = $null
Domain = $null
}
}
}
$results
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]

Sorry for the late reply on this. thanks for your guidancce