0

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
}

2 Answers 2

3

PowerShell is an interpreter language as opposed to a compiler language. Hence, the location of your Functions is messing with your results. They should be declared before they are being called.

In your case, during the 1st run, the while loop is not aware of the functions defined below it. Once you run it completely, the functions are loaded in memory and hence, the second run will probably give you the intended results.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks @sid for responding and pointing out the difference. Despite having made the changes, Options 1 and 3 still do that same whilst 2 and 4 always print out results. I have posted my current code below as an answer due to char limitations to add as comment.
0

thanks @sid for responding and pointing out the difference. Despite having made the changes, Options 1 and 3 still do that same whilst 2 and 4 always print out results.

Function get_all_tags{


  return get-aztag

}

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_resource_tags($resourceName)
{
    $inputResource = read-host -prompt 'Write a resource name you wish to search with all associated tags'
       $iteration++
     $iteration
    return (get-azresource -ResourceGroupName $inputResource).Tags


}

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
}



$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


}

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){
    write-host "hello"

    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

} 
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.