PoshCode Archive  Artifact [f0a82fbcae]

Artifact f0a82fbcae18278af203b0c9130d3934df056d38b3f5aed2afa461fa2df73234:

  • File Get-OUComputerNames.ps1 — part of check-in [e3fcf1d5eb] at 2018-06-10 13:55:33 on branch trunk — usage : .\Get-OUComputerNames.ps1 “OU=TESTOU,dc=domain,dc=com” (user: Nathan Hartley size: 1149)

# encoding: ascii
# api: powershell
# title: Get-OUComputerNames
# description: usage   : .\Get-OUComputerNames.ps1 “OU=TESTOU,dc=domain,dc=com”
# version: 0.1
# author: Nathan Hartley
# license: CC0
# x-poshcode-id: 546
# x-archived: 2014-03-12T00:13:34
# x-published: 2009-08-22T14:38:00
#
# returns : the names of computers in Active Directory (AD) that
# recursively match a given LDAP query
#
param( 
	[switch]$Help,
	[string] $OU
	)
$usage = @'
Get-OUComputerNames
usage   : .\Get-OUComputerNames.ps1 "OU=TESTOU,dc=domain,dc=com"
returns : the names of computers in Active Directory (AD) that
          recursively match a given LDAP query
author  : Nathan Hartley
'@
if ($help) {Write-Host $usage;break}
if (!$OU) {$OU = $(read-host "Please specify an LDAP search string")}
if (!$OU -match "^LDAP://") { $OU = "LDAP://" + $OU }

$strCategory = "computer"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$OU")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher($objDomain,"(objectCategory=$strCategory)",@('name'))
$objSearcher.FindAll() | %{$_.properties.name}