2017-02-01 3 views
1

내가 할 경우 :모든 Active Directory 컴퓨터를 PowerShell의 배열로 읽으려면 어떻게해야합니까?

# Gets all domain-joined computer names and properties in one object 
$strCategory = "computer" 
$objDomain = New-Object System.DirectoryServices.DirectoryEntry 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher 
$objSearcher.SearchRoot = $objDomain 
$objSearcher.Filter = ("(objectCategory=$strCategory)") 

$colProplist = "name" 

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

$Reader = $objSearcher.FindAll() 

foreach ($Computer in $Reader) { 
    Write-Host $Computer 
} 

$ComputerSystem.DirectoryServices.SearchResult로 나온다. Active Directory에서 모든 컴퓨터를 쿼리하고 문자열 값을 다음과 같은 배열에 저장할 수 있습니까? 내가 둘 모두와 함께 작동 뭔가를 원 요약

$Reader = Get-Content ((Resolve-Path $iL).Path) 

:

나는 내 코드 $Reader도 그래서 같은 컴퓨터의 파일을 받아 들일 수 있도록 때문에 foreach 구문 (예 : foreach $Computer in $Reader)를 유지해야 입력 파일 또는 동일한 반복 구문을 사용하여 광고를 쿼리하여.

편집 :

win7box1 
win7box2 
win10box3 
+0

왜 사용'은 Get-ADComputer -Filter * -Properties * '예를 들면? – sodawillow

+0

입력 파일과 함께 작동하거나 동일한 반복 구문을 사용하여 AD를 쿼리하여 무언가를 원합니다. 당신이 묘사 한 것이 어떻게 작동하는지 나는 잘 모르겠습니다. – AlwaysQuestioning

+0

[매개 변수 집합] (https://blogs.msdn.microsoft.com/powershell/2008/12/23/powershell-v2-parametersets/)? – 4c74356b41

답변

2

해결되었습니다. System.DirectoryServices.DirectorySearcher에서 문자열을 얻으려면, 당신은 내 예에서와 같이 루프를 수행하고 다만 수 :

foreach ($Computer in $Reader) { 
    $ComputerString = $Computer.Properties.Name 
} 
1
$Reader = Get-ADComputer -Filter * | Select-Object -ExpandProperty samAccountName 

foreach ($Computer in $Reader) { 
    #WMI processing 
} 

트릭을 할해야 : 내 입력 파일은 다음과 같이 줄 바꿈으로 컴퓨터 이름을 분리하는 텍스트 파일입니다. 더 이상 필요가 없습니다.

성능상의 이유로 System.DirectoryServices.DirectorySearcher을 사용할 수 있습니다. *이 아닌 실제 필터를 사용합니다.

코드를 here에서 가져온 것으로 추측합니다. 이전에 다른 것을 시도하지 않았다면 PowerShell이 ​​제공하는 가장 쉬운 방법은 아닙니다.

편집 :

당신은 사용할 수 Get-ADComputer을 위해로드 된 액티브 디렉토리 모듈 (Import-Module ActiveDirectory)가 필요합니다.

... ActiveDirectory PowerShell 모듈을 사용하려면 RSAT이 설치되어 있어야합니다.

편집 2 : RSAT가 설치되지 않은

는 글쎄, 확실히,이 답변이 무의미해진다.

+0

'Get-ADComputer'라는 용어가 cmdlet, function, 스크립트 파일 또는 실행 가능한 프로그램의 이름으로 인식되지 않습니다 .'라는 이유가 있을까요? – AlwaysQuestioning

+0

내 대답 업데이트 중 ... – sodawillow

+0

모듈을 실행할 사람의 컴퓨터에 모듈이 설치되지 않을 수 있습니다. 다른 방법이 있습니까? – AlwaysQuestioning