each time when I run the below script for the first time it will go through 3 or 4 loops and then fails.
only when I restart the script a second time it will continue as expected any Ideas?
[pre]
Install-Module msonline
Connect-MsolService
$GracePeriodMin = 30
[DateTime]$LastDirSyncTime = (Get-MsolCompanyInformation).LastDirSyncTime # Assuming this time is UTC
Using .ToUniversalTime() method of the DateTime object to obtain current UTC time
$MinutesSinceLastSync = [Math]::Round((New-TimeSpan -Start $LastDirSyncTime -End (Get-Date).ToUniversalTime()).TotalMinutes,0)
Write-Output “Last Dir Sync Time was ‘$LastDirSyncTime’ UTC - that’s ‘$MinutesSinceLastSync’ minutes ago”
Write-host -ForegroundColor cyan “you can now modify the globalDDI.xlsx sheet”
$counter = $GracePeriodMin - $MinutesSinceLastSync
$Zcounter = $counter++
$GracePeriodMin = $GracePeriodMin +1
while ($counter -ne $GracePeriodMin){
Write-host -ForegroundColor yellow “please return in ‘$Zcounter’ min for the second part of the offboarding script”
What code editor do you use? Don’t you get errors? In your code line 20 you missed to provide a value expression following the ‘-’ operator. and in you code line 21 you compare an integer with a string and you should move the condition into your while condition definietion.
If you don’t like to use the debug tools to debug your scripts you could add some Write-Debug oder Write-Verbose (or even Write-Host) commands to your code and simply output the values of your variables to actually see what’s going on.
Assumed you actually want to decrease the $zcounter in your while loop something like this should work I think:
while ($counter -ne $GracePeriodMin -and $zcounter -ne -1) {
Write-host -ForegroundColor yellow "please return in '$zcounter' min for the second part of the offboarding script"
Start-Sleep -seconds 60
$counter++
$zcounter–-
}
I’ve got the same behaviour as previously first run it kicks out
[pre]
PS C:\temp\powershell> C:\Temp\powershell\CheckLastTimeSyncAD.ps1
Last Dir Sync Time was ‘06/01/2020 14:12:01’ UTC - that’s ‘7’ minutes ago
you can now modify the globalDDI.xlsx sheet
please return in ‘23’ min for the second part of the offboarding script
please return in ‘22’ min for the second part of the offboarding script
please return in ‘21’ min for the second part of the offboarding script
please return in ‘20’ min for the second part of the offboarding script
please return in ‘19’ min for the second part of the offboarding script
please return in ‘18’ min for the second part of the offboarding script
please return in ‘17’ min for the second part of the offboarding script
Coffee Break is over get moving…
PS C:\temp\powershell>
[/pre]
and the second one without changing anything finishing correctly
[pre]
Last Dir Sync Time was ‘06/01/2020 14:12:01’ UTC - that’s ‘16’ minutes ago
you can now modify the globalDDI.xlsx sheet
please return in ‘14’ min for the second part of the offboarding script
please return in ‘13’ min for the second part of the offboarding script
please return in ‘12’ min for the second part of the offboarding script
please return in ‘11’ min for the second part of the offboarding script
please return in ‘10’ min for the second part of the offboarding script
please return in ‘9’ min for the second part of the offboarding script
please return in ‘8’ min for the second part of the offboarding script
please return in ‘7’ min for the second part of the offboarding script
please return in ‘6’ min for the second part of the offboarding script
please return in ‘5’ min for the second part of the offboarding script
please return in ‘4’ min for the second part of the offboarding script
please return in ‘3’ min for the second part of the offboarding script
please return in ‘2’ min for the second part of the offboarding script
please return in ‘1’ min for the second part of the offboarding script
please return in ‘0’ min for the second part of the offboarding script
Coffee Break is over get moving…
Hi Olaf,
jikes this means that when even the counter hits 30 it will get out of the loop even when the zcounter is still counting down
how do I solve this?
Your first condition is $counter -ne $GracePeriodMin. Depending on the initial values your loop will run until $counterequals$GracePeriodMin. What are the initial values?
I start my scripts on AD and do several tasks there, then I’m moving over to AzureAD but here I have to wait because of the sync that happens every 30 min
at this very moment the last sync has been done 20 min ago
[pre]
Last Dir Sync Time was ‘06/01/2020 15:41:56’ UTC - that’s ‘20’ minutes ago
[/pre]
this means that I have to wait 11 min before I can continue with the rest of my offboarding tasks on AzureAD
in the mean time I want to display the message that the user has to wait
10 min
9 min
8 min
ect
and exits on -1 (31 min) to be sure that I continue with my script on 30 min +1
that’s the goal of this script