question

SutharAnil-8229 avatar image
0 Votes"
SutharAnil-8229 asked AndreasBaumgarten commented

Get ad computers using powershell

I'm using below statement to get server lists from AD using windows server filter. I've two different domain "abc.com" and another "anil.abc.com". I've an access to read information from both the domains, when i run the script i get data only from the "abc.com" , but cannot get the server lists from "anil.abc.com". Can you please help what i can use to get a data from both using a single script ? Thanks in advance.


My script


$ScriptPath = Get-Location
Get-ADComputer -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt

windows-server-powershellwindows-server-2016
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten commented

Hi @SutharAnil-8229 ,
Let's give it another try please:

  $ScriptPath = Get-Location
  Get-ADComputer -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt
  Get-ADComputer -Server <yourDC of anil.abc.com> -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt -Append


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Dear Andreas, Thanks you so much.. it worked as below, I got the data from both the domains now. Appreciate your support in this, thank you so much.

This will get the details from my current dc.
Get-ADComputer -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt

For a different DC i have to update as below, and in last -append to add hostnames in to the text file.
Get-ADComputer -Server anil.abc.com -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt -Append

0 Votes 0 ·

Hi @SutharAnil-8229 ,

thanks for the feedback.

Are there any additional questions to this topic?

If you found the answer helpful, it would be great if you please mark it "Accept as answer". This will help others to find answers in Q&A

----------
Regards
Andreas Baumgarten

0 Votes 0 ·
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered SutharAnil-8229 commented

Hi @SutharAnil-8229 ,

please give this a try:

 $ScriptPath = Get-Location
 Get-ADComputer -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -SearchBase "DC=abc,DC=com" -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt
 Get-ADComputer -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -SearchBase "DC=anil,DC=abc,DC=com" -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt -Append


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

There is an error below when i run 3 lines together, please find below.
When i run line 2 alone it runs without any error but not output in the text file.
Get-ADComputer -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -SearchBase "DC=abc,DC=com" -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt

105660-image.png


0 Votes 0 ·
image.png (35.9 KiB)
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered SutharAnil-8229 edited

Hi @SutharAnil-8229 ,

could you please try this:

 $ScriptPath = Get-Location
 Get-ADComputer -Server <yourDC of abc.com domain> -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -SearchBase "DC=abc,DC=com" -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt
 Get-ADComputer -Server <yourDC of anil.abc.com> -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -SearchBase "DC=anil,DC=abc,DC=com" -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt -Append


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This time no error but the file is still blank :(, i cannot see any computer names yet.

0 Votes 0 ·