What i am trying to achieve is that if the output is one line and that that line gets written away in a variable. This is the code i have right now:
Connect-AzureRmAccount
(get-azurermresourcegroup).ResourceGroupName
$filter = Read-Host -Prompt "Please filter to find the correct resource group"
$RGName = get-azurermresourcegroup | Where-Object { $_.ResourceGroupName -match $filter }
$RGName.resourcegroupname
this code filters one time and after that it writes all the lines away underneath each other so the results are this:
ResourceGroup-Test
ResourceGroup-Test-1
ResourceGroup-Test-2
but the preferred output is to keep filtering until one is left
Out-GridView
Depending on what the running user chooses for filters this could be a punishing approach / needlessly complicated. If you only want one result how about we instead use something like
Out-GridView
to allow the user to select one result from their chosen filter.You could have used
-PassThru
but that allows for multiple selections.-OutputMode Single
. So this would still have the potential for making a huge selection set if$filter
was too vague but this is a simple way to ensure you get one result. Another caveat is that the user could click Cancel. So you might still need some loop logic:do{..}until{}
. That depends on how resilient you want to make this process.Choice
If
Out-GridView
is not your speed. Another option would be to make a dynamic choice system using$host.ui.PromptForChoice
. The following is an example that allows users to choose a subfolder from a collection.You should be able to adapt that into your code much in the same way that I suggested with
Out-GridView
. Be careful though about this approach. Too many options will clutter the screen.