How to omit script blocks?

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

This topic contains 3 replies, has 2 voices, and was last updated by

 
Senior Moderator
1 month, 3 weeks ago.

  • Author
    Posts
  • #168532

    Participant
    Topics: 7
    Replies: 10
    Points: 56
    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: 1041
    Points: 3,440
    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: 7
    Replies: 10
    Points: 56
    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: 1041
    Points: 3,440
    Helping Hand
    Rank: Community Hero

    yep.

You must be logged in to reply to this topic.