PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/articles/virtual-machines/virtual-machines-windows-ps-manage.md

https://gitlab.com/yeah568/azure-content
Markdown | 223 lines | 168 code | 55 blank | 0 comment | 0 complexity | 1cb6117eaa7175f842e286a8d51eda05 MD5 | raw file
  1. <properties
  2. pageTitle="Manage VMs using Resource Manager and PowerShell | Microsoft Azure"
  3. description="Manage virtual machines using Azure Resource Manager and PowerShell."
  4. services="virtual-machines-windows"
  5. documentationCenter=""
  6. authors="davidmu1"
  7. manager="timlt"
  8. editor=""
  9. tags="azure-resource-manager"/>
  10. <tags
  11. ms.service="virtual-machines-windows"
  12. ms.workload="na"
  13. ms.tgt_pltfrm="vm-windows"
  14. ms.devlang="na"
  15. ms.topic="article"
  16. ms.date="06/07/2016"
  17. ms.author="davidmu"/>
  18. # Manage Azure Virtual Machines using Resource Manager and PowerShell
  19. ## Install Azure PowerShell
  20. See [How to install and configure Azure PowerShell](../powershell-install-configure.md) for information about how to install the latest version of Azure PowerShell, select the subscription that you want to use, and sign in to your Azure account.
  21. ## Set variables
  22. All of the commands in the article require the name of the resource group where the virtual machine is located and the name of the virtual machine to manage. Replace the value of **$rgName** with the name of the resource group that contains the virtual machine. Replace the value of **$vmName** with the name of the VM. Create the variables.
  23. $rgName = "resource-group-name"
  24. $vmName = "VM-name"
  25. ## Display information about a virtual machine
  26. Get the virtual machine information.
  27. Get-AzureRmVM -ResourceGroupName $rgName -Name $vmName
  28. It returns something like this:
  29. ResourceGroupName : rg1
  30. Id : /subscriptions/{subscription-id}/resourceGroups/
  31. rg1/providers/Microsoft.Compute/virtualMachines/vm1
  32. Name : vm1
  33. Type : Microsoft.Compute/virtualMachines
  34. Location : centralus
  35. Tags : {}
  36. AvailabilitySetReference : {
  37. "id": "/subscriptions/{subscription-id}/resourceGroups/
  38. rg1/providers/Microsoft.Compute/availabilitySets/av1"
  39. }
  40. Extensions : []
  41. HardwareProfile : {
  42. "vmSize": "Standard_A0"
  43. }
  44. InstanceView : null
  45. NetworkProfile : {
  46. "networkInterfaces": [
  47. {
  48. "properties.primary": null,
  49. "id": "/subscriptions/{subscription-id}/resourceGroups/
  50. rg1/providers/Microsoft.Network/networkInterfaces/nc1"
  51. }
  52. ]
  53. }
  54. OSProfile : {
  55. "computerName": "vm1",
  56. "adminUsername": "myaccount1",
  57. "adminPassword": null,
  58. "customData": null,
  59. "windowsConfiguration": {
  60. "provisionVMAgent": true,
  61. "enableAutomaticUpdates": true,
  62. "timeZone": null,
  63. "additionalUnattendContents": [],
  64. "winRM": null
  65. },
  66. "linuxConfiguration": null,
  67. "secrets": []
  68. }
  69. Plan : null
  70. ProvisioningState : Succeeded
  71. StorageProfile : {
  72. "imageReference": {
  73. "publisher": "MicrosoftWindowsServer",
  74. "offer": "WindowsServer",
  75. "sku": "2012-R2-Datacenter",
  76. "version": "latest"
  77. },
  78. "osDisk": {
  79. "osType": "Windows",
  80. "encryptionSettings": null,
  81. "name": "osdisk",
  82. "vhd": {
  83. "Uri": "http://sa1.blob.core.windows.net/vhds/osdisk1.vhd"
  84. }
  85. "image": null,
  86. "caching": "ReadWrite",
  87. "createOption": "FromImage"
  88. }
  89. "dataDisks": [],
  90. }
  91. DataDiskNames : {}
  92. NetworkInterfaceIDs : {/subscriptions/{subscription-id}/resourceGroups/
  93. rg1/providers/Microsoft.Network/networkInterfaces/nc1}
  94. ## Start a virtual machine
  95. Start the virtual machine.
  96. Start-AzureRmVM -ResourceGroupName $rgName -Name $vmName
  97. After a few minutes, it returns something like this:
  98. RequestId IsSuccessStatusCode StatusCode ReasonPhrase
  99. --------- ------------------- ---------- ------------
  100. True OK OK
  101. ## Stop a virtual machine
  102. Stop the virtual machine.
  103. Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName
  104. You're asked for confirmation:
  105. Virtual machine stopping operation
  106. This cmdlet will stop the specified virtual machine. Do you want to continue?
  107. [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):
  108. Enter **Y** to stop the virtual machine.
  109. After a few minutes, it returns something like this:
  110. RequestId IsSuccessStatusCode StatusCode ReasonPhrase
  111. --------- ------------------- ---------- ------------
  112. True OK OK
  113. ## Restart a virtual machine
  114. Restart the virtual machine.
  115. Restart-AzureRmVM -ResourceGroupName $rgName -Name $vmName
  116. It returns something like this:
  117. RequestId IsSuccessStatusCode StatusCode ReasonPhrase
  118. --------- ------------------- ---------- ------------
  119. True OK OK
  120. ## Delete a virtual machine
  121. Delete the virtual machine.
  122. Remove-AzureRmVM -ResourceGroupName $rgName Name $vmName
  123. > [AZURE.NOTE] You can use the **-Force** parameter to skip the confirmation prompt.
  124. You're asked for confirmation if you didn't use the -Force parameter:
  125. Virtual machine removal operation
  126. This cmdlet will remove the specified virtual machine. Do you want to continue?
  127. [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):
  128. It returns something like this:
  129. RequestId IsSuccessStatusCode StatusCode ReasonPhrase
  130. --------- ------------------- ---------- ------------
  131. True OK OK
  132. ## Update a virtual machine
  133. This example shows how to update the size of the virtual machine.
  134. $vmSize = "Standard_A1"
  135. $vm = Get-AzureRmVM -ResourceGroupName $rgName -Name $vmName
  136. $vm.HardwareProfile.vmSize = $vmSize
  137. Update-AzureRmVM -ResourceGroupName $rgName -VM $vm
  138. It returns something like this:
  139. RequestId IsSuccessStatusCode StatusCode ReasonPhrase
  140. --------- ------------------- ---------- ------------
  141. True OK OK
  142. See [Sizes for virtual machines in Azure](virtual-machines-windows-sizes.md) for a list of available sizes for a virtual machine.
  143. ## Add a data disk to a virtual machine
  144. This example shows how to add a data disk to an existing virtual machine.
  145. $vm = Get-AzureRmVM -ResourceGroupName $rgName -Name $vmName
  146. Add-AzureRmVMDataDisk -VM $vm -Name "disk-name" -VhdUri "https://mystore1.blob.core.windows.net/vhds/datadisk1.vhd" -LUN 0 -Caching ReadWrite -DiskSizeinGB 1 -CreateOption Empty
  147. Update-AzureRmVM -ResourceGroupName $rgName -VM $vm
  148. The disk that you add is not initialized. To initialize the disk, you can log in to it and use disk management. If you installed WinRM and a certificate on it when you created it, you can use remote PowerShell to initialize the disk. You can also use a custom script extension:
  149. $location = "location-name"
  150. $scriptName = "script-name"
  151. $fileName = "script-file-name"
  152. Set-AzureRmVMCustomScriptExtension -ResourceGroupName $rgName -Location $locName -VMName $vmName -Name $scriptName -TypeHandlerVersion "1.4" -StorageAccountName "mystore1" -StorageAccountKey "primary-key" -FileName $fileName -ContainerName "scripts"
  153. The script file can contain something like this to initialize the disks:
  154. $disks = Get-Disk | Where partitionstyle -eq 'raw' | sort number
  155. $letters = 70..89 | ForEach-Object { ([char]$_) }
  156. $count = 0
  157. $labels = @("data1","data2")
  158. foreach($d in $disks) {
  159. $driveLetter = $letters[$count].ToString()
  160. $d |
  161. Initialize-Disk -PartitionStyle MBR -PassThru |
  162. New-Partition -UseMaximumSize -DriveLetter $driveLetter |
  163. Format-Volume -FileSystem NTFS -NewFileSystemLabel $labels[$count] `
  164. -Confirm:$false -Force
  165. $count++
  166. }
  167. ## Next Steps
  168. If there were issues with a deployment, you might take a look at [Troubleshooting resource group deployments with Azure Portal](../resource-manager-troubleshoot-deployments-portal.md)