How could I check the absolute location of AC object through powershell

I have like 500 windows servers need use WSUS server to update.

I want to make a spread sheet to get all servers' location inside AC. How could I do it though powershell. I tried to use Get-aDObject command to get one but get an error.


PS C:\Windows\system32> Get-ADObject KFIXECQA
Get-ADObject : Cannot find an object with identity: 'KFIXECQA' under: 'DC=na,DC=kfy,DC=com'.
At line:1 char:1
+ Get-ADObject KFIXECQA
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (KFIXECQA:ADObject) [Get-ADObject], ADIdentityNotFoundException
    + FullyQualifiedErrorId : Cannot find an object with identity: 'KFIXECQA' under: 'DC=na,DC=kfy,DC=com'.,Microsoft.
   ActiveDirectory.Management.Commands.GetADObject

PS C:\Windows\system32> Get-ADObject KFI-WEBTEST-02
Get-ADObject : Cannot find an object with identity: 'KFI-WEBTEST-02' under: 'DC=na,DC=kfy,DC=com'.
At line:1 char:1
+ Get-ADObject KFI-WEBTEST-02
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (KFI-WEBTEST-02:ADObject) [Get-ADObject], ADIdentityNotFoundException
    + FullyQualifiedErrorId : Cannot find an object with identity: 'KFI-WEBTEST-02' under: 'DC=na,DC=kfy,DC=com'.,Micr
   osoft.ActiveDirectory.Management.Commands.GetADObject

PS C:\Windows\system32> Get-ADObject NA-EXTDB-02
Get-ADObject : Cannot find an object with identity: 'NA-EXTDB-02' under: 'DC=na,DC=kfy,DC=com'.
At line:1 char:1
+ Get-ADObject NA-EXTDB-02
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (NA-EXTDB-02:ADObject) [Get-ADObject], ADIdentityNotFoundException
    + FullyQualifiedErrorId : Cannot find an object with identity: 'NA-EXTDB-02' under: 'DC=na,DC=kfy,DC=com'.,Microso
   ft.ActiveDirectory.Management.Commands.GetADObject

PS C:\Windows\system32>
Jason YuAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
If you read the help for Get-ADObject, you'll see that the -identity parameter for that cmdlet takes a DN or GUID.
Since you're only interested in computers, use Get-ADComputer.  It's identity parameter will take will take a DN, GUID, SID, or samAccountName.  However, in either case, if you're looking to retrieve many machines, you'll use the -filter parameter.  For example:
Get-ADComputer -filter * | Select DistinguishedName

Open in new window

Jason YuAuthor Commented:
It gave me output like below, this is confusing, what does -filter do?


PS C:\Windows\system32> Get-ADComputer -filter * | Select LAX-QASVCS-02

LAX-QASVCS-02
-------------
{}
{}
{}
{}
{}
{}
{}
{}
footechCommented:
You should run the command exactly as I posted.  It'll give you a list of all the computers' DNs.  I suggest reading the help for Get-ADComputer, it'll describe the -filter parameter.  Simply put "*" will return all, while something more complex will filter your results according to the criteria you set.
Cloud as a Security Delivery Platform for MSSPs

Every Managed Security Service Provider (MSSP) needs a platform to deliver effective and efficient security-as-a-service to their customers. Scale, elasticity and profitability are a few of the many features that a Cloud platform offers. View our on-demand webinar to learn more!

Jason YuAuthor Commented:
Thanks a lot, then how to pipe the result to a txt file?
Jason YuAuthor Commented:
I got it:   Get-ADComputer -filter * | Select DistinguishedName |out-file c:\test.txt


If I want to grep a specific computer from this command, how could I add filter condition?
Jason YuAuthor Commented:
how to read help for  "Get-ADComputer" command?

Thanks in advance.
footechCommented:
Microsoft has pages for all the cmdlets, or you can just run
get-help get-adcomputer -full

If you just want a single computer, here's a couple options
Get-ADComputer computername
Get-ADComputer -filter {name -eq "computername"}
yo_beeDirector of Information TechnologyCommented:
To add to this list you will want to filter for Operating System
 
[code]Get-adcomputer -filter {operatingsystem -like "*server*"} | select Name | export-csv filename -notypeinformation[/code]
Jason YuAuthor Commented:
Thanks for the replies. I got the following error:

