0

Recently i build below code to automatically move disabled account to other OU that are older then 30 days (in disabled state)

   $checkcurrentdate = Get-Date -Format dd/MM/yyyy
    $DateMaxTime = (Get-date).AddDays(-30)
    $getData = get-ADUser -Filter * -Properties * -SearchBase "OU=Disabled,DC=love,DC=donuts" `
    |where {$_.Description -like "LEFT*"} |select name,samaccountname,description
    
    $FinalResult = New-object System.Collections.ArrayList
    
    Foreach ($Data IN $getData){
    $DataPart = $null
    $UserdistinguishedName = $null
    $DataPart=$Data.description
    $DatePart= $DataPart.substring(5,10)
    $FinalDate = [datetime]::ParseExact($DatePart,'dd/MM/yyyy',$null)
    If ($FinalDate -lt $DateMaxTime  ){
     Write-Host "$($Data.samaccountname), $($Data.description) moved to deleteMe" 
     $User = $Data.samaccountname
     $UserdistinguishedName = (Get-ADUser -Identity $User).distinguishedName
     Move-ADObject -Identity $UserdistinguishedName -TargetPath "OU=DeleteMe,,DC=love,DC=donuts,"  #move user to disabled
     $FinalResult.Add(New-Object psobject -Property @{User=$User})
     }
    else{ 
    Write-Host "$($Data.samaccountname), $($Data.description) still in disabled"
    }
    }
    $list = $FinalResult |Select User |Out-String
    $list.gettype()

I start task scheduler and it was working for w few month in automation But recently i discovered an error which says below :

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

I run this code manually again and error dissapeard but it stuck my code and was not working

I am curios why this happend and if there is any way to alert me if this error occurs ?

probably this happends at line :

 Move-ADObject -Identity $UserdistinguishedName -TargetPath "OU=DeleteMe,,DC=love,DC=donuts,"  #move user to disabled
 $FinalResult += New-Object psobject -Property @{User=$User}
Wiktor
  • 581
  • 1
  • 12
  • 23
  • 2
    I recommend you to generally [avoid using the increase assignment operator (`+=`) to create a collection](https://stackoverflow.com/a/60708579/1701026) as for one thing, it is very expensive. – iRon Apr 29 '21 at 12:04
  • 1
    So instead $FinalResult += New-Object psobject -Property @{User=$User} i should use -> $FinalResult = 1..$Size | ForEach-Object { [PSCustomObject]@{User=$User}} ? Sorry for asking casue i am not an PS expert – Wiktor Apr 29 '21 at 12:26
  • Hi, when i tried to get result outside the loop i get only the same values , instead of diffrent values, how to fix this ? – Wiktor May 04 '21 at 09:02

1 Answers1

1

Swap out these lines for the ones below

$FinalResult = @()
#becomes...
$FinalResult = New-object System.Collections.ArrayList

$FinalResult += New-Object psobject -Property @{User=$User}
#becomes...
$FinalResult.Add(New-Object psobject -Property @{User=$User})

An ArrayList is mutable, and can be changed. It is fast always and even with tons of members.

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • And how to get this information outside the loop to get information from it ? – Wiktor May 04 '21 at 08:30
  • Once the code runs, the variable will persist through the rest of the PowerShell session. So just access it by using the `$finalResult` variable. You could export it to a CSV or do whatever you want with it. `Write-Host "Found $($FinalResult.Count) items", or something lie that – FoxDeploy May 04 '21 at 15:27
  • when i tried the code it persist but it gives me output for one user , just multiplied many times – Wiktor May 10 '21 at 11:26
  • Take your full code and edit it into the question please, I can't help you fix it without seeing how it looks. – FoxDeploy May 10 '21 at 13:16
  • Hi, I did , i just swaped those two arguments – Wiktor May 26 '21 at 07:50