[Azure]使用Azure Powershell查看ARM模式下Azure存储中的vhd文件对应的虚拟机

如题,使用Powershell将一个账号中所有订阅下ARM模式的存储账号过一遍,将其中的vhd文件对应的虚拟机找出来汇总成表。


脚本如下:

$Cred = New-Object System.Management.Automation.PSCredential("[email protected]",(ConvertTo-SecureString "XXXXXXXXX" -AsPlainText -Force));
$AzureRMCred = Get-Credential -Credential $Cred;
Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $AzureRMCred;

$vm_url_mapping = @{};
$storage_url_mapping = @{};

Function FillStorageMapping() 
{
    $storage_url_mapping.Clear();
    $storages = Get-AzureRmStorageAccount;

    foreach($storage in $storages)
    {
        $storageAccountName = $storage.StorageAccountName;
        $blobs = Get-AzureStorageBlob -Context $storage.Context -Container vhds -ErrorAction Ignore;

        foreach ($blob in $blobs)
        {
            if ($blob.BlobType -eq "PageBlob")
            {
                $url = $blob.ICloudBlob.Uri.AbsoluteUri;
                if ($url.EndsWith(".vhd") -and !$storage_url_mapping.ContainsKey($url))
                {
                    $storage_url_mapping.Add($url, $storageAccountName);
                }
            }
        }
    }
}

Function FillVmMapping()
{
    $vm_url_mapping.Clear();
    $vms = Get-AzureRmVM;

    foreach($vm in $vms)
    {
        $resourceGroupAndVM = $vm.ResourceGroupName + "#" + $vm.Name;
        
        $osDiskUrl = $vm.StorageProfile.OsDisk.Vhd.Uri;
        if (!$vm_url_mapping.ContainsKey($osDiskUrl))
        {
            $vm_url_mapping.Add($osDiskUrl, $resourceGroupAndVM);
        }

        $dataDisks = $vm.StorageProfile.DataDisks;
        foreach($dataDisk in $dataDisks)
        {
            $dataDiskUrl = $dataDisk.Vhd.Uri;
            if (!$vm_url_mapping.ContainsKey($dataDiskUrl))
            {
                $vm_url_mapping.Add($dataDiskUrl, $resourceGroupAndVM);
            }
        }
    }
}

Function ShowStorageVhdOwners()
{
    $subscriptions = Get-AzureRmSubscription;
    $results = New-Object System.Collections.ArrayList;

    foreach($subscription in $subscriptions)
    {
        $subscriptionName = $subscription.SubscriptionName;
        Select-AzureRmSubscription -SubscriptionName $subscriptionName;
        FillStorageMapping;
        FillVmMapping;
        foreach ($url in $storage_url_mapping.Keys)
        {
            $storageAccount = $storage_url_mapping[$url];
            $resourceGroup = "";
            $vmName = "";
            if ($vm_url_mapping.ContainsKey($url))
            {
                $resourceGroupAndVM = $vm_url_mapping[$url].Split('#');
                $resourceGroup = $resourceGroupAndVM[0];
                $vmName = $resourceGroupAndVM[1];
            }
            $results.Add(@{SubscriptionName=$subscriptionName; StorageAccountName=$storageAccount; VhdUrl=$url; ResourceGroupName=$resourceGroup; VirtualMachineName=$vmName;});
        }
    }

    $results | Select @{Name="SubscriptionName"; Expression={$_["SubscriptionName"]}}, @{Name="StorageAccountName";Expression={$_["StorageAccountName"]}}, @{Name="VHD Url"; Expression={$_["VhdUrl"]}}, @{Name="ResourceGroupName"; Expression={$_["ResourceGroupName"]}}, @{Name="VirtualMachineName"; Expression={$_["VirtualMachineName"]}} | Out-GridView;
}

ShowStorageVhdOwners


脚本输出:

[Azure]使用Azure Powershell查看ARM模式下Azure存储中的vhd文件对应的虚拟机_第1张图片


你可能感兴趣的:(Powershell,Script,Azure)