2020-03-24 19:08:37 +01:00
# Copyright (c) Microsoft Corporation.
2018-02-13 18:23:53 +01:00
# Licensed under the MIT License.
### The techniques used in this demo are documented at
2018-06-08 07:49:03 +02:00
### https://azure.microsoft.com/documentation/articles/powershell-azure-resource-manager/
2016-08-23 21:08:42 +02:00
### 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>
###
2017-03-15 20:04:28 +01: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
2017-07-22 06:03:49 +02:00
$resourceGroupName = " PSAzDemo " + ( New-Guid | ForEach-Object 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
2017-07-22 06:03:49 +02:00
$dnsLabelPrefix = $resourceGroupName | ForEach-Object tolower
2016-08-23 21:08:42 +02:00
$dnsLabelPrefix
2017-09-27 20:54:22 +02:00
#[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc secret.")]
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
2017-07-22 06:03:49 +02:00
Find-AzureRmResource -ResourceGroupName $resourceGroupName | Select-Object 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
2017-07-22 06:03:49 +02:00
Get-AzureRmResource -ResourceName MyUbuntuVM -ResourceType Microsoft . Compute / virtualMachines -ResourceGroupName $resourceGroupName -ODataQuery '$expand=instanceView' | ForEach-Object properties | ForEach-Object instanceview | ForEach-Object 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"
2017-07-22 06:03:49 +02:00
Get-AzureRmProviderOperation -OperationSearchString Microsoft . Compute / * | Select-Object 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.
2017-07-22 06:03:49 +02:00
Get-AzureRmResource -ResourceName MyUbuntuVM -ResourceType Microsoft . Compute / virtualMachines -ResourceGroupName $resourceGroupName -ODataQuery '$expand=instanceView' | ForEach-Object properties | ForEach-Object instanceview | ForEach-Object 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
2017-07-22 06:03:49 +02:00
Find-AzureRmResource -ResourceGroupName $resourceGroupName | Select-Object 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