2016-08-23 21:08:42 +02:00
### The techniques used in this demo are documented at
### https://azure.microsoft.com/en-us/documentation/articles/powershell-azure-resource-manager/
### Import AzureRM.Profile.NetCore.Preview and AzureRM.Resources.NetCore.Preview modules.
### AzureRM.NetCore.Preview is a wrapper module that pulls in these modules
###
### Because of issue https://github.com/PowerShell/PowerShell/issues/1618,
### currently you will not be able to use "Install-Module AzureRM.NetCore.Preview" from
### PowerShellGallery. You can use the following workaround until the issue is fixed:
###
### Install-Package -Name AzureRM.NetCore.Preview -Source https://www.powershellgallery.com/api/v2 -ProviderName NuGet -ExcludeVersion -Destination <Folder you want this to be installed>
###
2016-09-12 19:08:17 +02:00
### Ensure $env:PSMODULEPATH is updated with the location you used to install.
2016-08-23 21:08:42 +02:00
Import-Module AzureRM . NetCore . Preview
### Supply your Azure Credentials
2016-09-12 19:08:17 +02:00
Login-AzureRmAccount
2016-08-23 21:08:42 +02:00
2016-09-12 19:08:17 +02:00
### Specify a name for Azure Resource Group
$resourceGroupName = " PSAzDemo " + ( New-Guid | % guid ) -replace " - " , " "
2016-08-23 21:08:42 +02:00
$resourceGroupName
2017-01-16 22:31:14 +01:00
### Create a new Azure Resource Group
2016-09-12 19:08:17 +02:00
New-AzureRmResourceGroup -Name $resourceGroupName -Location " West US "
2016-08-23 21:08:42 +02:00
### Deploy an Ubuntu 14.04 VM using Resource Manager cmdlets
2017-01-16 22:31:14 +01:00
### Template is available at
2016-08-23 21:08:42 +02:00
### http://armviz.io/#/?load=https:%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2F101-vm-simple-linux%2Fazuredeploy.json
$dnsLabelPrefix = $resourceGroupName | % tolower
$dnsLabelPrefix
2016-09-12 19:08:17 +02:00
$password = ConvertTo-SecureString -String " PowerShellRocks! " -AsPlainText -Force
New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile . / Compute-Linux . json -adminUserName psuser -adminPassword $password -dnsLabelPrefix $dnsLabelPrefix
2016-08-23 21:08:42 +02:00
### Monitor the status of the deployment
2017-01-16 22:31:14 +01:00
Get-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName
2016-08-23 21:08:42 +02:00
### Discover the resources we created by the previous deployment
2016-09-12 19:08:17 +02:00
Find-AzureRmResource -ResourceGroupName $resourceGroupName | select Name , ResourceType , Location
2016-08-23 21:08:42 +02:00
### Get the state of the VM we created
### Notice: The VM is in running state
2016-09-12 19:08:17 +02:00
Get-AzureRmResource -ResourceName MyUbuntuVM -ResourceType Microsoft . Compute / virtualMachines -ResourceGroupName $resourceGroupName -ODataQuery '$expand=instanceView' | % properties | % instanceview | % statuses
2016-08-23 21:08:42 +02:00
2016-09-12 19:08:17 +02:00
### Discover the operations we can perform on the compute resource
2016-08-23 21:08:42 +02:00
### Notice: Operations like "Power Off Virtual Machine", "Start Virtual Machine", "Create Snapshot", "Delete Snapshot", "Delete Virtual Machine"
2016-09-12 19:08:17 +02:00
Get-AzureRmProviderOperation -OperationSearchString Microsoft . Compute / * | select OperationName , Operation
2016-08-23 21:08:42 +02:00
### Power Off the Virtual Machine we created
2017-01-16 22:31:14 +01:00
Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType Microsoft . Compute / virtualMachines -ResourceName MyUbuntuVM -Action poweroff
2016-08-23 21:08:42 +02:00
2016-09-12 19:08:17 +02:00
### Check the VM state again. It should be stopped now.
Get-AzureRmResource -ResourceName MyUbuntuVM -ResourceType Microsoft . Compute / virtualMachines -ResourceGroupName $resourceGroupName -ODataQuery '$expand=instanceView' | % properties | % instanceview | % statuses
2016-08-23 21:08:42 +02:00
### As you know, you may still be incurring charges even if the VM is in stopped state
### Deallocate the resource to avoid this charge
2017-01-16 22:31:14 +01:00
Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType Microsoft . Compute / virtualMachines -ResourceName MyUbuntuVM -Action deallocate
2016-08-23 21:08:42 +02:00
### The following command removes the Virtual Machine
2017-01-16 22:31:14 +01:00
Remove-AzureRmResource -ResourceName MyUbuntuVM -ResourceType Microsoft . Compute / virtualMachines -ResourceGroupName $resourceGroupName
2016-08-23 21:08:42 +02:00
### Look at the resources that still exists
2016-09-12 19:08:17 +02:00
Find-AzureRmResource -ResourceGroupName $resourceGroupName | select Name , ResourceType , Location
2016-08-23 21:08:42 +02:00
2016-09-12 19:08:17 +02:00
### Remove the resource group and its resources
2017-01-16 22:31:14 +01:00
Remove-AzureRmResourceGroup -Name $resourceGroupName