We help IT Professionals succeed at work.

script discovery of all server 2003 boxes in a specific OU

I have been asked to discover the hostnames of all the 2003 Server systems in a specific OU. I wanted to output to a text file. Is there a easy way with Powershell or VBS that you might suggest?
Thank you!
JohnD
Comment
Watch Question

Dale HarrisProfessional Services Engineer
Commented:
I would use Quest Powershell Commandlets found here:
http://www.quest.com/powershell/activeroles-server.aspx

Then using the command "Get-QADComputer" you can specify the OU  you want to target.

Save that to a variable, then do your "discovery" on them for OS type, etc.

$OU = "OU=Servers, DC=Contoso"
$Servers = Get-QADComputer -OU $OU
Foreach ($Server in $Servers){
$BIOS = Get-WMIObject Win32_BIOS -Computer $Server
$SN = $BIOS.serialnumber
$Win32OS = gwmi Win32_OperatingSystem -Computer $Server
$OS = $Win32OS.version
$ComputerSystem = gwmi Win32_ComputerSystem -Computer $Server
$Name = $ComputerSystem.name
}


Something like that, you get the idea.  Very easy to implement.  I've used long and short versions of certain commands, etc.  Remember that you can pull out much more than just the Serial Number, OS Version, and Name, so you can get most of what you want using those three WMI commands.

Good luck!

Dale Harris
Distinguished Expert 2019

Commented:
Dale HarrisProfessional Services Engineer

Commented:
I forgot to include the output to text file portion:

$OU = "OU=Servers, DC=Contoso"
$Servers = Get-QADComputer -OU $OU
Foreach ($Server in $Servers){
$BIOS = Get-WMIObject Win32_BIOS -Computer $Server
$SN = $BIOS.serialnumber
$Win32OS = gwmi Win32_OperatingSystem -Computer $Server
$OS = $Win32OS.version
$ComputerSystem = gwmi Win32_ComputerSystem -Computer $Server
$Name = $ComputerSystem.name
"Server found: $Name `n OS:$OS `n SN:$SN `n Computer Name:$Name" >> "NewServersFound.txt"
}

Actually, it appears this is what you're looking for...

 
dsquery * "OU=Domain Controllers,DC=Contoso,DC=Com" -filter "(&(objectCategory=computer)(operatingSystem=*2003*))"

Open in new window


Enjoy! :)

Author

Commented:
Thank you...I was running afoul the dsquery!
Here is the complete solution you're looking for...

You may run as:

example.bat (will search from root)
example.bat OU=Domain Controllers,DC=SBSuGenius,DC=local
example.bat "OU=Domain Controllers,DC=SBSuGenius,DC=local"

Note: You may use spaces in the distinguished name with or without quotes with this solution.

[example.bat]
@echo off
setlocal enabledelayedexpansion
if [%1]==[] (
	set dn=domainroot
) else (
	set dn=%*
	set dn=!dn:"=!
)
for /f "tokens=1 delims=$" %%s in ('dsquery * "!dn!" -filter "(&(objectCategory=computer)(operatingSystem=*2003*))"^|dsget computer -samid^|find /V "samid" ^|find /V "dsget"') do (
	set str=%%s
	set str=!str: =!
	call :log !str!
)
echo Done! Check %userprofile%\desktop\machines.txt
goto :eof

:log
echo Located %*...
title Located %*...
echo %*>>"%userprofile%\desktop\machines.txt"

Open in new window


Enjoy!