PS C:\PowerShell Scripts> get-ADComputer LAX-QASITES-02
get-ADComputer : Either the target name is incorrect or the server has rejected the client credentials.
At line:1 char:1
+ get-ADComputer LAX-QASITES-02
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (LAX-QASITES-02:ADComputer) [Get-ADComputer], AuthenticationException

Open in new window

   + FullyQualifiedErrorId : Either the target name is incorrect or the server has rejected the client credentials.,M
   icrosoft.ActiveDirectory.Management.Commands.GetADComputer

PS C:\PowerShell Scripts> get-ADComputer LAX-QASITES-02 | select distinguishedname
get-ADComputer : Either the target name is incorrect or the server has rejected the client credentials.
At line:1 char:1
+ get-ADComputer LAX-QASITES-02 | select distinguishedname
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (LAX-QASITES-02:ADComputer) [Get-ADComputer], AuthenticationException
    + FullyQualifiedErrorId : Either the target name is incorrect or the server has rejected the client credentials.,M
   icrosoft.ActiveDirectory.Management.Commands.GetADComputer

PS C:\PowerShell Scripts> Get-adcomputer -filter {name -eq  LAX-QASITES-02} | select Name
Get-adcomputer : Error parsing query: 'name -eq  LAX-QASITES-02' Error Message: 'syntax error' at position: '11'.
At line:1 char:1
+ Get-adcomputer -filter {name -eq  LAX-QASITES-02} | select Name
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Get-ADComputer], ADFilterParsingException
    + FullyQualifiedErrorId : Error parsing query: 'name -eq  LAX-QASITES-02' Error Message: 'syntax error' at positio
   n: '11'.,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
Jason YuAuthor Commented:
Yobee, the command doesn't work.


PS C:\Windows\system32> Get-adcomputer -filter {operatingsystem -like "*server*"} | select Name | export-csv filename c:
\testfile.txt
Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "c:\testfile.txt" to type "System.Char". Error:
"String must be exactly one character long."
At line:1 char:95
+ ... t-csv filename c:\testfile.txt
+                    ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand

PS C:\Windows\system32>
footechCommented:
Never seen that first error.
Get-adcomputer -filter {name -eq "LAX-QASITES-02"} | select Name

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jason YuAuthor Commented:
This is good, it is working now.

Thank you very much.
Jason YuAuthor Commented:
One last question, if I want to filter several computers out, how can I do it.

I want to add a "or" condition to the query.

PS C:\Windows\system32> Get-adcomputer -filter {name -eq "LAXDEVFS" || name -eq "GSESQLDEV"} | select Name, Distinguishe
dName
At line:1 char:45
+ Get-adcomputer -filter {name -eq "LAXDEVFS" || name -eq "GSESQLDEV"} | select Na ...
+                                             ~~
The token '||' is not a valid statement separator in this version.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvalidEndOfLine

PS C:\Windows\system32>
yo_beeDirector of Information TechnologyCommented:
Try without the export-csv

Get-adcomputer -filter {operatingsystem -like "*server*"} | select Name 

Open in new window

yo_beeDirector of Information TechnologyCommented:
Get-adcomputer -filter {name -eq "LAXDEVFS" || name -eq "GSESQLDEV"} | select Name, DistinguishedName

Open in new window


You have an incorrect syntax || can not be used. You cannot double pipe like you did.
if you are trying to find both machines you will need to do something like this.

Get-adcomputer -filter {name -eq "LAXDEVFS" -and name -eq "GSESQLDEV"} | select Name, DistinguishedName

Open in new window

yo_beeDirector of Information TechnologyCommented:
In regards to my little script I just tested the one below and it worked.
Note you need import the activedirectory module or run the Active Directory Powershell shortcut.
I would also recommend changing the .txt to .csv.
Import-module Activedirectory
Get-adcomputer -filter {operatingsystem -like "*server*"} | select Name,DistinguishedName  | Export-Csv test.txt -NoTypeInformation

Open in new window

footechCommented:
You would actually use the -or operator for the command below.  :)
Get-adcomputer -filter {name -eq "LAXDEVFS" -or name -eq "GSESQLDEV"}

Open in new window

yo_beeDirector of Information TechnologyCommented:
I have see the -or operator give an adverse results ,  but I would try both.
Jason YuAuthor Commented:
got it, it works now.
Jason YuAuthor Commented:
Thank you guys very much. I appreciate your help, I will spend more time on powershell and try to be better.

have a nice night.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2003

From novice to tech pro — start learning today.