2017-05-19 4 views
0

すべてのAzure VirtualMachinesとAzure SqlDatabasesのOperatingHoursタグの詳細を取得しようとしています。続き リソースにappIDができる可能性であり、私はそれに対応した出力で印刷する必要がある値:PowerShellを使用して、タグ名とタグ値を持つすべての紺碧のリソースを一覧表示します。

  1. をOperatingHoursタグ自体は任意のリソースに存在しない場合は、その後OperatingHoursタグ場合は「存在しないタグ」
  2. を表示が存在しますが、ヌルまたは空の文字列を含んでいる場合は、 "NULL/EMPTY"を表示します。
  3. OperatingHoursタグが他の値であれば、その値を表示します。

オプション(2)を別に処理する必要がありますか、それともOperatingHoursの通常の値を印刷するのと同じですか?このスクリプトを実行すると

$ErrorOccured = $false 
$resources = Get-AzureRmResource | 
      Where-Object {($_.ResourceType -eq "Microsoft.Compute/virtualMachines") -or ($_.ResourceType -eq "Microsoft.Sql/servers/databases")} | 
foreach { 
    new-object psobject -Property @{ 
     ResourceName = $_.ResourceName; 
     ResourceType = $_.ResourceType; 
     OperatingHours= try { 
         $ErrorActionPreference = 'SilentlyContinue'; 
         ($_ | select -expand Tags).OperatingHours;  } 
         catch { 
         $ErrorOccured = $true ;    } 
         if ($ErrorOccured) 
         { "Tag not present" } 
         else { 
         ($_ | select -expand Tags).OperatingHours; 
         $ErrorOccured = $false };} 
     } 
$resources | Format-Table 

、私は次のエラーが発生します:

At line:13 char:58 
+               } 
+               ~ 
The hash literal was incomplete. 
At line:20 char:2 
+ } 
+ ~ 
Unexpected token '}' in expression or statement. 
    + CategoryInfo   : ParserError: (:) [], ParentContainsErrorRecordException 
    + FullyQualifiedErrorId : IncompleteHashLiteral 

私はその後、次のコードでスクリプトをOperatingHours文を交換した場合、私は以下のスクリプトを作成した長い努力の後

成功を収めています。しかし、そうすることで、私は上記(1)の選択肢を満たすことができません。

Operating Hours = if (!($_ | select -expand Tags).OperatingHours) 
         {"Tag not present"} 
        else {($_ | select -expand Tags).OperatingHours} ; 

これを修正し、必要な出力を得る方法を教えてください。

おかげ

答えて

0

ヒットと試験と厳格なテストの多くの後、私は私が探していたものが見つかりました:

OperatingHours = if (($_ | select -expand Tags).OperatingHours -ieq $null) 
        {"TAG NOT PRESENT"} 
       elseif (($_ | select -expand Tags).OperatingHours -ieq '') 
        {"NULL/EMPTY"} 
       else 
        {($_ | select -expand Tags).OperatingHours } ; 

私は上記に元のスクリプトでOperatingHours文を置き換えます。

解決策は簡単に見えますが、それを見つけた後、私はそれを以前に見逃すことができたようでしたが、それは学習プロセスのすべてです。

関連する問題