I am a PowerShell novice trying to create a script that will query AD and return a list of computer names. I found a bit of code online and modified it to my purposes. The first line currently serves no useful purpose. The original script
that I found had something similar, and I might put in an IF statement later, so I kept that line as reminder.
Also, in our environment printers are setup as computer objects in AD. That was done years ago, well before I was hired. Not what I would have done, but it's the situation nonetheless and so needs to be dealt with. I noticed that the SamAccountName
for a computer object that is really a computer always ends with "$", whereas the the SamAccountName for a computer object that is really a printer always contains a MAC address. So the filter
-like "*$" effectively filters out the printers, leaving only true computers in the array.
I just figured out basic arrays tonight, and based on what I have read, the way I am doing it might come with a pretty big performance hit, but I am dealing with less than 1,000 machines and so far the performance is acceptable (Only a few seconds to generate
the list). For now, I just want to get it working at a basic level, and I will worry about optimization later.
Here is what I have so far:
$ObjectType = Read-Host 'Enter a Value: 1 - Computers, 2 - Printers, 3 - Both'
try{
$ou = "OU=Workstations,OU=$City,DC=Fabrikam,DC=com"
$Computers = Get-ADComputer -Filter 'SamAccountName -like "*$"' -SearchBase $ou
}
catch {
$error[0].exception.message
}
The problem is that this returns all of the following:
- DistinguishedName
- DNSHostName
- Enabled
- Name
- ObjectClass
- ObjectGUID
- SamAccountName
- SID
- UserPrincipalName
What I want is ONLY the SamAccountName. How can I modify this script to give me only the SamAccountName?
Thanks in advance for any help that you can offer!
--Tom