【发布时间】:2020-04-30 10:49:42
【问题描述】:
如您所见,一旦脚本运行,我有 5 个选项可供选择。当我按 1 时,option 1 selected" 被打印,但函数 get_all_tags 没有被调用,也没有打印标签列表。第二次打印函数的结果,但重复结果两次。
关于这个的奇怪之处在于它的不一致。我会运行脚本并在第一次尝试按 1 时,有时我会从函数 get_all_tags 中得到结果。当所有函数都单独测试时,它工作得非常好。 while 循环似乎导致了一些不规则的内部问题,不知道如何修复它。
$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
}
【问题讨论】:
标签: azure powershell while-loop scripting azure-virtual-machine