get-ADComputers.ps1

This is a sample script to list all the computers in a domain or in a specific OU. If no OU is specified all the computers in the domain will be listed.

#***************************************************************************
#** Script: get-ADComputers.ps1
#** Version: 1.0
#** Created: 12/21/2010 20:09
#** Author: Adriaan Westra
#** Website: http://www.westphil.nl
#**
#** Purpose / Comments:
#** List computers in AD or in a ou
#**
#**
#** Changelog :
#** 12/21/2010 20:09 : Initial version
#**
#***************************************************************************
#***************************************************************************
#** Function: Get-ADComputers
#** Version: 1.0
#** Created: 12/21/2010 20:09
#** Author: Adriaan Westra
#** Website: http://www.westphil.nl
#**
#** Purpose / Comments:
#** Get an array of computernames from the domain
#**
#**
#** Arguments :
#** Optional LDAP string to an OU you want to search
#**
#**
#**
#** Returns :
#** Array with computernames
#**
#**
#** Changelog :
#** 12/21/2010 20:09 : Initial version
#**
#***************************************************************************
Function Get-ADComputers ( [string] $searchroot=”LDAP://” + ([adsi]””).distinguishedname ) {
$Category = “computer”
#** Connect to the active directory
$domain = [ADSI] $searchroot
#** Create a searcher object for AD
$Searcher = New-Object System.DirectoryServices.DirectorySearcher
#** Set the searchroot to the AD Domain
$Searcher.SearchRoot = $Domain
#** Only return the computers from the search
$Searcher.Filter = (“(objectCategory=$Category)”)
$Proplist = “name”
#** Limit the returned properties to name only
foreach ($i in $PropList) {
$Searcher.PropertiesToLoad.Add($i) | out-null
}
#** Search the AD and store in $results
$Results = $Searcher.FindAll()
#** Initialize a counter
$i = 0
#** Create an array to hold the computernames
$computers= new-object object[] $results.count
#** Loop through the results from the AD query
foreach ($Result in $Results) {
#** convert the name to a string and add it to the return array
ForEach( $value in $Result.psbase.Properties.item(“name”) ) {
$computers[$i] = $value.ToString()
}
#** raise the counter by 1
$i += 1
}
#** return the array with computer names
return $computers | Sort-Object
} # End Function
#***************************************************************************
#** Start of script
#***************************************************************************
#** Clear the screen
clear-host #** List the computers in the domain.
ForEach( $computer in Get-ADComputers ) {
$computer
}

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *