This post will be updated continuously with new Azure RM cmdlets, which are useful for the daily work at office.

Networking

In this section, we can find cmdlets for Azure Network Resources.

Change The Public IP State : (Set-AzureRmPublicIpAddress)

Before executing one of the following two scripts we must make sure that the Public IP Address is not associated with a running Network Interface.

Login-AzureRMaccount

$PIPName = 'Public IP Name'
$ResourceGroupName = 'Resource Group Name'

$PIP = Get-AzureRmPublicIpAddress -Name $PIPName -ResourceGroupName $ResourceGroupName
$PIP.PublicIpAllocationMethod = "Static"  
Set-AzureRmPublicIpAddress -PublicIpAddress $PIP
Get-AzureRmPublicIpAddress -Name $PIPName -ResourceGroupName $ResourceGroupName
Login-AzureRMaccount

$PIPName = 'Public IP Name'
$ResourceGroupName = 'Resource Group Name'

$PIP = Get-AzureRmPublicIpAddress -Name $PIPName -ResourceGroupName $ResourceGroupName
$PIP.PublicIpAllocationMethod = "Dynamic"  
Set-AzureRmPublicIpAddress -PublicIpAddress $PIP
Get-AzureRmPublicIpAddress -Name $PIPName -ResourceGroupName $ResourceGroupName

Desired State Configuration

Working script for DSC deployment on a single VM. You need to create a storage account in the same location as the rest of the resources. The configuration file will be stored inside that storage account.

You can find a configuration file to install IIS here.

$resourceGroup = 'Name of RG'
$location = 'Location of resources'
$vmName = 'Name of VM'
$storageName = 'Storage account to upload the configuration file'
#Publish the configuration script to user storage
Publish-AzureRmVMDscConfiguration -ConfigurationPath .\iisInstall.ps1 -ResourceGroupName $resourceGroup -StorageAccountName $storageName -force
#Set the VM to run the DSC configuration
Set-AzureRmVMDscExtension -Version 2.76 `
-ResourceGroupName $resourceGroup `
-VMName $vmName `
-ArchiveStorageAccountName $storageName `
-ArchiveBlobName "Name-of-the-configuration-file.zip" `
-AutoUpdate: $true `
-ConfigurationName "Name-of-the-configuration-file"

Azure Hybrid Benefit

In this section you can find scripts  for the Azure Hybrid Benefits.

$vms = Get-AzureRMVM
foreach ($vm in $vms) {"VM Name: " + $vm.Name, " Azure Hybrid Benefit for Windows Server: "+
$vm.LicenseType}

Schedule VM resizing

Prerequisites: Save your account profile in local file.

Login-AzureRmAccount
#Save profile to local json file
Save-AzureRmContext -Path “path to the profile json file”

Save as ps1 file and execute from Task Scheduler
https://community.spiceworks.com/how_to/17736-run-powershell-scripts-from-task-scheduler

#Connect to Azure account from the local json file

Import-AzureRmContext -Path "path to the profile json file"

#Resize the VM
Set-AzureRmContext -SubscriptionId "ID of the subscription"
$resourceGroup = "name of RG"
$vmName = "name of VM"
$vm = Get-AzureRmVM -ResourceGroupName $resourceGroup -VMName $vmName 
$vm.HardwareProfile.VmSize = 'Standard_B2ms'
Update-AzureRmVM -VM $vm -ResourceGroupName $resourceGroup




Share This