본문 바로가기
IT/Azure

ADH(Azure dedicated host) 유지관리 진행상태 확인(CMW ) cli

by rapker 2023. 4. 25.
반응형
728x90

 

사용방법 

검사할 리소스 그룹을 스크립트 하단의 $ResourceGroupList.Add()로 추가 후 실행


결과 미리보기

 

스크립트

dh-show-cmw-progress.ps1
0.00MB

 

반응형



powershell 7.1

#https://docs.microsoft.com/ko-kr/powershell/module/microsoft.powershell.utility/add-type?view=powershell-7.1#example-1--add-a--net-type-to-a-session
Add-Type @"
namespace Custom
{
  public class HostInfo
  {
      public string cmwName;
      public string rgName;
      public string hostName;
      public string groupName;
  }
}
"@
function CheckHost {
  param(
      [Parameter(mandatory=$true)]
      [AllowNull ()]
      [string]
      $ResourceGroupList         
  )
  $HostList = New-Object 'system.collections.generic.List[Custom.HostInfo]'
  $ResourceGroupList | ForEach-Object -Parallel {
      $HostList = $using:HostList
      #https://docs.microsoft.com/ko-kr/cli/azure/vm/host/group?view=azure-cli-latest#az_vm_host_group_list
      $groupNames = az vm host group list -g $_ --query '[].name' | ConvertFrom-Json
      foreach ($groupName in $groupNames) {
        #https://docs.microsoft.com/ko-kr/cli/azure/vm/host?view=azure-cli-latest#az_vm_host_list
        $hostListTmp = az vm host list -g $_ --host-group $groupName --query '[].{name:name, rg:resourceGroup}' | ConvertFrom-Json
        foreach ($hostTmp in $hostListTmp) {
         
            #https://docs.microsoft.com/ko-kr/cli/azure/maintenance/assignment?view=azure-cli-latest#az_maintenance_assignment_list_parent
            $cmwId = @(az maintenance assignment list-parent `
                    --resource-group $hostTmp.rg `
                    --provider-name "Microsoft.Compute" `
                    --resource-name $hostTmp.name `
                    --resource-type "hosts" `
                    --resource-parent-name $groupName `
                    --resource-parent-type "hostGroups" `
                    --query '[].maintenanceConfigurationId' | ConvertFrom-Json)[0]
            $cmwName = @($cmwId -split "/")[8]
            $obj = New-Object Custom.HostInfo
            $obj.cmwName = $cmwName
            $obj.rgName = $hostTmp.rg
            $obj.hostName = $hostTmp.name
            $obj.groupName = $groupName
         
            $HostList.Add($obj)
        }
      }
  } -ThrottleLimit 20
   
  $remainList = New-Object 'system.collections.generic.List[Custom.HostInfo]'
  $remainList.AddRange($HostList)
  $today = Get-Date -Format "yyyy/MM/dd"
  if ($HostList.Count -gt 0) {
      while ($remainList.Count -gt 0) {
        Write-Host ("=============== 완료 체크시작 =========== ")
        $tmpList = New-Object 'system.collections.generic.List[Custom.HostInfo]'
        $tmpList.AddRange($remainList)
        $tmpList | ForEach-Object -Parallel {
            $remainList = $using:remainList
            $today = $using:today
            #https://docs.microsoft.com/ko-kr/cli/azure/maintenance/applyupdate?view=azure-cli-latest#az_maintenance_applyupdate_get_parent-examples
            $get = @(az maintenance applyupdate get-parent `
                  --name $_.cmwName `
                  --provider-name Microsoft.Compute `
                  --resource-group $_.rgName `
                  --resource-name $_.hostName `
                  --resource-parent-name $_.groupName `
                  --resource-parent-type hostGroups `
                  --resource-type hosts | ConvertFrom-Json)
            #$get
            $hostname = @($get.resourceId -split "/")[10]
            #status value : Completed, InProgress, Pending
            if (@(Get-Date($get.lastUpdateTime) -Format "yyyy/MM/dd") -eq $today) {
              if ($get.status -eq 'Completed') {
                  Write-Host $hostname " => " $get.status ", lastUpdateTime : " $get.lastUpdateTime
                  $silent = $remainList.Remove($_)
              }
              else {
                  Write-Host $hostname " => " $get.status ", lastUpdateTime : " $get.lastUpdateTime -ForegroundColor Red
              }
            }
            else {
              Write-Host $hostname " => " $get.status ", lastUpdateTime : " $get.lastUpdateTime -ForegroundColor Red
            }
           
           
           
        } -ThrottleLimit 20
        $totalCount = $HostList.Count
        $remainCount = $remainList.Count
        $completedCount = $totalCount - $remainCount
        Write-Host "전체:" $totalCount
        Write-Host "완료: " $completedCount
        Write-Host "미완료: " $remainCount
        Write-Host ("=============== 완료 체크종료 =========== ")
      }
  }
}
$ResourceGroupList = New-Object 'system.collections.generic.List[string]'
$ResourceGroupList.Add("")
CheckHost $ResourceGroupList

 

 

반응형
LIST