Hello,
I have the following function:
function Delete-ADUser { Param($userName = $(throw 'need username')) $searcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]"","(&(objectcategory=user)(sAMAccountName=$userName))") $user = $searcher.findone().GetDirectoryEntry() $user.psbase.DeleteTree() #$user.SetInfo() }
I am running it as :
$Global:SchoolList = Import-Csv "C:\Powershell\CSV\SchoolsList.csv"
#Remove Disabled Users
$SchoolList |
ForEach-Object{ $School = $_.School $ou = 'OU=users,OU=' + $School + ',Rest of OU Structure here'
Get-QADUser -Title "student", "teacher","staff","parent" -SearchRoot $OU -SizeLimit 0
$SamAccountName = $_.SamAccountName
Delete-ADUser $SamAccountName
}
Does anyone know why the following error is being produced:
Exception calling "FindOne" with "0" argument(s): "The (&(objectcategory=user)(sAMAccountName=)) search filter is inval
id."
At C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:209 char:26
+ $user = $searcher.findone <<<< ().GetDirectoryEntry()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:210 char:24
+ $user.psbase.DeleteTree <<<< ()
+ CategoryInfo : InvalidOperation: (DeleteTree:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "FindOne" with "0" argument(s): "The (&(objectcategory=user)(sAMAccountName=)) search filter is inval
id."
At C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:209 char:26
+ $user = $searcher.findone <<<< ().GetDirectoryEntry()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:210 char:24
+ $user.psbase.DeleteTree <<<< ()
+ CategoryInfo : InvalidOperation: (DeleteTree:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The line it refers to in the profile is the function above.
If anyone has any ideas I would much appreciate them.
Many Thanks
James