1
votes

For a lab working on PowerShell, I have to target a specific OU and list the following information in a text file.

DistinguishedName
DNSHostName
Enabled
Name
ObjectClass
ObjectGUID
SamAccountName
SID
UserPrincipleName

I've found a ton of resources online on how to do this and continuously get an error no matter how I format it.

Here is my code:

$ou = 'OU=Testing,OU=Labs,OU=UWEC Computers DC=uwec, DC=edu'
$Computers = Get-ADComputer -Filter '*' -SearchBase $ou
$Computers | foreach {
    $_.DNSHostName
} | Out-File -Filepath "C:\Windows\Temp\Lab7.txt"

I continuously get this error no matter what syntax I use:

Get-ADComputer : The object name has bad syntax
At line:1 char:1
+ Get-ADComputer -Filter '*' -SearchBase 'OU=Testing,OU=Labs,OU=UWEC Co ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ADComputer], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
1

1 Answers

1
votes

The code you posted does not match the error you posted. However, the most likely reason for the error is a missing comma in your OU:

$ou = 'OU=Testing,OU=Labs,OU=UWEC Computers DC=uwec, DC=edu'
#                                          ^right here

Change that into

$ou = 'OU=Testing,OU=Labs,OU=UWEC Computers,DC=uwec, DC=edu'

and the problem should disappear.