So here’s my issue. I am trying to move a user to a new OU in Active Directory. I want to make sure that the user moves successfully before moving on in the script. Here’s the logic I came up with:
$MoveAttempt=0 do { Start-Sleep 2 $Ismoved = Get-ADUser $UserName -properties CanonicalName | ?{$_.CanonicalName -like "/Separations/$($User.DisplayName)"} if (-not($Ismoved)){$MoveAttempt++} } until($Ismoved -or ($MoveAttempt = 10)) if ($Ismoved){Write-Host -ForegroundColor Yellow "$($User.SamAccountName) has been moved to the Separations OU"} else{Write-Host -ForegroundColor Red "-= Unable to move $($User.SamAccountName) to the Separations OU. Please Investigate Manually. =-"}
My issue is that it appears to wait the 2 seconds as indicated in the start-sleep command, and then do MoveAttempt++ 10 times instantly.
I want it to attempt the Get-ADUser command, and if it doesn’t find the result i’m seeking, then increment $MoveAttempt, and try the command again.
submitted by /u/Dracolis
[link] [comments]
The post Do something 10 times, or until a command succeeds. appeared first on How to Code .NET.