poison
March 31, 2020, 10:42am
1
Hi,
I’m a begginer in powershell and i encounter an issue with my little script
This is my code :
$OUPath= Get-ADOrganizationalUnit -ldapfilter '(name=*)' -SearchBase "OU=Test,OU=Utilisateurs,OU=Titi,DC=TOTO,DC=local" | ? {$_.distinguishedname -notmatch 'OU=_Désactivés'}
$OuName = Get-ADOrganizationalUnit -filter * -SearchBase $OUPath | Select-Object Name
$UserNumber= "1"
write-host $OuName";"$UserNumber
The result is :
@{Name=Test};1
But What i want is :
Test;1
In fact my real script is :
Remove-Item -Path C:\test\UserOU.csv
$UserNumber=''
$exportPath= 'C:\test\UserOU.csv'
$OUPath= Get-ADOrganizationalUnit -ldapfilter '(name=*)' -SearchBase "OU=Test,OU=Utilisateurs,OU=Titi,DC=TOTO,DC=local" | ? {$_.distinguishedname -notmatch 'OU=_Désactivés'}
Foreach ($OUPath in $OUPath) {
$UserNumber = @(Get-AdUser -filter * -SearchBase $OUPath |Where {$_.enabled -eq "True"}).count
Write-Host $OUPath";"$UserNumber
Write-Output $OUPath";"$UserNumber | out-file -FilePath $exportPath -append
}
But the result shows me the OU’s distinguishedname but the name it’s all I need.
PS : Sorry for my english, i’m a french user.
Regards,
Looks to me like “Name” is a property of the $OUPath object. Try this instead:
Write-Host $OUPath.Name, $UserNumer -Separator ";"
poison
March 31, 2020, 12:02pm
3
Hi Mike,
Thanks,
I’ve already tried that, it works fine for the write-host but when i use the write-output and the export (the last line of my last code) the content of my csv file looks like :
PLD
2
#NAME?
;
instead of something like the write-host which is :
PLD;2
Regards,
Are you really just making a csv with “;” delimiters? If so, you can just do this:
$exportPath= 'C:\test\UserOU.csv'
$OUPath= Get-ADOrganizationalUnit -ldapfilter '(name=*)' -SearchBase "OU=Test,OU=Utilisateurs,OU=Titi,DC=TOTO,DC=local" | ? {$_.distinguishedname -notmatch 'OU=_Désactivés'}
$OUPath |
Select-Object -Property name,
@{n=usernumber;e={@(Get-AdUser -filter * -SearchBase $_ |Where {$_.enabled -eq "True"}).count}} |
Export-Csv -Path $exportPath -Delimiter ";"
Olaf
March 31, 2020, 4:14pm
5
Assuming your query for ADOrganizationalUnits provides a list of OUs you could use something like this:
$exportPath = 'C:\test\UserOU.csv'
Remove-Item -Path $exportPath -Force
$OUList = Get-ADOrganizationalUnit -ldapfilter '(name=*)' -SearchBase "OU=Test,OU=Utilisateurs,OU=Titi,DC=TOTO,DC=local" | Where-Object { $_.distinguishedname -notmatch 'OU=_Désactivés' }
$Result = Foreach ($OU in $OUList) {
$UserList = Get-AdUser -filter "enabled -eq '$true'" -SearchBase $OU
[PSCustomObject]@{
OrganizationalUnit = $OU
UserCount = $UserList.count
}
}
$Result | Export-Csv -Path $exportPath -NoTypeInformation
poison
April 1, 2020, 2:24am
6
Thanks to both of you.
@Olaf I’ve used your code.
I’ve just added 2 things :
.name in this part
OrganizationalUnit = $OU.name
because if not, the result was the full distinguishedname of the OU instead of the name only.
And | measure-object in this part
$UserList = Get-AdUser -filter "enabled -eq '$true'" -SearchBase $OU | measure-object
because if not, the result wasn’t correct for OU with 0 or 1 user.
Final code is :
$exportPath = 'C:\test\UserOU.csv'
Remove-Item -Path $exportPath -Force
$OUList = Get-ADOrganizationalUnit -ldapfilter '(name=*)' -SearchBase "OU=Test,OU=Utilisateurs,OU=Titi,DC=TOTO,DC=local" | Where-Object { $_.distinguishedname -notmatch 'OU=_Désactivés' }
$Result = Foreach ($OU in $OUList) {
$UserList = Get-AdUser -filter "enabled -eq '$true'" -SearchBase $OU | measure-object
[PSCustomObject]@{
OrganizationalUnit = $OU.Name
UserCount = $UserList.count
}
}
$Result | Export-Csv -Path $exportPath -NoTypeInformation
Olaf
April 1, 2020, 2:58am
7
OK. I wouldn’t expect to find OUs with 0 or 1 user only. Another approach would be to cast an array no matter how many objects you get in return … like this:
$UserList = @(Get-AdUser -filter "enabled -eq '$true'" -SearchBase $OU)
The search scope should be specified or you will get counts of any child containers, this worked for me:
Import-Module ActiveDirectory
$results= Get-ADOrganizationalUnit -Filter {Name -ne '_Disabled'} -SearchBase "OU=Demo,DC=DEMO,DC=LOCAL" |
Select Name,
DistinguishedName,
@{Name='UserCount';Expression={[int]@(Get-AdUser -Filter {Enabled -eq $true} -SearchBase $_.DistinguishedName -SearchScope OneLevel).count}}
$results