单独的功能菜单

时间:2019-11-25 08:21:55

标签: powershell office365 superuser

因此,我建立了一个脚本,该脚本允许功能强大的人员通过菜单执行某些日常任务并输入某些详细信息。然后,它运行所需的命令并执行该命令。我请这个工作。但是,我希望某些选项能够显示不同的提示。因此,共有14个选项,所有这些选项都需要组/邮箱/用户输入,并且大多数用户需要访问权限。有些可能需要更少或更多的需求。我想删除那些选项不需要的选项。我知道最好的方法可能是另一个功能,但不确定如何将其写入脚本以使其工作。我使用“ 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个选项添加了另一部分,但是它出错了,所以我认为链接它们还需要另一种语法?

先谢谢大家。

1 个答案:

答案 0 :(得分:0)

由于您要对菜单进行硬编码,因此确定哪些选项需要额外的信息(例如权限用户名)并不困难。 从函数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>是的,$inputAutomatic variable,您不应将其用作自定义变量名