
Boa tarde,
Estou tentando contar quantos objetos estão em 9 UOs diferentes, que estão em uma UO para o DC da minha organização. Esta lista retornará especificamente apenas a quantidade de objetos (computadores nesta situação), mas o conhecimento adquirido me ajudará a executar scripts semelhantes em outras UOs, se necessário.
O problema que estou enfrentando é que a instrução foreach não conta todos os itens em meu contêiner e não examina cada UO diferente. Provavelmente faz mais sentido se você ler o roteiro. (Mudei os nomes de OU e 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")
Estou pesquisando / trabalhando nisso há cerca de (infelizmente) 4 horas hoje e quase perdi a cabeça. Eu realmente apreciaria qualquer ajuda; Nunca tive problemas com uma declaração Foreach preguiçosa antes. Meu palpite é que fiz algo errado ou trabalhei no PDrive AD:\ é incomum.
Se você acha que sou preguiçoso ou que não fiz nenhuma pesquisa, sinto muito. Mas sinto que fiz uma quantia decente. Alguns artigos que estava lendo:
Como posso corrigir meu loop foreach no PowerShell para cada usuário do Active Directory OU?
https://powershell.org/forums/topic/foreach-loop-not-working/
A instrução Powershell foreach não funciona
https://serverfault.com/search?q=Active+directory+get+child+item