counter fails

Welcome Forums General PowerShell Q&A counter fails

Viewing 12 reply threads
  • Author
    Posts
    • #232372
      Participant
      Topics: 34
      Replies: 82
      Points: 467
      Rank: Contributor

      Hi

      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?

      # 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”
      
      Start-Sleep -seconds 60
      $counter++
      $Zcounter–
      if($zcounter -eq “-1”){break}
      }
      

       

    • #232414
      Participant
      Topics: 5
      Replies: 2384
      Points: 6,066
      Helping Hand
      Rank: Community MVP

      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.

    • #232447
      Participant
      Topics: 34
      Replies: 82
      Points: 467
      Rank: Contributor

      Hi Olaf,

      that’s weird since I have the additional “-” on line 20, and I use powershell ise or visual studio code

      how would you solve line 21?

       

      Paul

    • #232480
      Participant
      Topics: 5
      Replies: 2384
      Points: 6,066
      Helping Hand
      Rank: Community MVP

      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–-
      }
    • #232492
      Participant
      Topics: 34
      Replies: 82
      Points: 467
      Rank: Contributor

      Hi Olaf,

      I’ve got the same behaviour as previously first run it kicks out

      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>
      

      and the second one without changing anything finishing correctly

      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……. :)
      
      PS C:\temp\powershell>
      

      any suggestions on how to get around this?

       

    • #232498
      Participant
      Topics: 5
      Replies: 2384
      Points: 6,066
      Helping Hand
      Rank: Community MVP

      Because you have two independend conditions you should output the two according variables. 😉

    • #232501
      Participant
      Topics: 34
      Replies: 82
      Points: 467
      Rank: Contributor

      Olaf,
      you lost me here 🙁

    • #232510
      Participant
      Topics: 5
      Replies: 2384
      Points: 6,066
      Helping Hand
      Rank: Community MVP

      If I understood you right you don’t know what I mean, right?
      Try this:

      while ($counter -ne $GracePeriodMin -and $zcounter -ne -1) {
          Write-host -ForegroundColor yellow "`$zcounter: '$zcounter'" -NoNewline 
          Write-host -ForegroundColor Green  "`$counter:  '$counter'" 
          $counter++
          $zcounter–-
      }
    • #232537
      Participant
      Topics: 34
      Replies: 82
      Points: 467
      Rank: Contributor

      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?

      Paul

      • This reply was modified 1 month, 2 weeks ago by acer460527.
    • #232543
      Participant
      Topics: 5
      Replies: 2384
      Points: 6,066
      Helping Hand
      Rank: Community MVP

      or am I still missing the point?

      Either you or I. 😉

      Your first condition is $counter -ne $GracePeriodMin. Depending on the initial values your loop will run until $counter equals $GracePeriodMin. What are the initial values?

    • #232546
      Participant
      Topics: 5
      Replies: 2384
      Points: 6,066
      Helping Hand
      Rank: Community MVP

      how do I solve this?

      What is it what you actually want to do?

    • #232555
      Participant
      Topics: 34
      Replies: 82
      Points: 467
      Rank: Contributor

      Olaf,

      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

      Last Dir Sync Time was ’06/01/2020 15:41:56′ UTC – that’s ’20’ minutes ago
      

      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

       

    • #232591
      Participant
      Topics: 34
      Replies: 82
      Points: 467
      Rank: Contributor

      found the solution I changed

      while ($counter -ne $GracePeriodMin -and $zcounter -ne -1)
      

      to

      while ($zcounter -ne -1)
      

      to solve this problem
      thanks Olaf for your help in this

Viewing 12 reply threads
  • You must be logged in to reply to this topic.