How to use PowerShell to collect an azure VM image list

Source: Internet
Author: User

This time to share the script on Azure, while using azure, some of the newly created resources are basically already in arm mode, and Azure Classic Portal is now completely superseded by the new portal. In the use of experience, the arm mode is more convenient for most of the ASM, but there are some details of the place because of the more powerful, can support more and more content, sometimes the platform itself provides the functionality may not be able to fully meet our needs, so we will write some of our own small feet to try to make up


The content to share today is very simple, when creating a virtual machine using ARM mode, it is actually three parameters to decide what operating system we want to create, Imagepublisher,imageoffer,imagesku. The three are nested relationships, imagesku contained in the Imageoffer, Imageoffer is included in the Imagepublisher


You can see the publisher of the VM image in each area directly through Get-azurermvmimagepublisher


You can then use Get-azurermvmimageoffer to see what offers he can offer for each different publisher, taking the most common Windows Server examples


For example, if I want to know how to create a virtual machine for server 2016, how to specify the parameters, we can use Get-azurermvmimagesku to see which sku,sku in the selected offer determines the operating system of the virtual machine we created last. For example, you can see that 2016 has a lot of different sku,server cores or container and so on.



So if we want to create a virtual machine with the most basic server 2016, the parameters specified are

Publisher:microsoftwindowsserver

Offer:windowsserver

Sku:2016-datacenter


This will create a virtual machine for Windows Server 2016.


But this is not very convenient, because actually provide the mirror of Publisher is very much, especially in the international version, in East Asia to provide image Publisher has more than 700, not to mention the below offer and SKU.


So we really need a list that tells us what we want to create a virtual machine, and how the corresponding image should be filled out, such as I'm going to create a CentOS 7.2 So how do I specify the image parameter, how to create Ubuntu and how to specify it.


Here we share a small script that can export the image information from Azure to a CSV file, which makes it easy for us to query


The code is as follows:


param  ([Parameter (mandatory =  $false)] $LocationName  =  "Chinanorth", [Parameter ( mandatory =  $false)] $ExportTo  = [environment]::getfolderpath ("Desktop")  +  "\"  +  $LocationName  +  "-vmimage-"  + $ (get-date -format  "Yyyymmdd-hhmmss")  +  ". csv") function check-azurermlocation () {param ([string] $LocationName  = $ (throw   "Parameter missing: -locationname locationname") write-host  "$ (Get-Date)  *  Checking location  $LocationName " -ForegroundColor Cyan$Location =  Get-azurermlocation | where-object { $_. location -eq  $LocationName  }If  (-not  ($Location)) {write-host  "$ (get-date)  *  the location "  $LocationName   does not exist."  -ForegroundColor Redreturn  $false}else{write-host  "$ (get-date)  * Location  $LocationNamE exists " -ForegroundColor Cyanreturn  $true}} $LocationExist  =  check-azurermlocation -locationname  $LocationNameif   ($LocationExist  -eq  $true) { write-host  "$ (get-date)  * begin to collect vm image information. Please wait " -ForegroundColor  ' Cyan ' [pscustomobject[]] $VMImageObjects  =  $nullif   (test-path  $ExportTo) {clear-content  $ExportTo  -force} $Error. Clear () try{$Publishers  =  ( get-azurermvmimagepublisher -location  $LocationName  -erroraction stop). publishername$totalpublishercounts =  $Publishers .count$publishercount = 1foreach  ($ publisher in  $Publishers) {write-progress -activity  ("current publisher: $ Publisher ")  -Status  searching  $PublisherCount  Publisher, Total Publisher:  $TotalPublisherCounts " -PercentComplete  ($PublisherCount/  $TotalPublisherCounts  * 100)  -Id 1$Offers =  (get-azurermvmimageoffer  -Location  $LocationName  -PublisherName  $Publisher  -erroraction stop). offer$ totaloffercounts =  $Offers .countif  ($TotalOfferCounts  -eq 0) {$PublisherCount ++}else{$ offercount = 1foreach  ($Offer  in  $Offers) {write-progress -activity  (" current offer:  $Offer ")  -Status  searching  $OfferCount  Offer, Total  offer:  $TotalOfferCounts  -PercentComplete  ($OfferCount/  $TotalOfferCounts  * 100 )  -id 2 -parentid 1$skus = get-azurermvmimagesku -location $ locationname -publishername  $Publisher  -Offer  $Offer  -erroraction stop$ totalskucounts =  $Skus .countif  ($TotalSkuCounts  -eq 0) {$OfferCount ++}else{$SkuCount  = 1foreach  ($Sku  in  $SKus) {$VMImageObject  = new-object -typename psobject$vmimageobject | add-member  -MemberType NoteProperty -Name LocationName -Value  $Sku. Location$vmimageobject  | add-member -membertype noteproperty -name publishername -value $ sku.publishername$vmimageobject | add-member -membertype noteproperty -name  offername -value  $Sku. Offer$vmimageobject | add-member -membertype noteproperty  -Name SkuName -Value  $Sku .skus$vmimageobjects +=  $VMImageObjectWrite-progress  -Activity  ("current sku: $ ($Sku. SKUs)")  -Status  "searching  $SkuCount   sku, total sku:  $TotalSkuCounts  -PercentComplete  ($SkuCount/  $TotalSkuCounts  * 100)  -id 3 -parentid 2start-sleep -milliseconds 500$skucount++}$ offercount++}} $PublisherCount++}}if  ($VMImageObjects  -ne  $null) {$VMImageObjects  | export-csv -literalpath   $ExportTo  -NoTypeInformation -Force -ErrorAction StopWrite-Host  "$ (get-date)  * Export successfully, Please check  $ExportTo " -foregroundcolor cyan} else{write-host  "$ (get-date)  * something wrong"  -foregroundcolor red}}catch{ write-warning  $Error [0]. Exception.Message}}



Not a professional code farmers, write code is not professional ha ~ Make it possible to use the line, because the script will run longer, the international version of the words may take more than half an hour, so add some progress bar to let users have a good understanding of the progress of the operation. The state of operation is almost like this.




Finally there is a file on the desktop, this is the list of the image we need ~

If you want to create something, just search it OK ~

How to use PowerShell to collect an azure VM image list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.