We have now altered our script to use the ADSI API, but we still get the "The pipeline has been stopped" error.
The OU contains about 1800 users.
This is the script:
Import-Module Adaxes
[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")
$exportPath = "C:\UserCount\"
$domainName = $Context.GetObjectDomain("%distinguishedName%")
$customerNumber = (Get-AdmOrganizationalUnit -Identity "%distinguishedName%" -Properties * -AdaxesService localhost -Server $domainName).postalCode
$adsi = New-Object Softerra.Adaxes.Adsi.AdmNamespace
$adaxes = $adsi.GetServiceDirectly("localhost")
# User Count
$exportFile = $exportPath + "Brukere_$domainName.csv"
New-Item $exportFile -Type File
$search = $adaxes.OpenObject("Adaxes://%distinguishedName%", $null, $null, 0)
$search.SearchScope = "ADS_SCOPE_SUBTREE"
$search.PageSize = 10000
$search.SearchFilter = "(&(objectCategory=user)(samAccountName=*)(!userAccountControl:1.2.840.113556.1.4.803:=2)(!company=*ikkebetalbar*))"
$result = $search.ExecuteSearch()
foreach ($user in $result.FetchAll()) {
$objUser = $adaxes.OpenObject($user.AdsPath, $null, $null, 0)
$userAccount = $objUser.Get("sAMAccountName")
$userName = $objUser.Get("name")
Add-Content $exportFile "`"$domainName`",`"$customerNumber`",`"%ou%`",`"$userAccount $userName`""
}
$result.Dispose()
# Group Count
$exportFile = $exportPath + "Grupper_$domainName.csv"
New-Item $exportFile -Type File
$search = $adaxes.OpenObject("Adaxes://%distinguishedName%", $null, $null, 0)
$search.SearchScope = "ADS_SCOPE_SUBTREE"
$search.PageSize = 10000
$search.SearchFilter = "(&(objectCategory=group)(description=TilgangTil*))"
$result = $search.ExecuteSearch()
foreach ($group in $result.FetchAll()) {
$objGroup = $adaxes.OpenObject($group.AdsPath, $null, $null, 0)
$groupDescription = $objGroup.Get("description") -replace "TilgangTil ", ""
foreach ($byteGuid in $objGroup.Get("adm-MembersGuid")) {
$guid = New-Object System.Guid(,$byteGuid)
$guid = $guid.ToString("B")
$objUser = $adaxes.OpenObject("Adaxes://<GUID=$guid>", $null, $null, 0)
try {
if (($objUser.Get("objectClass") -eq "group") -or ($objUser.Get("userAccountControl") -band 0x2) -or ($objUser.Get("company") -like "*ikkebetalbar*")) {
continue
}
}
catch{}
$userAccount = $objUser.Get("sAMAccountName")
if ($userAccount.Length -le 0) {
continue
}
$userName = $objUser.Get("name")
Add-Content $exportFile "`"$domainName`",`"$customerNumber`",`"%ou%`",`"$groupDescription`",`"$userAccount $userName`""
}
}
$result.Dispose()