{"id":88,"date":"2015-07-15T11:11:21","date_gmt":"2015-07-15T03:11:21","guid":{"rendered":"http:\/\/kylemcdonald.com.au\/?p=88"},"modified":"2018-08-14T00:36:38","modified_gmt":"2018-08-13T16:36:38","slug":"select-menu-item-from-a-dynamic-powershell-array","status":"publish","type":"post","link":"https:\/\/kylemcdonald.com.au\/2015\/07\/15\/select-menu-item-from-a-dynamic-powershell-array\/","title":{"rendered":"Select menu item from a dynamic Powershell array"},"content":{"rendered":"
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. <\/p>\n
I’m sure a simpler example based on listing files in the current directory would have been easier, but oh well \ud83d\ude42<\/p>\n
<\/p>\n
<#\r\n.DESCRIPTION\r\n Powershell script that allows you to select a Mailbox Database in your Exchange environment from a dynamic array.\r\n.PARAMETERS\r\n None - execute directly from the Exchange Management Shell.\r\n.Version\r\n 0.2\r\n.Author\r\n Kyle McDonald\r\n.Compatibility\r\n Exchange 2013\r\n.Release Date\r\n July 2015\r\n.Notes\r\n v0.1, 20150715 - Initial version\r\n v0.2, 20150716 - Added check for existing Exchange connection\r\n#>\r\n\r\ncls\r\n\r\n$MailHost = \"mailhost.contoso.com\"\r\n \r\n# Check for an open connection to Exchange\r\n$OpenSession = Get-PSSession | Where-Object {$_.ComputerName -eq \"$MailHost\" -and $_.State -eq \"Opened\"} | select ComputerName -expandproperty ComputerName\r\nIF ([string]::IsNullOrEmpty($OpenSession)) \r\n {}\r\n ELSE \r\n { $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http:\/\/$MailHost\/PowerShell\/ -Authentication Kerberos ; Import-PSSession -disablenamechecking $Session }\r\n \r\n# Get all Mailbox Databases\r\nWrite-Host \"\"\r\nWrite-Host \"Getting Mailbox Databases...\" -ForegroundColor Cyan\r\n$MBXDBarray = get-mailboxdatabase | select name -expandproperty name | sort name\r\n \r\n# LoopMain Start\r\nDo {\r\n$userMenuChoice = \"y\"\r\n \r\n# List Mailbox Databases Found\r\ncls\r\nWrite-Host \"\"\r\nWrite-Host \"Mailbox Databases Found:\" -ForegroundColor Cyan\r\nfor($i=0;$i-le $MBXDBarray.length-1;$i++)\r\n{\"[{0}] - {1}\" -f $i,$MBXDBarray[$i]}\r\n \r\n# Select MBXDB\r\nWrite-Host \"\"\r\n Write-Host \"Which Database would you like to use (0 to\" ($MBXDBarray.length-1)\")\" -ForegroundColor Cyan -NoNewLine ; $MBXSelect = Read-Host \" \"\r\nWrite-Host \"\"\r\n \r\n# Validate selection\r\nIF ($MBXSelect -le ($MBXDBarray.length-1))\r\n {\r\n Write-Host \"Selection is valid\"\r\n # Display item from MBXDBarray\r\n Write-Host \"\"\r\n Write-Host \"You selected \" -NoNewLine ; Write-Host $MBXDBarray[$MBXSelect] -ForegroundColor Cyan -NoNewLine ; $MBXSelectCont = Read-Host \". Shall we continue? (Y\/N)\"\r\n }\r\n ELSE\r\n {\r\n Write-Host \"Selection is not valid\"\r\n $MBXSelectCont = \"n\"\r\n }\r\n \r\n# Do Stuff\r\nWrite-Host \"\"\r\n IF ($MBXSelectCont -eq \"y\")\r\n {\r\n Write-Host \"Starting ...\" -ForegroundColor Green\r\n Write-Host \"\"\r\n Write-Host \"Pretending to do something with the MBXDB here...\"\r\n \r\n Write-Host \"Done!\" -ForegroundColor Cyan\r\n }\r\n ELSE\r\n {\r\n Write-Host \"Stopping!\" -ForegroundColor Red\r\n }\r\n \r\n#Give the user a choice to run the script again\r\ndo {\r\n Write-Host \"\"\r\n $userMenuChoice = read-host -prompt \"Would you like to do this again? (Y\/N)\"\r\n } until ($userMenuChoice -eq \"y\" -or $userMenuChoice -eq \"n\")\r\n \r\n }\r\nwhile ($userMenuChoice -eq \"y\")\r\n# LoopMain End\r\n \r\nWrite-Host \"OK, Bye.\"\r\nWrite-Host \"\"\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"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 Continue reading Select menu item from a dynamic Powershell array<\/span>