Powershell script to collect certain operating system versions and pipe out computer name

One of the things I struggle is ‘syntax’ when it comes to scripting.  This Powershell script is a combo of things I found to help filter which results are exported from Active Directory.   Hope this helps.


$strCategory = “computer”
$objDomain = New-Object System.DirectoryServices.DirectoryEntry


$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
#$objSearcher.Filter = “(&(objectCategory=computer)(operatingsystemversion=5.2 (3790)))”
#$objSearcher.Filter = “(&(objectCategory=computer)(operatingSystem=Windows Server 2003))”
$objSearcher.Filter = “(&(objectCategory=computer)(operatingsystemversion=6.0 (6001)))”
$colProplist = “name”

foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}


$colResults = $objSearcher.FindAll()
$colResults | foreach {
 $objItem = $_.Properties
 $obj = new-object psobject
 add-member -inp $obj noteproperty name $($objItem.name)
 $obj
} | export-csv servers.csv -noType


Cheers,


Steve

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s