如何运行嵌入式功能

时间:2019-07-12 10:17:11

标签: function powershell nested scoping

我正在为MSP的一组交换管理员设置一个小脚本,该脚本包含4个主要功能,并且在这些功能中还有更多功能。我在运行嵌入式功能时遇到了一些麻烦。下面,我举了其中一个“管理团队”功能的示例

我添加了一个Switch($ option)来查看是否可以解决此问题,最初我有$ option = Read-host-提示“某些文本”

这确实解决了问题,但是在浏览功能时找不到

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}
    }
}

我希望以下几点:

  

管理团队    “你想干什么”   启用添加访客    运行功能以启用访客访问权限

3 个答案:

答案 0 :(得分:1)

让我退后一步来补充AdminOfThings' helpful answer

如果想让嵌套函数在定义它们的函数之外显示 ,只需直接在该外部范围内定义它们

默认情况下,像变量一样,嵌套函数在定义的作用域内是本地的,并且后代作用域中也可见,这样,在相同范围中定义为兄弟姐妹功能可以相互调用

在同一作用域中定义所有函数时,可以避免使用script:在(固定)不同作用域中定义函数的尴尬 [1]

  • 虽然PowerShell 允许修改其他范围,但从健壮性和可维护性的角度来看,通常是个坏主意。

  • 通过从另一个函数内部定义脚本级别的函数,直到第一次调用定义函数后,脚本范围才可以看到它们。

因此,代码结构如下:

# All functions are defined in the same scope, as siblings.

Function Enable-AddGuests {
  # ...
}

Function Disable-AddGuests {
  # ...
}

Function Manage-Teams {

    $option = Read-Host "Would you like to allow or disable external access? Enable-AddGuests/Disable-AddGuests"

    switch ($option) {
      'Enable-AddGuests'  { Enable-AddGuests; break }
      'Disable-AddGuests' { Disable-AddGuests; break }
    }
}

[1]请注意,对于在命令行上粘贴或“点源”(通过脚本,使用运算符.从脚本中)的代码,{{1} }作用域是指 global 作用域。

答案 1 :(得分:0)

似乎您的代码中有错字。

您使用的是Write-Host cmdlet,而不是Read-Host cmdlet。

更改此:

$option = Write-Host 'Would you like to allow or disable external access? Enable-AddGuests/Disable-AddGuest'

对此:

$option = Read-Host 'Would you like to allow or disable external access? Enable-AddGuests/Disable-AddGuest'

答案 2 :(得分:0)

这是脚本的简化版本,用于演示。

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"}
    }
}

输出:

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

说明:

我将$option更改为使用Read-Host来提示执行者一条消息,然后存储键入的内容作为响应。我将Enable-AddGuestsDisable-AddGuests的范围定为script的范围。我在您的Default语句的Switch条件中添加了内容,使您在没有收到提示时期望的值时可以执行某些操作。

在执行本示例中的Manage-Teams之后,您就可以访问Enable-AddGuestsDisable-AddGuests函数,因为它们的作用域是脚本作用域。默认情况下,这些功能仅在其封闭范围内,即在Manage-Teams内部,而在外部不可见。您也可以使用Tab键完成它们。如果要访问这些功能而不先运行Manage-Teams,则需要在Manage-Teams之外定义和加载它们。