Azure Cli

in Azure, Informatica

Azure Cli & PowerShell – Tips

Reading Time: 2 minutes

In questo post ho raccolto una serie di Tips legati alla Azure Cli e a PowerShell. Si tratta di materiale che ho raccolto nell’ultimo periodo seguendo vari percorsi di certificazione in ambito Azure.

Creazione di una Azure App

# generate a unique name and store as a shell variable
webappname=mywebapp$RANDOM

# create a resource group
az group create --location westeurope --name myResourceGroup

# create an App Service plan
az appservice plan create --name $webappname --resource-group myResourceGroup --sku FREE

# create a Web App
az webapp create --name $webappname --resource-group myResourceGroup --plan $webappname

# store a repository url as a shell variable
gitrepo=https://github.com/Azure-Samples/php-docs-hello-world

# deploy code from a Git repository
az webapp deployment source config --name $webappname --resource-group myResourceGroup --repo-url $gitrepo --branch master --manual-integration

# print out the FQDN for the Web App
echo http://$webappname.azurewebsites.net

Abilitazione Encryption di un Azure Disk

Creazione di un Azure Key Vault

az keyvault create \
 --name "kvDemo006" \
 --resource-group "demo001" \
 --location centralUS \
 --enabled-for-disk-encryption True

Abilitazione dell’encryption sulla VM

az vm encryption enable \
 --resource-group "kvDemo006" \
 --name "devUScWeb003" \
 --disk-encryption-keyvault "demo006" \
 --volume-type "all"

Visualizzazione dello stato dell’encryption

az vm encryption show \
 --resource-group "demo006" \
 --name "devUScWeb003"`

Creazione di una Azure VM tramite Powershell

Il primo passaggio riguarda il recupero delle locations disponibili

Get-AzureRmLocation | Format-Table

e delle Azure Resource Groups disposinibili per la sottoscrizione:

Get-AzureRmResourceGroup | select ResourceGroupName

Procediamo con il setup della variabili per il deploy:

$VMLocalAdminUser = "myAdminUser"
$VMLocalAdminSecurePassword = ConvertTo-SecureString "MyStrongPassword@123456789" -AsPlainText -Force
$LocationName = "centralus"
$ResourceGroupName = "demo001PS"
$ComputerName = "MyVM"
$VMName = "devUScWeb002"
$VMSize = "Standard_DS3"
$NICName = "devUScWeb002-ip"

Creiamo il nuovo Resources Group:

New-AzureRmResourceGroup -Name $ResourceGroupName -Location $LocationName

La Virtual Network associata alla nuova VM:

$virtualNetwork = New-AzVirtualNetwork `
  -ResourceGroupName $ResourceGroupName `
  -Location $LocationName `
  -Name $VMName `
  -AddressPrefix 10.0.0.0/16

$subnetConfig = Add-AzVirtualNetworkSubnetConfig `
  -Name default `
  -AddressPrefix 10.0.0.0/24 `
  -VirtualNetwork $virtualNetwork

$virtualNetwork | Set-AzVirtualNetwork

$nic = New-AzureRmNetworkInterface `
 -Name "NIC1" `
 -ResourceGroupName $ResourceGroupName `
 -Location $LocationName `
 -SubnetId $virtualNetwork.Subnets[0].Id `
 -IpConfigurationName "IPConfig1" `
 -DnsServer "8.8.8.8", "8.8.4.4"

ed infine la VM:

$Credential = New-Object System.Management.Automation.PSCredential ($VMLocalAdminUser, $VMLocalAdminSecurePassword)

$VirtualMachine = New-AzureRmVMConfig `
 -VMName $VMName -VMSize $VMSize

$VirtualMachine = Set-AzureRmVMOperatingSystem `
 -VM $VirtualMachine `
 -Windows `
 -ComputerName $ComputerName `
 -Credential $Credential `
 -ProvisionVMAgent -EnableAutoUpdate

$VirtualMachine = Add-AzureRmVMNetworkInterface `
 -VM $VirtualMachine `
 -Id $NIC.Id

$VirtualMachine = Set-AzureRmVMSourceImage `
 -VM $VirtualMachine `
 -PublisherName 'MicrosoftWindowsServer' `
 -Offer 'WindowsServer' `
 -Skus '2012-R2-Datacenter' `
 -Version latest

$job = New-AzureRmVM `
 -ResourceGroupName $ResourceGroupName `
 -Location $LocationName `
 -VM $VirtualMachine `
 -AsJob