Get-ChildItem과 결합하면 Powershell Foreach 문이 AD에서 작동하지 않습니다.

Get-ChildItem과 결합하면 Powershell Foreach 문이 AD에서 작동하지 않습니다.

좋은 오후에요,

내 조직 DC의 OU에 있는 9개의 서로 다른 OU에 개체 수를 계산하려고 합니다. 이 목록은 구체적으로 개체(이 상황에서는 컴퓨터)의 양만 반환하지만, 얻은 지식은 필요한 경우 다른 OU에서 유사한 스크립트를 실행하는 데 도움이 될 것입니다.

내가 겪고 있는 문제는 foreach 문이 내 컨테이너의 모든 항목을 계산하지 않고 각각의 다른 OU를 조사하지 않는다는 것입니다. 아마도 대본을 읽어보면 더 이해가 될 것 같습니다. (OU 및 DC 이름을 변경했습니다).

        Import-Module ActiveDirectory

    Set-Location "AD:\OU=MACHINES,OU=WESTWING,OU=BLDG202,DC=NORTH,DC=APPS,DC=WEST,DC=HQ"
    #IDK why we have so many DCs/A convoluted Active Directory. 
    $gci = (gci).DistinguishedName  #This selects the DistinguishedName Property of each OU (9 of them)
    $amountofOU = $gci.Count
    $amountofOU #This number is 9, because there are 9 OU's in the MACHINES OU
    $Trimthis = ",OU=MACHINES,OU=WESTWING,OU=BLDG202,DC=NORTH,DC=APPS,DC=WEST,DC=HQ"
    $array = @()
    $i = 0 #####For counting proof that my Foreach is lazy

    foreach($item in $gci){


    $i ++  


    $DNTrim = $item.Replace($Trimthis,$null)


    $lowrgci = gci ($DNTrim).Trim()


    $amount = $lowrgci.count

    $Letter = "`nThe OU $DNTrim has $amount of objects inside" 
##This is the message I am trying to have popup at end of script, with each OU's object count in it. 
    $array += $Letter

    return $array;}
    $i       
####This has been returning 1, because the foreach does not go to each $item


    #The below are just me messing around with output - 
    #Bonus points if you can make one of them work when not using console
    #(New-Object -ComObject wscript.shell).Popup("$array",2000,"OU Counter Thing",0x0 + 0x0)

    [System.Windows.Forms.MessageBox]::Show("$array","Title")

나는 오늘 약 (슬프게도) 4시간 동안 인터넷 검색/작업을 했고 거의 정신을 잃었습니다. 도움을 주시면 정말 감사하겠습니다. 이전에는 게으른 Foreach 문에 문제가 있었던 적이 없습니다. 내 생각엔 내가 뭔가 잘못했거나 PSDRIVE AD:\에서 작업하는 것이 특이한 것 같습니다.

제가 게으르다거나 연구를 하지 않았다고 생각하신다면 죄송합니다. 하지만 나는 꽤 많은 일을 한 것 같다. 내가 읽고 있던 일부 기사:

모든 Active Directory OU 사용자에 대해 powershell에서 foreach 루프를 수정하려면 어떻게 해야 합니까?

https://powershell.org/forums/topic/foreach-loop-not-working/

Powershell foreach 문이 작동하지 않습니다.

https://serverfault.com/search?q=Active+directory+get+child+item

관련 정보