Welcome Forums General PowerShell Q&A How to omit script blocks?

Viewing 3 reply threads
  • Author
    Posts
    • #168532
      Participant
      Topics: 9
      Replies: 11
      Points: 82
      Rank: Member

      I have a script that creates users on AD, syncs the DCs, Syncs to Azure and creates Office 365 subscription. I’d like to make this script public, but not sure how to skip the sections the user might not want to run. (ie… Sync to Azure AD).

       

    • #168535
      Senior Moderator
      Topics: 8
      Replies: 1164
      Points: 4,076
      Helping Hand
      Rank: Community Hero

      well, that’s were the design considerations apply. You can have functions for all these capabilities and use Switch parameters to change the behavior or some other parameter where its value will decide which or what functions has to be called.

      function DoThis {
          # code here
      }
      
      function DoThat {
          # code here
      }
      
      function DoAction {
      Param(
          [switch]$This,
          [switch]$That
      )
          if($This.IsPresent){
            DoThis
          }
      
          if($That.IsPresent){
            DoThat
          }
      
      }
      
      # OR
      
      function DoAction {
      Param(
          [ValidateSet('This','That')]
          [string[]]$Action
      )
          switch($Action){
              'This' {DoThis}
              'That' {DoThat}
          }
      }
      
    • #168547
      Participant
      Topics: 9
      Replies: 11
      Points: 82
      Rank: Member

      Okay, just to verify you would create functions inside this script for instance, the Sync to Azure AD, then if the user did not supply a parameter for that function it wouldn’t run?

    • #168580
      Senior Moderator
      Topics: 8
      Replies: 1164
      Points: 4,076
      Helping Hand
      Rank: Community Hero

      yep.

Viewing 3 reply threads
  • The topic ‘How to omit script blocks?’ is closed to new replies.