Whilst talking with a colleague, the topic of being able to create a selectable menu item from a dynamically generated array came up. This was in the context of having a script that would query a list of all mailbox databases within our Exchange 2013 environment and allow the end-user to select one of these databases to then perform work on it.
I’m sure a simpler example based on listing files in the current directory would have been easier, but oh well 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
<# .DESCRIPTION Powershell script that allows you to select a Mailbox Database in your Exchange environment from a dynamic array. .PARAMETERS None - execute directly from the Exchange Management Shell. .Version 0.2 .Author Kyle McDonald .Compatibility Exchange 2013 .Release Date July 2015 .Notes v0.1, 20150715 - Initial version v0.2, 20150716 - Added check for existing Exchange connection #> cls $MailHost = "mailhost.contoso.com" # Check for an open connection to Exchange $OpenSession = Get-PSSession | Where-Object {$_.ComputerName -eq "$MailHost" -and $_.State -eq "Opened"} | select ComputerName -expandproperty ComputerName IF ([string]::IsNullOrEmpty($OpenSession)) {} ELSE { $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$MailHost/PowerShell/ -Authentication Kerberos ; Import-PSSession -disablenamechecking $Session } # Get all Mailbox Databases Write-Host "" Write-Host "Getting Mailbox Databases..." -ForegroundColor Cyan $MBXDBarray = get-mailboxdatabase | select name -expandproperty name | sort name # LoopMain Start Do { $userMenuChoice = "y" # List Mailbox Databases Found cls Write-Host "" Write-Host "Mailbox Databases Found:" -ForegroundColor Cyan for($i=0;$i-le $MBXDBarray.length-1;$i++) {"[{0}] - {1}" -f $i,$MBXDBarray[$i]} # Select MBXDB Write-Host "" Write-Host "Which Database would you like to use (0 to" ($MBXDBarray.length-1)")" -ForegroundColor Cyan -NoNewLine ; $MBXSelect = Read-Host " " Write-Host "" # Validate selection IF ($MBXSelect -le ($MBXDBarray.length-1)) { Write-Host "Selection is valid" # Display item from MBXDBarray Write-Host "" Write-Host "You selected " -NoNewLine ; Write-Host $MBXDBarray[$MBXSelect] -ForegroundColor Cyan -NoNewLine ; $MBXSelectCont = Read-Host ". Shall we continue? (Y/N)" } ELSE { Write-Host "Selection is not valid" $MBXSelectCont = "n" } # Do Stuff Write-Host "" IF ($MBXSelectCont -eq "y") { Write-Host "Starting ..." -ForegroundColor Green Write-Host "" Write-Host "Pretending to do something with the MBXDB here..." Write-Host "Done!" -ForegroundColor Cyan } ELSE { Write-Host "Stopping!" -ForegroundColor Red } #Give the user a choice to run the script again do { Write-Host "" $userMenuChoice = read-host -prompt "Would you like to do this again? (Y/N)" } until ($userMenuChoice -eq "y" -or $userMenuChoice -eq "n") } while ($userMenuChoice -eq "y") # LoopMain End Write-Host "OK, Bye." Write-Host "" |