2014-12-09 6 views
1

WMI를 사용 중이고 로컬 컴퓨터의 OU를 가져 와서 해당 OU에있는 컴퓨터의 전체 목록을 가져올 수있는 powershell 스크립트를 찾으려고합니다. 여기WMI를 사용하여 컴퓨터의 현재 OU를 얻고 해당 OU의 다른 모든 컴퓨터를 나열하려면 어떻게합니까?

+5

왜 WMI입니까? '\ root \ directory \ LDAP'는 엉덩이에 통증이 있습니다. 'ActiveDirectory' 모듈이나'System.DirectoryServices.DirectorySearcher'를 사용하는 것은 무한히 쉽습니다. –

답변

3

당신은 이동 :

$ComputerName = '<Name of Computer>'; 
$Computer = Get-WmiObject -Namespace 'root\directory\ldap' -Query "Select DS_distinguishedName from DS_computer where DS_cn = '$ComputerName'"; 
$OU = $Computer.DS_distinguishedName.Substring($Computer.DS_distinguishedName.IndexOf('OU=')); 
$ComputersInOU = Get-WmiObject -Namespace 'root\directory\ldap' -Query "Select DS_cn, DS_distinguishedName from DS_computer where DS_distinguishedName like '%$OU'"; 

나는이 또한 하위 OU에있는 컴퓨터를 발견 생각하지만 쿼리의 톤을하지 않고 하나의 OU로 제한하는 방법을 모르겠어요. 쿼리 구문은 매우 드문 경우입니다. 전체 목록을 검색 한 후 하위 OU 개체를 제거하는 것은 성능과 관련하여이를 수행하는 유일한 방법 일 수 있습니다.

공정 경고 : 느립니다. 정말입니다. "아, 쓰레기 나 뭔가를 깨뜨 렸니?" 느린. OU를 20 대 미만의 다른 컴퓨터와 공유하는 컴퓨터에서이 컴퓨터를 가리키며 실행하는 데 거의 1 분이 걸립니다. 단일 컴퓨터를 처음으로 가져 오는 경우에도 1 초 이상 걸립니다.

는 여기에 내가 추천 내용은 다음과 같습니다

Active Directory 모듈을로드하는 것을 포함하여 2 초, 소요
$ComputerName = '<Name of Computer>'; 
Import-Module -Name ActiveDirectory -Cmdlet Get-ADComputer, Get-ADOrganizationalUnit; 
$Computer = Get-ADComputer $ComputerName; 
$OU = $Computer.DistinguishedName.SubString($Computer.DistinguishedName.IndexOf('OU=')); 
$ComputersInOU = Get-ADComputer -Filter * -SearchScope OneLevel -SearchBase (Get-ADOrganizationalUnit $OU).DistinguishedName; 

. 이미로드 된 상태에서 200 밀리 초 미만이 소요됩니다.

ActiveDirectory PowerShell 모듈에 액세스 할 수없는 경우 [ADSISearcher]을 사용할 수 있습니다. 또한 결과가 표시되는 방식 때문에 사용하기가 힘들지 만 ActiveDirectory 모듈보다 훨씬 빠릅니다. 기본적으로이 모듈은 래퍼입니다.

$ComputerName = '<Name of Computer>'; 
$ADSISearcher = New-Object System.DirectoryServices.DirectorySearcher; 
$ADSISearcher.Filter = '(&(name=' + $ComputerName + ')(objectClass=computer))'; 
$ADSISearcher.SearchScope = 'Subtree'; 
$Computer = $ADSISearcher.FindAll(); 

$OU = $($Computer.Properties.Item('distinguishedName')).Substring($($Computer.Properties.Item('distinguishedName')).IndexOf('OU=')); 
$OUADsPath = 'LDAP://' + $OU; 

$ADSISearcher = New-Object System.DirectoryServices.DirectorySearcher; 
$ADSISearcher.Filter = '(objectClass=computer)'; 
$ADSISearcher.SearchScope = 'OneLevel'; 
$ADSISearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry($OUADsPath); 
$ComputersInOU = $ADSISearcher.FindAll(); 

약 50 밀리 초 단위로 실행됩니다.