Search code examples
functionpowershellnestedscoping

How to run embedded function


I am setting up a small script for a team of exchange admins at our MSP, the script consists of a 4 main functions and within these functions are more functions. I am having some trouble running the embedded functions. Below I have put an example of one of these functions "Manage-Teams"

I have added a Switch ($option) to see if this would resolve the issue, originally I had $option = Read-host -prompt "some text"

This did resolve the issue however I could not find it when tabbing through the functions

function Manage-Teams() {
    Write-Host -ForegroundColor Yellow "What would you like to do? <Enable-AddGuests/Home>"
    $option = Write-Host 'Would you like to allow or disable external access? Enable-AddGuests/Disable-AddGuest'
    function Enable-AddGuests () {

            #Set specific Group back to $True or $False
            # GroupID is <Name.ExcterDirectoryObjectId>
        $GroupID = get-unifiedgroup -Identity (Read-Host -prompt "object ID or SMTP") | Select-Object -ExpandProperty ExternalDirectoryObjectId
        $SettingID = Get-AzureADObjectSetting -TargetType Groups -TargetObjectID $GroupID | select-object -expandproperty ID
        remove-azureadobjectsetting -id $settingid -targettype Groups -TargetObjectID $GroupID
        $template = Get-AzureADDirectorySettingTemplate | ? {$_.displayname -eq "group.unified.guest"}
        $settingsCopy = $template.CreateDirectorySetting()
        $settingsCopy["AllowToAddGuests"]= True
        New-AzureADObjectSetting -TargetType Groups -TargetObjectId $groupID -DirectorySetting $settingsCopy
    }

    function Disable-AddGuests {

        #Set specific Group back to $True or $False
        # GroupID is <Name.ExcterDirectoryObjectId>
    $GroupID = get-unifiedgroup -Identity (Read-Host -prompt "object ID or SMTP") | Select-Object -ExpandProperty ExternalDirectoryObjectId
    $SettingID = Get-AzureADObjectSetting -TargetType Groups -TargetObjectID $GroupID | select-object -expandproperty ID
    remove-azureadobjectsetting -id $settingid -targettype Groups -TargetObjectID $GroupID
    $template = Get-AzureADDirectorySettingTemplate | ? {$_.displayname -eq "group.unified.guest"}
    $settingsCopy = $template.CreateDirectorySetting()
    $settingsCopy["AllowToAddGuests"]= False
    New-AzureADObjectSetting -TargetType Groups -TargetObjectId $groupID -DirectorySetting $settingsCopy
}
    Switch ($option) 
    {
        Enable-AddGuests {Enable-AddGuests}
        Disable-AddGuests {Disable-AddGuests}
        Home {Home}
    }
}

I am hoping for the following:

Manage-teams "what would you like to do" Enable-AddGuests Runs function to enable guest access


Solution

  • This is a simplified version of your script for demonstration purposes.

    Function Manage-Teams {
    
        $option = Read-Host "Would you like to allow or disable external access? Enable-AddGuests/Disable-AddGuests"
    
    
        Function script:Enable-AddGuests {
            "Executing Enable-AddGuests"
        }
        Function script:Disable-AddGuests {
            "Executing Disable-AddGuests"
        }
    
        Switch ($option) {
            'Enable-AddGuests' {Enable-AddGuests}
            'Disable-AddGuests' {Disable-AddGuests}
            Default {"Entered an incorrect option"}
        }
    }
    

    Output:

    Manage-Teams
    Would you like to allow or disable external access? Enable-AddGuests/Disable-AddGuests: Enable-AddGuests
    Executing Enable-AddGuests
    Manage-Teams
    Would you like to allow or disable external access? Enable-AddGuests/Disable-AddGuests: Disable-AddGuests
    Executing Disable-AddGuests
    Manage-Teams
    Would you like to allow or disable external access? Enable-AddGuests/Disable-AddGuests: HelpMe
    Entered an incorrect option
    
    Get-Help Enable-AddGuests
    
    NAME
        Enable-AddGuests
    
    SYNTAX
        Enable-AddGuests
    
    
    ALIASES
        None
    
    
    REMARKS
        None
    
    
    
    Get-Help Disable-AddGuests
    
    NAME
        Disable-AddGuests
    
    SYNTAX
        Disable-AddGuests
    
    
    ALIASES
        None
    
    
    REMARKS
        None
    

    Explanation:

    I changed $option to use Read-Host to prompt the executor with a message and then store the typed in response. I scoped Enable-AddGuests and Disable-AddGuests to the script scope. I added the Default condition of your Switch statement to do something when you do not receive the values you are expecting at the prompt.

    Once Manage-Teams is executed, you can then gain access to the Enable-AddGuests and Disable-AddGuests functions in this example because they are scoped to the script scope. By default, those functions would be local to their enclosing scope only, i.e. inside of Manage-Teams, and not visible to the outside. You will be able to tab complete them as well. If you want access to those functions without running Manage-Teams first, you will need to define and load them outside of Manage-Teams.