2017-05-20 7 views
0

次のスクリプトは正常に動作しています。しかし、大文字小文字を区別しないタグフィールドは読んでいません。大文字小文字を区別して拡張キーを読み取る

$resources = Get-AzureRmResource | Where-Object {($_.ResourceType -eq "Microsoft.Compute/virtualMachines") -or ($_.ResourceType -eq "Microsoft.Sql/servers/databases")} | 
 foreach { 

   new-object -TypeName psobject -Property @{ 
                                    ResourceName       =       $_.ResourceName; 
                                    ResourceType       =       $_.ResourceType; 
                                    OperatingHours  =   
                                                        if (($_ | select -expand Tags).OperatingHours -ieq $null) 
                                                        {"TAG NOT PRESENT"} 
                                                        elseif (($_ | select -expand Tags).OperatingHours -ieq '') 
                                                        {"NULL/EMPTY"} 
                                                        else 
                                                        {($_ | select -expand Tags).OperatingHours} ; 
                                             } 
          } 
$resources | Format-Table 

上記のスクリプトの出力は次のとおりです。

ここ
ResourceType                       ResourceName       OperatingHours             
------------------------            --------------     ------------------             
Microsoft.Compute/virtualMachines   asa-perfvm16       TAG NOT PRESENT 
Microsoft.Compute/virtualMachines   OAASLogicApps1    TAG NOT PRESENT 
Microsoft.Compute/virtualMachines   VMForPerfTest      TAG NOT PRESENT 

、いくつかのリソースではなく、彼らが「operatinghours」または「Operatinghours」または任意の他のケースの組み合わせを持って、「OperatingHours」を持っていません。この値はユーザーによって設定されるため、どのような組み合わせでもかまいません。 私の上記スクリプトは、Tags.Keyの大文字と小文字の区別に関連して適切なデータを取得していません。私はTags.Valueの場合には関心がありません。

上記のスクリプトを次のように変更しようとしましたが、出力は同じです。出力に現れる3つのリソースリスト。すべてのリソースが私によって作成されるので、関係するタグはすべてに存在しているが、異なるケースがあると確信しています。

変更したスクリプト(ただし成功):

$resources = Get-AzureRmResource | Where-Object {($_.ResourceType -eq "Microsoft.Compute/virtualMachines") -or ($_.ResourceType -eq "Microsoft.Sql/servers/databases")} | 
 foreach { 
          
        $rTags = @{} 
            foreach($key in $resources.Tags.key) 
            { 
                      $rTags.Add($key.ToLower(), $resources.Tags[$key].ToLower()) 
            } 

   new-object -TypeName psobject -Property @{ 
      ResourceName       =       $_.ResourceName; 
      ResourceType       =       $_.ResourceType; 
             OperatingHours =   
               if  ($rTags.OperatingHours -ieq $null) 
                     {"TAG NOT PRESENT"} 
               elseif ($rTags.OperatingHours -ieq '') 
                     {"NULL/EMPTY"} 
               else 
                     {$rTags.OperatingHours} ; 
                                             } 
          } 
$resources | Format-Table 

私はここで間違っつもりどこを教えてください!

+0

私はあなたの問題はあるとは思いません「OperatingHours」属性名の大文字小文字の区別に関連しています。 PowerShellは変数名を大文字と小文字を区別しないで処理します - >このリンクを確認してください:https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_variables属性existenseのチェックを '[bool]($ TestObject.PsObject.Properties.name -match" PropertyNameToCheck ")'に変更すると、おそらく役に立ちます。 – Moerwald

答えて

0

私はそれが非常に小さな変更でなければならないことを知っていました。以下は、必要な出力を取得するのに役立つ更新されたスクリプトです。

$resources = Get-AzureRmResource | Where-Object {($_.ResourceType -eq "Microsoft.Compute/virtualMachines") -or ($_.ResourceType -eq "Microsoft.Sql/servers/databases")} | 
foreach { 
            [hashtable] $rTags = @{} 
            foreach($key in $_.Tags.keys) 
            { 
                      $rTags.Add($key.ToLower(), $_.Tags[$key].ToLower()) 
            } 
    new-object -TypeName psobject -Property @{ 
            ResourceName = $_.ResourceName; 
            ResourceType = $_.ResourceType; 
            operatinghours = 
                 if (! $rTags.ContainsKey("operatinghours")) 
                 {"TAG NOT PRESENT"} 
                 elseif ($rTags.operatinghours -ieq '' -or $rTags.operatinghours -ieq $null) 
                 {"NULL/EMPTY"} 
                 else 
                 { $rTags.operatinghours } ; 
            env    = 
                 if (! $rTags.ContainsKey("env")) 
                 {"TAG NOT PRESENT"} 
                 elseif ($rTags.env -ieq '' -or $rTags.env -ieq $null) 
                 {"NULL/EMPTY"} 
                 else 
                 { $rTags.env } ; 
              } 
      } 
$resources | Format-Table 

どのような変更を行いましたか?

    $ _を使用してHastable
  1. 使用むしろ$リソースええ...非常に敷居の間違い:P)より
関連する問題