As you can see I have 5 options to select once the script runs. When I press 1, option 1 selected" gets printed but the function get_all_tags doesn't get called out and doesn't print list of tags. Second time around it prints the results from the function but repeats the result twice.
The weird thing about this is that the its inconsistent. I would run the script and in my first try pressing 1, sometimes, I would get the result from the function get_all_tags .When all the functions are tested on their own, it works perfectly fine. The while loop seems to have caused something irregular within and not sure how I can fix it.
$options=0
$iteration=0
while($options -ne 5)
{
$options = read-host -prompt "
Press
1. to search for all the tags running on Azure Virtual Machine(s)
2. to search for resource using tag name
3. to search for specific resource
4. to search for value of a tag name
5. to exit
"
if ($options -eq 1){
write-host "option 1 selected"
get_all_tags
#give option to exit or go back to the main menu
$iteration++
$iteration
}
elseif ($options -eq 2){
get_resource_with_tag_name $inputTagName
#give option to exit or go back to the main menu
}
elseif ($options -eq 3){
get_resource_tags $inputResource
#give option to exit or go back to the main menu
}
elseif ($options -eq 4){
get_keys_value $inputTagKey
#give option to exit or go back to the main menu
}
}
Function get_resource_tags($resourceName)
{
$inputResource = read-host -prompt 'Write a resource name you wish to search with all associated tags'
return (get-azresource -ResourceGroupName $inputResource).Tags
}
Function get_resource_with_tag_name($tagName){
$inputTagName = read-host -prompt 'Write a tag name you wish to search associated with resource'
return (get-AzResource -TagName $inputTagName).Name
}
Function get_all_tags{
return get-aztag
}
Function get_keys_value($tagKeyName){
$inputTagKey = read-host -prompt 'Write a tag name'
$result = (get-aztag -Detailed $inputTagKey).Values
$final = $result | ft -property @{n="Tag Value";e={$_.name}},count
return $final
}