Sunday, July 7, 2013

Getting Started with Azure PowerShell - Part 1

Windows Azure PowerShell is a powerful scripting environment that you can use to control and automate the deployment of virtual machines and other resources in Azure. This blog post will cover the basics to get started. By the end you should able to create a VM in the cloud and manage its lifecycle. In the next blog I will cover how to run remote commands against this VM. Since you are going to pay for the actual duration you are using, this automation will be extremely useful.

Install Azure Module

Install the PS module from http://www.windowsazure.com/en-us/downloads. This installation happens via Web Platform Installer and it will automatically install all the dependencies.
Make sure to enable script execution in PS if not already done. This can be done by running “Set-ExecutionPolicy RemoteSigned” from an elevated prompt.
Import the azure module, to access Azure cmdlets. On a 64 bit version of windows, the command would be:

Import-Module 'C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure'

Once imported you can check the module with Get-Module cmdlet.

PS C:\windows\system32> Get-Module

ModuleType Name                                ExportedCommands                                  
---------- ----                                ----------------                                  
Binary     Azure                               {Add-AzureCacheWorkerRole, ...
Script     ISE                                 {Get-IseSnippet, Import-IseSnippet,...
Manifest   Microsoft.PowerShell.Management     {Add-Computer, Add-Content, ...
Manifest   Microsoft.PowerShell.Utility        {Add-Member, Add-Type,...

You can run Get-Command, to get the list of cmdlets supported by the azure module.

PS C:\windows\system32> Get-Command -Module azure

CommandType     Name                                               ModuleName                    
-----------     ----                                               ----------                    
Cmdlet          Add-AzureCacheWorkerRole                           Azure
Cmdlet          Add-AzureCertificate                               Azure
Cmdlet          Add-AzureDataDisk                                  Azure
Cmdlet          Add-AzureDisk                                      Azure

<... output truncated ...>             
   

Configure your workstation to connect with Azure

You need to do this setup only once in your workstation. To connect with Azure you need three pieces of information a) Subscription name b) Subscription ID and c) Certificate. You can either supply this information manually or from a publishing settings file. Manual configuration can be done by cmdlet “Set-AzureSubscription” Let’s do the easy way! Run the cmdlet “Get-AzurePublishSettingsFile” to download the settings file, and import into PowerShell. (Note: It will open the browser, you need to login to your azure account if not already logged in).

#Run the following cmdlets once per workstation.
Get-AzurePublishSettingsFile
Import-AzurePublishSettingsFile  'C:\temp\azure.publishsettings’


If you have more than one subscription then, use Get-AzureSubscription to get current subscription and Select-AzureSubscription to select active subscription. Other azure cmdlets uses current subscription in talking to azure. Now you are ready to talk to Azure!

VM Lifecycle management

Get-AzureVM lists the VMs in your current subscription. Note you don’t have to specify any credentials as you have configured the certificate for authentication in the previous step.

PS C:\windows\system32> Get-AzureVM

ServiceName                   Name                         Status                      
-----------                   ----                         ------                      
sivadomain                    azurevm1                     StoppedDeallocated          

To create a new VHD you need to specific what OS image to use. Azure comes with tons of stock images. If you are not happy with that you even upload your own image. In this case I am going to use the stock image for Windows Server 2012 Datacenter. As there can be multiple datacenter images, I am going to select the last one. “?” is a short form for where. Following script gets the image, saves it into $image, and outputs the label to console.

$image = Get-AzureVMImage |
           ? label -Like "Windows Server 2012 datacenter*"  |
           select -Last 1
$image.Label

Test if the servicename is available. Since service name maps to DNS name it has to be globally unique.

PS C:\windows\system32> Test-AzureName -Service -name sivablogvm1
False

Following script, creates two VMs. They are created differently to illustrate both ways.

#get the Server 2012 datacenter image
#select the last one that will likely be the latest
$image = Get-AzureVMImage |
           ? label -Like "Windows Server 2012 datacenter*"  |
           select -Last 1
$image.Label


#location where to create the VM
$location = Get-AzureLocation | ? Name -EQ 'West US'

#There are two ways to create the VM
#blogvm1 is created using New-AzureQuickVM
#blogvm2 is created using New-AzureVM
#service name is also the DNS name and hence has to be globally unique.


New-AzureQuickVM -Name "sivablogvm1" `
                -Windows `
                -ServiceName "sivablogvm1" `
                -Location $location.Name `
                -ImageName $image.ImageName `
                -InstanceSize ExtraSmall `
                -AdminUsername "siva" `
                -Password "Pass@word" `
                -EnableWinRMHttp `
                -WaitForBoot  

New-AzureVMConfig -Name "sivablogvm2" -InstanceSize ExtraSmall -ImageName $image.ImageName |
   Add-AzureProvisioningConfig –Windows -AdminUsername "siva" –Password "Pass@word" -EnableWinRMHttp |
   New-AzureVM -ServiceName "sivablogvm2" -WaitForBoot -Location $location.Name
You can login to the portal and see these VMs. To check the VMs from PS use Get-AzureVM:

PS C:\windows\system32> Get-AzureVM

ServiceName                   Name                         Status                      
-----------                   ----                         ------                      
sivablogvm1                   sivablogvm1                  ReadyRole                   
sivablogvm2                   sivablogvm2                  ReadyRole                   


Finally you can delete sivablogvm2 using “Remove-AzureVM”:

PS C:\windows\system32> Get-AzureVM -ServiceName "sivablogvm2" | Remove-AzureVM

PS C:\windows\system32> Get-AzureVM

ServiceName                   Name                         Status                      
-----------                   ----                         ------                      
sivablogvm1                   sivablogvm1                  ReadyRole                   

Start/Stop VM

#stop the VM
Stop-AzureVM -ServiceName "sivablogvm1" -Name "sivablogvm1" -Force
#start the VM
Start-AzureVM -ServiceName "sivablogvm1" -Name "sivablogvm1"
#restart the VM
Restart-AzureVM -ServiceName "sivablogvm1" -Name "sivablogvm1"

Azure Disks

Every VM in Azure is backed by a disk where the state is stored. Disk can either be an OS disk or data disk. In our case, there is no data disk, the OS disk is implicitly created. Deleting a VM does not delete the associated disk. The following script queries the disk and finally deletes the OS disk associated with sivablogvm2.

#to get OS disk associated with the VM
Get-AzureVM -ServiceName "sivablogvm1" -Name "sivablogvm1" |
       Get-AzureOsDisk

#to get data disk associated with the VM
#since we did not associate a seperate data disk, this will be empty
Get-AzureVM -ServiceName "sivablogvm1" -Name "sivablogvm1" |
       Get-AzureDataDisk

#get the disk associated with the deleted sivablogvm2
#deleting the VM will not delete the associated disk
Get-AzureDisk | ? diskname -Like 'sivablogvm2*'

#now delete the associated disk
Get-AzureDisk | `
   ? diskname -Like 'sivablogvm2*' |
   Remove-AzureDisk

In part 2, I will go over automating and connecting to the VM.
Explore & Enjoy!
/Siva

135 comments:


  1. Nice information. I was searching for the same. It helped me alot and saved my time. Thanks alot. windows azure training in hyderabad

    ReplyDelete
  2. Much obliged to you for requiring significant investment to give us a portion of the valuable and restrictive data with us.
    Regards,
    Azure Training institute in Chennai | Microsoft windows azure Training | Azure classes in Chennai

    ReplyDelete
  3. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete


  4. Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating. Power BI Online Training .

    ReplyDelete
  5. It’s always so sweet and also full of a lot of fun for me personally and my office colleagues to search your blog a minimum of thrice in a week to see the new guidance you have got.
    selenium training in chennai

    ReplyDelete
  6. Nice article Thanks for sharing this article Wonderful information from this website Thanks !!!

    Embedded training in chennai | Embedded training centre in chennai

    ReplyDelete
  7. Wow. great job! It’s amazing, thank you so much for explaining information in this post with example, it’s very clever and will be extremely helpful for all people. Got to learn new things from your Blog.
    Introduction to Azure DevOps

    ReplyDelete
  8. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    Devops training in Chennai
    Devops training in Bangalore
    Devops Online training
    Devops training in Pune

    ReplyDelete
  9. I would assume that we use more than the eyes to gauge a person's feelings. Mouth. Body language. Even voice. You could at least have given us a face in this test.
    java training in chennai | java training in bangalore

    java training in tambaram | java training in velachery

    java training in omr

    ReplyDelete
  10. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.
    Best AWS Training in Chennai | Amazon Web Services Training in Chennai

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    ReplyDelete
  11. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
    python training in chennai
    python training in Bangalore
    Python training institute in chennai

    ReplyDelete
  12. Awesome..You have clearly explained …Its very useful for me to know about new things..Keep on blogging..
    iphone service center | ipad service center | imac service center

    ReplyDelete
  13. And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
    SOFTWARE TRAINING IN CHENNAI
    POWERBI TRAINING IN CHENNAI
    CCNA TRAINING IN CHENNAI

    ReplyDelete
  14. really nice blog has been shared by you. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.really nice blog has been shared by you. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.

    Click here Devops Training in Bangalore
    to learn trending courses in International market and we will provide u 100% job assistance even.

    ReplyDelete
  15. Alleyaaircool is the one of the best home appliances repair canter in all over Delhi we deals in repairing window ac, Split ac , fridge , microwave, washing machine, water cooler, RO and more other home appliances in cheap rates

    Window AC Repair in vaishali
    Split AC Repair in indirapuram
    Fridge Repair in kaushambi
    Microwave Repair in patparganj
    Washing Machine Repair in vasundhara
    Water Cooler Repair in indirapuram
    RO Service AMC in vasundhara
    Any Cooling System in vaishali
    Window AC Repair in indirapuram

    ReplyDelete
  16. Thanks for sharing awesome information with us.

    I started my journey as a data scientist, faced many difficulties while searching for Job. Things are hardly any different in India than the rest of the world. Experience speaks the last word in the corporates. But in cities like Bangalore, you can get a job or an internship at a startup quite easily. You need to be trained first. Data science training in Bangalore is easily available and top-notch.

    ReplyDelete
  17. Excellent post!!! It was very interesting and very comprehensive post. Well done post and Keep doing...
    ORACLE TRAINING IN CHENNAI

    ReplyDelete
  18. Thanks for sharing this blog. really nice and useful for me
    big data training in chennai

    ReplyDelete
  19. Excellent post!!
    https://www.kaashivinfotech.com/summer-internship-in-chennai/
    https://www.kaashivinfotech.com/summer-internship-in-chennai/
    https://www.kaashivinfotech.com/winter-internship-in-chennai/
    https://www.kaashivinfotech.com/online-internships/

    ReplyDelete
  20. Very interesting information blog post, my favorite this post, thanks a lot for these amazing content.
    Assignment Help
    Online Assignment Help

    ReplyDelete
  21. Thanks for the information. The one thing I have noticed in this website is that you were continuously updating the changes that you have been made. It is a good sign to attract more people and I appreciate it. Hope more update and news from you.

    Matlab Training in Chennai | Artificial Intelligence Training in Chennai | C Training in Chennai | C++ Training in Chennai | PHP Training in Chennai | Sql Server Training in Chennai | Web Design Training in Chennai | Python Training in Chennai | Linux Training in Chennai

    ReplyDelete
  22. Assignment Help is the best solution to fix the issues of assignment writing at affordable rates. Get your homework completed in one click. For more information, talk to our experts.

    ReplyDelete
  23. Nice Post! It helps me to know about Nepal Tour Package. It gives a lot of information to book the best Nepal tour packages. Thanks for sharing this blog!
    Nepal Tour Packages
    Nepal Tour Package
    Nepal Tourism Packages
    Nepal Tourist Packages
    Nepal Tour
    Most Beautiful Places In The World

    ReplyDelete
  24. when checking for the live train information you need to get the live train data from here

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. norton.com/setup - Norton security program can secure either one device or up to ten devices and keep the whole network safe from malware such as worms, spyware, rootkits, etc. For getting Norton Setup, visit norton.com/setup 

    ReplyDelete
  27. 'I'm highly impressed by the piece of thoughts you have shared on this portal. all the best
    connect us on Assignment Help can shed your burden of assignments with a return of qualitative assignments.

    ReplyDelete
  28. Everything is perfect with this work. The writer has managed to keep the article interesting while discussing some really serious points. This is not easily done. All the best.
    How To Start A College Essay
    essay definition
    essay title generator
    sample college essays
    essay examples

    ReplyDelete
  29. Assignment Help Australia services are the best options to submit pending assignments on time without taking stress of missing due dates. Check out our website for more information about online academic writing services in Australia.
    Assignment Help
    Assignment Help Online
    Online Assignment Help
    Assignment Help Online Services
    Assignment Helper
    Assignment Assistance
    Assignment Help Experts
    Online Assignment Help Services

    ReplyDelete
  30. Python Training in Bangalore with 100% placement. We are the Best Python Training Institute in Bangalore. Our Python courses are taught by working professionals who are experts in.

    python training in bangalore

    python courses in bangalore

    python classes in bangalore

    python training institute in bangalore

    python course syllabus

    best python training

    python training centers

    ReplyDelete
  31. VMWare Training in Bangalore with 100% placement. We are the Best VMWare Training Institute in Bangalore. Our VMWare courses are taught by working professionals who are experts in.

    vmware training in bangalore

    vmware courses in bangalore

    vmware classes in bangalore

    vmware training institute in bangalore

    vmware course syllabus

    best vmware training

    vmware training centers

    ReplyDelete
  32. AWS Training in Bangalore with 100% placement. We are the Best AWS Training Institute in Bangalore. Our AWS courses are taught by working professionals who are experts in.

    aws training in bangalore

    aws courses in bangalore

    aws classes in bangalore

    aws training institute in bangalore

    aws course syllabus

    best aws training

    aws training centers

    ReplyDelete
  33. Mulesoft Training in Bangalore with 100% placement. We are the Best Mulesoft Training Institute in Bangalore. Our Mulesoft courses are taught by working professionals who are experts in Mulesoft with minimum 8+ Years of Real-time Work Exposure.

    mulesoft training in bangalore

    mulesoft courses in bangalore

    mulesoft classes in bangalore

    mulesoft training institute in bangalore

    mulesoft course syllabus

    best mulesoft training

    mulesoft training centers

    ReplyDelete
  34. Devops Training in Bangalore with 100% placement. We are the Best Devops Training Institute in Bangalore. Our Devops courses are taught by working professionals who are experts in.

    devops training in bangalore

    devops courses in bangalore

    devops classes in bangalore

    devops training institute in bangalore

    devops course syllabus

    best devops training

    devops training centers

    ReplyDelete
  35. Selenium Training in Bangalore with 100% placement. We are the Best Selenium Training Institute in Bangalore. Our Selenium courses are taught by working professionals who are experts in.

    selenium training in bangalore

    selenium courses in bangalore

    selenium classes in bangalore

    selenium training institute in bangalore

    selenium course syllabus

    best selenium training

    selenium training centers

    ReplyDelete
  36. ServiceNow Training in Bangalore with 100% placement. We are the Best ServiceNow Training Institute in Bangalore. Our ServiceNow courses are taught by working professionals who are experts in ServiceNow with minimum 8+ Years of Real-time Work Exposure.

    servicenow training in bangalore

    servicenow courses in bangalore

    servicenow classes in bangalore

    servicenow training institute in bangalore

    servicenow course syllabus

    best servicenow training

    servicenow training centers

    ReplyDelete
  37. Dell Boomi Training in Bangalore with 100% placement. We are the Best Dell Boomi Training Institute in Bangalore. Our Dell Boomi courses are taught by working professionals who are experts in Dell Boomi with minimum 8+ Years of Real-time Work Exposure.

    dell boomi training in bangalore

    dell boomi courses in bangalore

    dell boomi classes in bangalore

    dell boomi training institute in bangalore

    dell boomi course syllabus

    best dell boomi training

    dell boomi training centers

    ReplyDelete
  38. Citrix Training in Bangalore with 100% pacement. We are the Best Citrix Training Institute in Bangalore. Our Citrix courses are taught by working professionals who are experts in.

    citrix training in bangalore

    citrix courses in bangalore

    citrix classes in bangalore

    citrix training institute in bangalore

    citrix course syllabus

    best citrix training

    citrix training centers

    ReplyDelete
  39. Workday Training in Bangalore with 100% placement. We are the Best Workday Training Institute in Bangalore. Our Workday courses are taught by working professionals who are experts in.

    workday training in bangalore

    workday courses in bangalore

    workday classes in bangalore

    workday training institute in bangalore

    workday course syllabus

    best workday training

    workday training centers

    ReplyDelete
  40. Thanks for sharing your innovative ideas to our vision. I have read your blog and I gathered some new information through your blog. Your blog is really very informative and unique. Keep posting like this. Awaiting for your further update.If you are looking for any Big Data related information, please visit our website Big Data training institute in Bangalore.

    ReplyDelete

  41. Hi,Great information.Thanks for sharing.I always read your blog and get lot of useful information.Are you trying for deep data preparation and analysis?Then learn tableau, a data visualization tool from tableau training institute in bangalore

    ReplyDelete
  42. SAP ABAP Training in Bangalore with 100% pacement. We are the Best SAP ABAP Training Institute in Bangalore. Our SAP ABAP courses are taught by working professionals who are experts in

    sap abap training in bangalore

    sap abap courses in bangalore

    sap abap classes in bangalore

    sap abap training institute in bangalore

    sap abap course syllabus

    best sap abap training

    sap abap training centers

    ReplyDelete
  43. SAP BASIS Training in Bangalore with 100% pacement. We are the Best SAP BASIS Training Institute in Bangalore. Our SAP BASIS courses are taught by working professionals who are experts in

    sap basis training in bangalore

    sap basis courses in bangalore

    sap basis classes in bangalore

    sap basis training institute in bangalore

    sap basis course syllabus

    best sap basis training

    sap basis training centers

    ReplyDelete
  44. SAP FICO Training in Bangalore with 100% pacement. We are the Best SAP FICO Training Institute in Bangalore. Our SAP FICO courses are taught by working professionals who are experts in

    sap fico training in bangalore

    sap fico courses in bangalore

    sap fico classes in bangalore

    sap fico training institute in bangalore

    sap fico course syllabus

    best sap fico training

    sap fico training centers

    ReplyDelete
  45. SAP HANA Training in Bangalore with 100% pacement. We are the Best SAP HANA Training Institute in Bangalore. Our SAP HANA courses are taught by working professionals who are experts in

    sap hana training in bangalore

    sap hana courses in bangalore

    sap hana classes in bangalore

    sap hana training institute in bangalore

    sap hana course syllabus

    best sap hana training

    sap hana training centers

    ReplyDelete
  46. SAP MM Training in Bangalore with 100% pacement. We are the Best SAP MM Training Institute in Bangalore. Our SAP MM courses are taught by working professionals who are experts in


    sap mm training in bangalore

    sap mm courses in bangalore

    sap mm classes in bangalore

    sap mm training institute in bangalore

    sap mm course syllabus

    best sap mm training

    sap mm training centers

    ReplyDelete
  47. This is a very knowledge and informative article,
    Thanks for sharing with us,
    We are again come on your website,
    Thanks and good day,
    Please visit our site,
    buylogo

    ReplyDelete
  48. Blockchain Course in Bangalore with 100% placement. We are the Best Blockchain Course Institute in Bangalore. Our Agile testing course and Certification courses are taught by working professionals who are experts in Blockchain.

    Blockchain Training in Bangalore

    Blockchain course in bangalore

    Blockchain in bangalore

    Blockchain classes in bangalore

    Blockchain course institute in bangalore

    Agile testing course and Certification course syllabus

    best Blockchain course

    Blockchain course centers

    ReplyDelete
  49. This article was really helpful. The tip to clarify the topic was good for me to read, I liked your ideas, keep continue.

    norton.com/setup

    ReplyDelete
  50. Hello Admin!

    Thanks for the post. It was very interesting and meaningful. I really appreciate it! Keep updating stuffs like this. If you are looking for the Advertising Agency in Chennai | Printing in Chennai , Visit Inoventic Creative Agency Today..

    ReplyDelete