Hello,
I have the following function:
<BR><BR>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() }<BR><BR>I am running it as :
<BR><BR>$Global:SchoolList = Import-Csv "C:\Powershell\CSV\SchoolsList.csv" <BR>#Remove Disabled Users <BR><BR>$SchoolList | <BR>ForEach-Object{ $School = $_.School $ou = 'OU=users,OU=' + $School + ',Rest of OU Structure here' <BR>Get-QADUser -Title "student", "teacher","staff","parent" -SearchRoot $OU -SizeLimit 0 <BR>$SamAccountName = $_.SamAccountName <BR>Delete-ADUser $SamAccountName <BR>}<BR><BR>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