howto restart code from the top

Welcome Forums General PowerShell Q&A howto restart code from the top

Viewing 3 reply threads
  • Author
    Posts
    • #245544
      Participant
      Topics: 44
      Replies: 97
      Points: 605
      Rank: Major Contributor

      Hi all,

      I’ve this code and it’s working just fine the only thing I want to be able to is to restart from the top if a user is still active when this code is executed

      if the account is still active I’m correctly diverted into the Else part, is there a way that I can re-launch this code for a second attempt?
      or do I simply need to call the script again by adding ./mypowershell.ps1 ?

       

      thanks for your input in this matter

    • #245598
      Participant
      Topics: 9
      Replies: 676
      Points: 2,675
      Helping Hand
      Rank: Community Hero

      You could turn the section of code you need to reuse into a function or simply store it in a variable as a scriptblock. if you’re passing parameters to it, I would say a function. I’m not sure which part you are needing to rerun, so this is just an example.

      Now that it’s a function, you can choose how to handle calling it again and how many times. For example, we can predefine 3 attempts per user.

      You could also run the entire list through, keeping track of the successes and failures, and then run the failing users through again. Hopefully you find this helpful. Just for completeness, here is how you can assign to and call scriptblock.

      And you can call it like

      or

      One difference between the two calling conventions is the . method will retain script scoped variables in the calling scope. See this example for clarification of what I mean.

      Output

      But using the .

      Output

    • #245919
      Participant
      Topics: 44
      Replies: 97
      Points: 605
      Rank: Major Contributor

      Hi Doug

      thanks for the explanation of the function Since I’m learning and a complete noob in functions I’ve got a question concerning this part

      in my original code I already import this list
      $users = import-csv c:\temp\csv\toRemove.csv

      this means that you use this list twice?
      for me it would just be sufficient to call the function again after lets say 2 min

      I did find another alternative  by modifying the Else statement by adding this code

      and in the offboarding2 I’m checking again if when the last sync is done and when the script went through that cycle to continue with removing all the groups this user was member off

      you way is obviously faster

      would this work

      thanks again for your much appreciated input

      Paul

    • #246816
      Participant
      Topics: 8
      Replies: 168
      Points: 818
      Helping Hand
      Rank: Major Contributor

      I have never been able to figure out the backtic magic either, until now. I used the html char code and formatted that as code, seems to work `

      Carriage return and line feed `r`n

Viewing 3 reply threads
  • The topic ‘howto restart code from the top’ is closed to new replies.