/articles/virtual-machines/virtual-machines-windows-ps-manage.md
Markdown | 223 lines | 168 code | 55 blank | 0 comment | 0 complexity | 1cb6117eaa7175f842e286a8d51eda05 MD5 | raw file
- <properties
- pageTitle="Manage VMs using Resource Manager and PowerShell | Microsoft Azure"
- description="Manage virtual machines using Azure Resource Manager and PowerShell."
- services="virtual-machines-windows"
- documentationCenter=""
- authors="davidmu1"
- manager="timlt"
- editor=""
- tags="azure-resource-manager"/>
- <tags
- ms.service="virtual-machines-windows"
- ms.workload="na"
- ms.tgt_pltfrm="vm-windows"
- ms.devlang="na"
- ms.topic="article"
- ms.date="06/07/2016"
- ms.author="davidmu"/>
- # Manage Azure Virtual Machines using Resource Manager and PowerShell
- ## Install Azure PowerShell
-
- 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.
- ## Set variables
- 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.
- $rgName = "resource-group-name"
- $vmName = "VM-name"
- ## Display information about a virtual machine
- Get the virtual machine information.
-
- Get-AzureRmVM -ResourceGroupName $rgName -Name $vmName
- It returns something like this:
- ResourceGroupName : rg1
- Id : /subscriptions/{subscription-id}/resourceGroups/
- rg1/providers/Microsoft.Compute/virtualMachines/vm1
- Name : vm1
- Type : Microsoft.Compute/virtualMachines
- Location : centralus
- Tags : {}
- AvailabilitySetReference : {
- "id": "/subscriptions/{subscription-id}/resourceGroups/
- rg1/providers/Microsoft.Compute/availabilitySets/av1"
- }
- Extensions : []
- HardwareProfile : {
- "vmSize": "Standard_A0"
- }
- InstanceView : null
- NetworkProfile : {
- "networkInterfaces": [
- {
- "properties.primary": null,
- "id": "/subscriptions/{subscription-id}/resourceGroups/
- rg1/providers/Microsoft.Network/networkInterfaces/nc1"
- }
- ]
- }
- OSProfile : {
- "computerName": "vm1",
- "adminUsername": "myaccount1",
- "adminPassword": null,
- "customData": null,
- "windowsConfiguration": {
- "provisionVMAgent": true,
- "enableAutomaticUpdates": true,
- "timeZone": null,
- "additionalUnattendContents": [],
- "winRM": null
- },
- "linuxConfiguration": null,
- "secrets": []
- }
- Plan : null
- ProvisioningState : Succeeded
- StorageProfile : {
- "imageReference": {
- "publisher": "MicrosoftWindowsServer",
- "offer": "WindowsServer",
- "sku": "2012-R2-Datacenter",
- "version": "latest"
- },
- "osDisk": {
- "osType": "Windows",
- "encryptionSettings": null,
- "name": "osdisk",
- "vhd": {
- "Uri": "http://sa1.blob.core.windows.net/vhds/osdisk1.vhd"
- }
- "image": null,
- "caching": "ReadWrite",
- "createOption": "FromImage"
- }
- "dataDisks": [],
- }
- DataDiskNames : {}
- NetworkInterfaceIDs : {/subscriptions/{subscription-id}/resourceGroups/
- rg1/providers/Microsoft.Network/networkInterfaces/nc1}
- ## Start a virtual machine
- Start the virtual machine.
- Start-AzureRmVM -ResourceGroupName $rgName -Name $vmName
- After a few minutes, it returns something like this:
- RequestId IsSuccessStatusCode StatusCode ReasonPhrase
- --------- ------------------- ---------- ------------
- True OK OK
- ## Stop a virtual machine
- Stop the virtual machine.
- Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName
- You're asked for confirmation:
- Virtual machine stopping operation
- This cmdlet will stop the specified virtual machine. Do you want to continue?
- [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):
-
- Enter **Y** to stop the virtual machine.
- After a few minutes, it returns something like this:
- RequestId IsSuccessStatusCode StatusCode ReasonPhrase
- --------- ------------------- ---------- ------------
- True OK OK
- ## Restart a virtual machine
- Restart the virtual machine.
- Restart-AzureRmVM -ResourceGroupName $rgName -Name $vmName
- It returns something like this:
- RequestId IsSuccessStatusCode StatusCode ReasonPhrase
- --------- ------------------- ---------- ------------
- True OK OK
- ## Delete a virtual machine
- Delete the virtual machine.
- Remove-AzureRmVM -ResourceGroupName $rgName –Name $vmName
- > [AZURE.NOTE] You can use the **-Force** parameter to skip the confirmation prompt.
- You're asked for confirmation if you didn't use the -Force parameter:
- Virtual machine removal operation
- This cmdlet will remove the specified virtual machine. Do you want to continue?
- [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):
- It returns something like this:
- RequestId IsSuccessStatusCode StatusCode ReasonPhrase
- --------- ------------------- ---------- ------------
- True OK OK
- ## Update a virtual machine
- This example shows how to update the size of the virtual machine.
-
- $vmSize = "Standard_A1"
- $vm = Get-AzureRmVM -ResourceGroupName $rgName -Name $vmName
- $vm.HardwareProfile.vmSize = $vmSize
- Update-AzureRmVM -ResourceGroupName $rgName -VM $vm
-
- It returns something like this:
- RequestId IsSuccessStatusCode StatusCode ReasonPhrase
- --------- ------------------- ---------- ------------
- True OK OK
-
- See [Sizes for virtual machines in Azure](virtual-machines-windows-sizes.md) for a list of available sizes for a virtual machine.
- ## Add a data disk to a virtual machine
- This example shows how to add a data disk to an existing virtual machine.
- $vm = Get-AzureRmVM -ResourceGroupName $rgName -Name $vmName
- 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
- Update-AzureRmVM -ResourceGroupName $rgName -VM $vm
- 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:
- $location = "location-name"
- $scriptName = "script-name"
- $fileName = "script-file-name"
- Set-AzureRmVMCustomScriptExtension -ResourceGroupName $rgName -Location $locName -VMName $vmName -Name $scriptName -TypeHandlerVersion "1.4" -StorageAccountName "mystore1" -StorageAccountKey "primary-key" -FileName $fileName -ContainerName "scripts"
- The script file can contain something like this to initialize the disks:
- $disks = Get-Disk | Where partitionstyle -eq 'raw' | sort number
- $letters = 70..89 | ForEach-Object { ([char]$_) }
- $count = 0
- $labels = @("data1","data2")
- foreach($d in $disks) {
- $driveLetter = $letters[$count].ToString()
- $d |
- Initialize-Disk -PartitionStyle MBR -PassThru |
- New-Partition -UseMaximumSize -DriveLetter $driveLetter |
- Format-Volume -FileSystem NTFS -NewFileSystemLabel $labels[$count] `
- -Confirm:$false -Force
- $count++
- }
- ## Next Steps
- 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)