【问题标题】:Powershell function in while loop doesn't output result first time but does output second time,twicewhile循环中的Powershell函数第一次不输出结果,但第二次输出,两次
【发布时间】: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


    【解决方案1】:

    PowerShell 是一种解释器语言,而不是编译器语言。因此,您的函数的位置会影响您的结果。应该在调用它们之前声明它们。

    在您的情况下,在第一次运行期间,while 循环不知道它下面定义的函数。一旦你完全运行它,函数就会加载到内存中,因此,第二次运行可能会给你预期的结果。

    【讨论】:

    • 感谢@sid 回复并指出不同之处。尽管进行了更改,但选项 1 和 3 仍然执行相同的操作,而选项 2 和 4 始终打印出结果。由于字符限制添加为评论,我在下面发布了我当前的代码作为答案。
    【解决方案2】:

    感谢@sid 回复并指出不同之处。尽管进行了更改,但选项 1 和 3 仍然执行相同的操作,而选项 2 和 4 始终打印出结果。

    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
    
    } 
    }
    

    【讨论】:

      猜你喜欢
      • 2013-05-05
      • 1970-01-01
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      相关资源
      最近更新 更多