Azure Powershell 获取可用镜像 PublisherName,Offer,Skus,Version

#登录
$username="{登录名}"  #定义一个用户账号的变量,可以输入需要登录的订阅账号名称
$password=ConvertTo-SecureString -String "{密码}" -AsPlainText -Force #对密码进行转换字符加密
$cred= New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$password #定义登录的密钥
Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $cred #登录

# 选择订阅
Get-AzureRmSubscription | select Name, SubscriptionId
$subscriptionId=Read-Host "请选择上面列出的订阅ID:"#多种订阅条件下,因订阅不同,所以要选择一个默认订阅
Write-Host "Selecting subscription '$subscriptionId'";
Select-AzureRmSubscription -SubscriptionID $subscriptionId;


###############获取订阅下可用的image,并打印image信息
$images = New-Object System.Collections.ArrayList;
$location = "China North";
$publishers = Get-AzureRmVMImagePublisher -Location $location;

foreach($publisher in $publishers)
 {
    $offers = Get-AzureRmVMImageOffer -Location $location -PublisherName $publisher.PublisherName;
    foreach($offer in $offers)
    {
        $skus = Get-AzureRmVMImageSku -Location $location -PublisherName $publisher.PublisherName -Offer $offer.Offer;
        foreach($sku in $skus)
        {
            $image = Get-AzureRmVMImage -Location $location -PublisherName $publisher.PublisherName -Offer $offer.Offer -Skus $sku.Skus;
            Write-Output $image  #打印
            $images.Add($image);
        }
    }
    }

foreach($image in $images)
{
    $len = $image.Count;
    if ($len -gt 0)
    {
        $image[$len-1] | select PublisherName,Offer,Skus,Version;
    }
}

在ARM模板中引用镜像时,一般Version参数为 latest,其他几个参数需要指定

 "imageReference": {
            "publisher": "[variables('imagePublisher')]",
            "offer": "[variables('imageOffer')]",
            "sku": "[variables('imageSku')]",
            "version": "latest"
          },

  

原文地址:https://www.cnblogs.com/oceanwang/p/8405266.html