Mr. Chand
Mr. Chand

Reputation: 11

how can I query to multiple domain's computer account in powershell

I have bulk computer list in my CSV file with header of servers.

All these servers are different domains under single forest.

I need to get all these server attribute details like name and operating system, status.

I have created below script but that's not working..

Any help would be appreciated.

Import-Module ActiveDirectory

# For each domain in the forest

$domains = (Get-ADForest).Domains
$servers = Import-Csv "D:\temp\computer.csv" | % {$_.server}

foreach ($server in $servers)
{
  foreach ($domain in $domains)
  {
    Get-ADComputer $server -Server $domain -Properties operatingsystem | select name,operatingsystem 
  }
}
#

HI

I have added my script like below:

#
Import-Module ActiveDirectory



# For each domain in the forest



$domains = (Get-ADForest).Domains

$servers = Import-Csv "D:\temp\computers.csv" | % {$_.server}

$DomainController = "DC2:3268"  #  3268 is the commen port of global catalogue

$SearchBase = ((Get-ADDomain (Get-ADForest).RootDomain).DistinguishedName)

foreach ($server in $servers)

{

foreach ($domain in $domains)

{

Get-ADComputer $server -Server $DomainController -SearchBase $SearchBase -Properties operatingsystem | select name,operatingsystem

}
}
#

Getting below error now and also I have specified only samaccountname of computer not FQDS this time..

#### Error
Get-ADComputer : A positional parameter cannot be found that accepts argument 'DPS002'.
At D:\temp\search_computer.ps1:34 char:5
+     Get-ADComputer $server -Server $DomainController -SearchBase $SearchBase -Pr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADComputer], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

Upvotes: 1

Views: 6282

Answers (1)

Buxmaniak
Buxmaniak

Reputation: 478

You have to run your request against the global catalogue to find AD objects in the whole AD forest.

  1. You need a server witch is supporting global catalogue. Choose one which is next to you.

    Import-Module ActiveDirectory
    @((Get-ADForest).GlobalCatalogs) | Sort-Object
    
  2. Your script, a little bit modified

    Import-Module ActiveDirectory
    $DomainController = "ServerFromStep1:3268"  #  3268 is the commen port of global catalogue
    $SearchBase       = ((Get-ADDomain (Get-ADForest).RootDomain).DistinguishedName)
    
    foreach ($server in $servers)
    {
        Get-ADComputer $server -Server $DomainController -SearchBase $SearchBase -Properties operatingsystem | select name,operatingsystem
    }
    
  3. The same as 2 but able to processed FQDNs from server list.

    foreach ($serverFQDN in $servers)
    {
        $Local:ServerName = (($serverFQDN -replace "\..*$", "").Trim())
        if ($ServerName) {
            Get-ADComputer $ServerName -Server $DomainController -SearchBase $SearchBase -Properties operatingsystem | select name,operatingsystem
        }
    }
    

Upvotes: 2

Related Questions