Asked by:
getting all computers grouped by OU

Question
-
Id like to export to html a list of all computers grouped by OU.
This code sort of works
$OUresults = @()
$groupResults = @()
$OUs = get-adorganizationalunit -filter *
foreach ($OU in $OUs)
{
$OUmember= Get-ADComputer -Filter * -SearchBase $OU -Properties * |Select -ExpandProperty DNSHostName
$properties = @{
"OUName" = $OU
"Computer" = $OUMember
}
$GroupResults += new-Object -TypeName PSCustomObject -property $properties
$OUresults += $groupResults
}
$OUresults | select OUName,ComputerBut, the computers are listed in an array like this
OUName Computer
OU=Domain Controllers {DC1,DC2,DC3}
OU=Servers {server1,server2,server3}Id like output to be more like
OU=DomainContollers
DC1
DC2
DC3OU=Servers
Server1
Server2
Server3
Any help appreciated!Friday, July 27, 2018 2:37 PM
All replies
-
Here is how to do this in PowerShell:
Get-AdOrganizationalUnit -filter * | ForEach-Object{ $ou = $_.DistinguishedName Get-ADComputer -Filter * -SearchBase $ou | ForEach-Object{ [pscustomobject]@{ OUName = $ou Computer = $_.Name } } } #| #Select-Object OUName,Computer
\_(ツ)_/
- Edited by jrv Friday, July 27, 2018 3:42 PM
Friday, July 27, 2018 3:42 PM -
Thank you very much!
Works great. I really appreciate the help.
Steve
Friday, July 27, 2018 3:55 PM -
This is why you should take time to learn PwoerShell instead of copying useless code from the Internet or trying to guess at the answer.
\_(ツ)_/
Friday, July 27, 2018 4:05 PM -
Ive been trying to learn powershell.
This is why I dont like asking for help. You welcome would have been a nicer response.
Friday, July 27, 2018 6:17 PM -
Ive been trying to learn powershell.
This is why I dont like asking for help. You welcome would have been a nicer response.
That was a welcome. I showed you where to get started. I also warned against guesswork and copying code you do not understand.
If you are not someone who knows how to do tutorials or doesn't like to learn then guessing will be slow and you will not learn correctly. You can do that. Their are no laws against that. It is just very slow and painful.
\_(ツ)_/
Friday, July 27, 2018 6:23 PM