Freigeben über


Managing Azure VMs with Azure PowerShell-Part 1

 Managing Azure VMs with Azure PowerShell-Part 1

Working on creating some VMs in my lab environment, thought why not post it as a reference for others

Service Configurations
1. Storage Accounts: satishlocalstorage, satishvnet1sa
2. Cloud Service: satishcloud, satishVnet1cloudsvc
3. VNET: Satishnetwork (infrasubnet, websubnet SQLsubnet), Satishvnet1 (infrasubnet1, appsubnet1)

Goal

1. Deploy  VMs  in a specific  storage account, cloud service and  VNET
2. Setting a static IP address for your existing VM
3. Create a New VM and configure a static IP during the VM deployment
4. Capturing a Sysprep’ed / generalized VM testvm2 as a template for future deployments  & deploy the image
5. Capturing a running VM image for a quick snapshot & redeploy the VM to a good point in time from the latest capture

1. Deploy VMs in a specific storage account, cloud service and VNET
Associate subscription to the storageaccount
PS C:\> Set-AzureSubscription -SubscriptionName "yoursubscriptionaname" -CurrentStorageAccountName "satishlocalstorage"
  (All VM’s will now be created in “satishlocalstorage” account” by default

Create a VM and from a latest image release date(best practice)
PS C:\> $image=(get-azurevmimage | where {$_.imagefamily -eq "windows Server 2012 R2 DataCenter"} | sort publisheddate -descending | select-object -first 1).imagename

PS C:\> New-AzureVMConfig -Name "testvm1" -InstanceSize "Basic_A1" -imagename $image | add-azureprovisioningconfig -windows -adminusername userid -Password yourpassword | set-azuresubnet -subnetnames "infrasubnet" | New-AzureVM -ServiceName "satishcloud" –vnetname “satishnetwork”

 VM status
PS C:\> Get-AzureVM -ServiceName "satishcloud" -name "testvm1" | select-object name, ipaddress, powerstate, servicename

Name IpAddress PowerState ServiceName
---- --------- ---------- -----------
testvm1 10.101.2.4 Started satishcloud

2. Setting a static IP address for your existing VM

Configure VM “testvm1” with a static ip address of 10.101.2.99 or 10.101.2.5(whichever is available)

PS C:\> Test-AzureStaticVNetIP -vnetname "satishnetwork" -IPAddress "10.101.2.5"

 PS C:\> Test-AzureStaticVNetIP -vnetname "satishnetwork" -IPAddress "10.101.2.99"
                               IsAvailable : True
                                   AvailableAddresses : {}
                                  OperationDescription : Test-AzureStaticVNetIP
                                  OperationId : d497b983-d876-3c22-a266-c4eb0c0c773e
                                   OperationStatus : Succeeded

PS C:\> get-azurevm -servicename "satishcloud" -name "testvm1" |Set-AzureStaticVNetIP - iPAddress "10.101.2.99" | update-Azurevm
         (This command will restart and update the VM with new static IP address)

PS C:\> Get-AzureVM -ServiceName "satishcloud" -name "testvm1" | select-object name, ipaddress, powerstate, servicename

                        Name IpAddress PowerState ServiceName
                               ---- --------- ---------- -----------
                              testvm1 10.101.2.99 Started satishcloud

PS C:\> $staticipvm=get-azurevm -servicename 'satishcloud' -name 'testvm1'
PS C:\> Get-AzureStaticVNetIP -vm $staticipvm

                             IPAddress
                                ---------
                             10.101.2.99
   If you want to remove static IP for a VM, run
Get-AzureVM -ServiceName “satishcloud” -Name “testvm1l”  | Remove-AzureStaticVNetIP | Update-AzureVM

3. Create a New VM and with a static IP address

PS C:\> $image=(get-azurevmimage | where {$_.imagefamily -eq "windows Server 2012 R2 DataCenter"} | sort publisheddate -descending | select-object -first 1).imagename

PS C:\> New-AzureVMConfig -Name "Testvm2" -InstanceSize "Basic_A1" -imagename $image | add-azureprovisioningconfig -windows -adminusername username -Password yourpassword | set-azuresubnet -subnetnames "infrasubnet" | Set-AzureStaticVNetIP -IPAddress "10.101.2.100" | New-AzureVM -ServiceName "satishcloud" –vnetname “satishnetwork”

4. Capturing a Sysprep’ed / generalized VM "testvm2" as a template for future deployments & deploy the image

PS C:\> Save-AzureVMImage -ServiceName satishcloud -Name testvm2 -ImageName webtemplate1 -ImageLabel webtemplate1 -OSState Generalized
(this command will delete the VM testvm2 and create an image from this VM

  Deploy a new VM called webvm1 from the capured generalized image webtemplate1

PS C:\> New-AzureVMConfig -Name "webvm1" -InstanceSize "Basic_A1" -imagename webtemplate1 | add-azureprovisioningconfig -windows -adminusername userid1 -Password yourpassword | set-azuresubnet -subnetnames "infrasubnet" | Set-AzureStaticVNetIP -IPAddress "10.101.2.100" | New-AzureVM -ServiceName "satishcloud" –vnetname “satishnetwork"

PS C:\> get-azurevm -ServiceName "satishcloud" -name "webvm1" | select-object name, ipaddress, status
                                         Name IpAddress Status
                                            ---- --------- ------
                                         webvm1 10.101.2.100 ReadyRole

5. Capturing a running VM "webvm1" for a quick snapshot & redeploy the VM to a good point in time from the latest capture 

PS C:\> Save-AzureVMImage -ServiceName satishcloud -Name webvm1 -ImageName webvm1snapshot -ImageLabel webvm1snapshot -OSState Specialized
PS C:\> Get-AzureVMImage | where {$_.imagename -like "web*"} |select-object imagename,category,RoleName

                         

                                 ImageName Category RoleName
                               --------- -------- --------
                               webtemplate1 User Testvm2
                              webvm1snapshot User webvm1

(Next I Deleted the VM webvm1 and redeployed it, all previous configurations were intact including domainjoin in my case 

 PS C:\> Remove-AzureVM -ServiceName "satishcloud" -name "webvm1" -DeleteVHD
PS C:\> New-AzureVMConfig -Name "webvm1" -InstanceSize "Basic_A1" -imagename webvm1snapshot | set-azuresubnet -subnetnames "infrasubnet" | Set-AzureStaticVNetIP -IPAddress "10.101.2.100" | New-AzureVM -ServiceName "satishcloud" –vnetname “satishnetwork”

Note: when the VM is back online,and when you try to login for the first time you will see a dialog pop up stating the VM was not properly shutdown, this may be an expected behavior, as we captured a running VM

Stopped all VMs in cloud service "satishcloud"

PS C:\> get-azurevm -ServiceName "satishcloud" | stop-azurevm

DISCLAIMER: This posting is provided "AS IS" with no warranties and confers no rights