dcsimg
Read more on "Server OS Spotlight" »

9 Quick Yet Powerful PowerShell Cmdlets for Azure Virtual Machines

By Nirmal Sharma (Send Email)
Posted August 21, 2018


Microsoft has put in a lot of effort into PowerShell scripting. While PowerShell scripting originated as a means to efficiently manage Windows operating systems, Microsoft has since extended the capabilities of PowerShell to automate a range of other tasks and systems, including Azure. In today's Server tutorial, we'll examine some of the useful PowerShell cmdlets you can use to interact with Azure resources.

Creating an Azure Virtual Machine

If you need to create a new Azure virtual machine for production or development use, you can use the New-AzureRMVM PowerShell cmdlet. You can specify parameters such as location of VHD, memory, processors, etc. To quickly create a virtual machine in Azure and with default configuration, just execute the following PowerShell command: Windows Server Tutorials

New-AzureRMVM -ResourceGroupName ResourceGroupHere -Name VMNameHere

While this command creates a virtual machine quickly in the Resource Group specified in the "-ResourceGroupName" parameter, the next command will create a virtual machine in Azure from a managed image.

New-AzureRMVM -ResourceGroupName ResourceGroupHere -Name VMNameHere -ImageName "YourImage" -Location "West-US"

Reporting Azure Virtual Machines

For reporting purposes, you might find the need to check how many Azure virtual machines have been deployed and are currently running. Microsoft provides the Get-AzureRMVM PowerShell cmdlet, which can be used to report virtual machines from an Azure subscription and/or from an Azure resource group. The next set of PowerShell commands provides some examples of using Get-AzureRMVM:

To get all the virtual machines from an Azure Resource Group, execute this PowerShell command:

Get-AzureRMVM -ResourceGroupName "ResourceGroupNameHere"

To export output to a CSV file, add the Export-CSV cmdlet as shown in below:

Get-AzureRMVM -ResourceGroupName "ResourceGroupNameHere" | Export-CSV C:\Temp\AllAzureVMs.CSV -NoTypeInfo

In Get-AzureRMVM you can also specify the properties that you would like to be reported when querying virtual machines in Azure. For example, the following PowerShell command only collects the type of Operating System running on virtual machines:

Get-AzureRMVM | Select Name,@{Name="OSType"; Expression={$_.StorageProfile.OSDisk.OSType}}

By using the method shown in the command above, you can access nested properties of Azure virtual machines.

If you wish to list all the virtual machines in an Azure subscription, simply execute the "Get-AzureRMVM" command in an elevated PowerShell window. If you would like to list virtual machines from Resource Groups listed in a text file, using the following PowerShell script would work:

$ResGroups = "C:\Temp\ResGroups.TXT"
$ReportFile = "C:\Temp\AllVMsInAzure.CSV"
$STR = "VM Name, Resource Group, Location, Status Code"
Add-Content $ReportFile $STR
Foreach ($ResGroup in Get-Content $ResGroups)
{
$allVMsInReg = Get-AzureRMVM -ResourceGroupName "$ResGroup"
ForEach ($AllVMs in $AllVMsInRes)
{
$VMLocation = $AllVMs.Location
$VMName = $AllVMs.Name
$VMStatus = $AllVMs.StatusCode

$STR = $VMName+","+$ResGroup+","+$VMLocation+","+$VMStatus
Add-Content $ReportFile $STR
}
}

When you have finished executing the above script, a report file will be generated that contains the name of the virtual machine, the resource group name of the virtual machine, the virtual machine Azure location and the status code indicating the overall status of the virtual machine.

Staring, Stopping, and Restarting Azure Virtual Machines

From time to time you may need to stop and start Azure virtual machines in order to reduce billing costs. For example, you may not want to run development virtual machines if they are not in use. Similarly, if you have assigned temporary virtual machines to a third-party vendor, you may want to remove them on your end as quickly as possible.

To stop an Azure virtual machine, you can use the Stop-AzureRMVM PowerShell cmdlet as shown in the following PowerShell command:

Stop-AzureRMVM -ResourceGroupName "ResourceGroupName" -Name VMNameToStopHere

And the next PowerShell script can be used to stop specific virtual machines specified in a text file. Let's assume the text file contains the list of virtual machines to be stopped. Once you have created the text file that contains the virtual machines to be stopped, run the following PowerShell script:

$VMFile = "C:\Temp\AllVMsToStop.TXT"
Foreach ($ThisVM in Get-Content $VMFile)
{
Stop-AzureRMVM -Name $ThisVM
}

Starting an Azure virtual machine is similar to stopping an Azure virtual machine. However, you will of course need to use the Start-AzureRMVM PowerShell cmdlet in place of the Stop-AzureRMVM cmdlet as shown in the following command:

Start-AzureRMVM -ResourceGroupName "ResourceGroupName" -Name VMNameToStopHere

If you run into any issues with the Azure virtual machine and are not able to take control via the Azure management portal or if you have any issues accessing the virtual machine, what you can do is restart the virtual machine using the PowerShell command below:

Restart-AzureRMVM -ResourceGroupName "ResourceGroupName" -Name VMNameToStopHere

Conclusion

We provided some very handy Azure PowerShell commands you can use when managing Azure virtual machines. We explained how to create a simple Azure virtual machine or an Azure virtual machine from a managed image. We also explained how to list all Azure virtual machines and/or virtual machines in resource groups specified in a text file. Finally, our server tutorial provideed some useful PowerShell commands to stop, start and restart Azure virtual machines.


Nirmal Sharma is a MCSEx3, MCITP and Microsoft MVP in Directory Services. He specializes in directory services, Microsoft Azure, Failover clusters, Hyper-V, System Center and Exchange Servers, and has been involved with Microsoft technologies since 1994. In his spare time, he likes to help others and share some of his knowledge by writing tips and articles on various sites and contributing to Health Packs for ADHealthProf.ITDynamicPacks.Net solutions. Nirmal can be reached at nirmal_sharma@mvps.org.

Follow ServerWatch on Twitter and on Facebook

Page 1 of 1

Read more on "Server OS Spotlight" »

Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.


 

 


Thanks for your registration, follow us on our social networks to keep up-to-date