单独的功能菜单

Seperate Function menus

所以我构建了一个脚本,它允许 powershell 能力有限的人通过菜单执行一些日常任务并输入某些细节。然后它运行所需的命令并执行命令。我有这个工作请客。但是我希望某些选项可以显示不同的提示。因此,有 14 个选项都需要 Group/Mailbox/User 输入,并且大多数需要访问权限的用户。有些可能需要更少或额外的一个。我想删除那些选项不需要的那些。我知道最好的方法可能是另一个函数,但不确定如何将它写入我的脚本以使其工作。我使用 ''If'' 命令删除了 'Q' 退出命令的所有内容,但可以让它为其他人工作,所以认为最好的方法可能是为此目的编写一个新函数。

我希望在这方面有更多知识的人可以提供帮助,这样我以后就可以理解了,因为我是 powershell 这方面的新手。

"Set-ExecutionPolicy RemoteSigned"

$UserCredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

Import-PSSession $Session -DisableNameChecking

function Show-Menu
{
     param (
           [string]$Title = 'O365 EXO Management'
     )
     cls
     Write-Host "================ $Title ================" -ForegroundColor Yellow

     Write-Host "1: Press '1' for adding user to Shared Mailbox/User's Mailbox Full Access." 
     Write-Host "2: Press '2' for adding user to Shared Mailbox/User's Mailbox Full Access with no Mapping."
     Write-Host "3: Press '3' for removing User from Shared Mailbox/User's Mailbox."
     Write-Host "4: Press '4' for checking User's permission to a Shared Mailbox."
     Write-Host "5: Press '5' for checking all users permissions to a Shared Mailbox/User's Mailbox."
     Write-Host "6: Press '6' for removing mailbox permissions on user mailbox."
     Write-Host "7: Press '7' for adding permissions on User's mailbox."
     Write-Host "8: Press '8' for adding Calendar permissions to user or shared mailbox."
     Write-Host "9: Press '9' for Checking User/Shared Mailbox calendar permissions."
     Write-Host "10: Press '10' for adding Contact permissions to user or shared mailbox."
     Write-Host "11: Press '11' for Checking User/Shared Mailbox Contact permissions."
     Write-Host "12: Press '12' for adding Send on Behalf rights to user or shared mailbox."
     Write-Host "13: Press '13' for Checking Send on Behalf rights Contact permissions."
     Write-Host "14: Press '14' for removing users calendar events."
     Write-Host "Q: Press 'Q' to quit."
}
do
{
     Show-Menu
     $input = Read-Host "Please make a selection"
    If ('q' -notcontains $input) {
    $DGroup = Read-Host 'Insert Mailbox Name e.g. Example@Example.com'
    $username = Read-Host 'Insert User who needs Access or Removal'
    $Rights = Read-Host 'Access permissions if recquired. It not required leave blank (Owner, Editor, Author, Reviewer, Contributor, AvailiabilityOnly)'
}
switch ($input)
    # Switch statement

     {
           '1' {
                Add-MailboxPermission -Identity $DGroup -User $username -AccessRights FullAccess
                'You chose option #1'
           } '2' {
                Add-MailboxPermission -Identity $DGroup -User $username -AccessRights FullAccess -AutoMapping:$false
                'You chose option #2'
           } '3' {
                Remove-MailboxPermission -Identity $DGroup -User $username -AccessRights FullAccess
                'You chose option #3'
           } '4' {
                Get-MailboxPermission -Identity $DGroup -User $username
                'You chose option #4'
           } '5' {
                Get-MailboxPermission -Identity $Dgroup | Format-List
                'You chose option #5'
           } '6' {
                Remove-MailboxPermission -Identity $DGroup -User  $username -AccessRights FullAccess -InheritanceType All
                'You chose option #6'
           } '7' {
                Add-MailboxPermission -Identity $DGroup -User  $username -AccessRights FullAccess -InheritanceType All
                'You chose option #7'
           } '8' {
                Add-MailboxFolderPermission -Identity ${DGroup}:\calendar -user $username -AccessRights $Rights
                'You chose option #8'
           } '9' {
                Get-MailboxFolderPermission -Identity ${DGroup}:\calendar
                'You chose option #9'
           } '10' {
                Add-MailboxFolderPermission -Identity ${DGroup}:\Contacts -AccessRights $Rights -User $username
                'You chose option #10'
           } '11' {
                Get-MailboxFolderPermission -Identity ${DGroup}:\Contacts
                'You chose option #11'
           } '12' {
                Set-Mailbox $DGroup -GrantSendOnBehalfTo $username
                'You chose option #12'
           } '13' {
                Get-Mailbox $DGroup | fl displayname, GrantSendOnBehalfTo
                'You chose option #13'
           } '14' {
                Remove-CalendarEvents -Identity $DGroup -CancelOrganizedMeetings
                'You chose option #14'
           } 'q' {
                return
           }
     }
     pause
}
until ($input -eq 'q')

我尝试了 do 命令并为 8 和 10 选项添加了另一个部分,但它出错了,所以我认为 link 它们需要另一种语法?

提前致谢。

由于您对菜单进行了硬编码,因此不难确定哪些选项需要额外的信息,例如权限的用户名。 取自你的函数 Show-Menu,你可以这样做:

# enter an endless loop and break out if the user presses 'Q'
while ($true) {
    Show-Menu
    $choice = Read-Host "`r`nPlease make a selection. Press 'Q' to quit."

    # First: test what the user entered
    If ($choice -like 'Q*') { break }                            # the user wants to quit; exit the loop

    # see if $choice is a numeric value in range of 1 to 14
    $numChoice = 0
    if (![int]::TryParse($choice, [ref]$numChoice)) { continue } # not numeric; re-enter the loop and try again

    # if an invalid numeric option was chosen
    if ((1..14) -notcontains $numChoice) { continue }            # choice was not in range; re-enter the loop and try again  

    # From here on, we have a valid option chosen. Now get the extra info needed for that choice

    # all options need the Mailbox name
    $mailBox = Read-Host "`r`nInsert Mailbox Name e.g. Example@Example.com"
    if ($mailBox -eq 'Q') { break }                              # the user wants to quit; exit the loop

    # not all options need a user name
    if (1,2,3,4,6,7,8,10,12 -contains $numChoice) {
        do {
            # do not allow empty username
            $userName = Read-Host "`r`nInsert User who needs Access or Removal"
        } until (![string]::IsNullOrWhiteSpace($userName))
        if ($userName -eq 'Q') { break }                         # the user wants to quit; exit the loop
    }

    # not all options need the permissions
    if (8,10 -contains $numChoice) {
        "`r`nEnter Access permissions. (Owner, Editor, Author, Reviewer, Contributor, AvailiabilityOnly)"
        $rights = Read-Host 'If not required leave blank'
        if ($rights -eq 'Q') { break }                           # the user wants to quit; exit the loop
    }

    # here, perform the action
    Write-Host "`r`nYou chose option $numChoice" -ForegroundColor Cyan

    switch ($numChoice) {
       1  { Add-MailboxPermission -Identity $mailBox -User $userName -AccessRights FullAccess } 
       2  { Add-MailboxPermission -Identity $mailBox -User $userName -AccessRights FullAccess -AutoMapping:$false }
       3  { Remove-MailboxPermission -Identity $mailBox -User $userName -AccessRights FullAccess } 
       4  { Get-MailboxPermission -Identity $mailBox -User $userName }
       5  { Get-MailboxPermission -Identity $mailBox | Format-List } 
       6  { Remove-MailboxPermission -Identity $mailBox -User  $userName -AccessRights FullAccess -InheritanceType All }
       7  { Add-MailboxPermission -Identity $mailBox -User  $userName -AccessRights FullAccess -InheritanceType All }
       8  { Add-MailboxFolderPermission -Identity ${DGroup}:\calendar -user $userName -AccessRights $rights }
       9  { Get-MailboxFolderPermission -Identity ${DGroup}:\calendar } 
       10 { Add-MailboxFolderPermission -Identity ${DGroup}:\Contacts -AccessRights $rights -User $userName }
       11 { Get-MailboxFolderPermission -Identity ${DGroup}:\Contacts } 
       12 { Set-Mailbox $mailBox -GrantSendOnBehalfTo $userName }
       13 { Get-Mailbox $mailBox | Format-List displayname, GrantSendOnBehalfTo }
       14 { Remove-CalendarEvents -Identity $mailBox -CancelOrganizedMeetings }
     }

     Start-Sleep 3
}

P.S> 是的,$input 是一个 Automatic variable,您不应该将其用作自定义变量名