A few PowerShell (in Azure from virtual machines) impressions summarized!



Hi Azure Friends,


In this article I have summarized a few tasks that I used on a project. This is certainly not an exhaustive summary when it comes to working with Azure and PowerShell, I just wanted to summarize a few points.


I used the PowerShell ISE for this configuration. But you are also very welcome to use Visual Studio Code, just as you wish. Please start with the following steps to begin the deployment (the Hashtags are comments):


#The first two lines have nothing to do with the configuration, but make some space below in the blue part of the ISE

Set-Location C:\Temp


#So that you can carry out the configuration, you need the necessary cmdlets, these are contained in the module Az (is the higher-level module from a number of submodules)

Install-Module -Name Az -Force -AllowClobber -Verbose


#Log into Azure


#Select the correct subscription



Get-AzSubscription -SubscriptionName "your subscription name" | Select-AzSubscription


#Some variables
$RgName = "tw-rg01"
$vmName = "tw-win2019"
$Location = "westeurope"


#Infos about the vm's


#Get VM status and add results to variable
$AllVMs = Get-AzVM -ResourceGroupName $RgName -Status | Select-Object ResourceGroupName,Name,Location, @{ label = "VMStatus"; Expression = { $_.PowerState } }


#Display the results
$AllVMs | Format-Table -Auto -Wrap

#Or get VM status where server names match VM and add results to variable
$AllVMs = Get-AzVM -ResourceGroupName $RgName -Status | Where-Object {$_.name -match "srv"} | Select-Object ResourceGroupName,Name,Location, @{ label = "VMStatus"; Expression = { $_.PowerState } }

#Display the results
$AllVMs | Format-Table -Auto -Wrap

#Display results in new window
$AllVMs | Out-GridView -Title "Azure VMs"

#List VMs in a resource group
Get-AzVM -ResourceGroupName $RgName


#Get all virtual machines in the location
Get-AzVM -Location $Location


#Get information about a VM
Get-AzVM -ResourceGroupName $RgName -Name $vmName


#We start just one vm
Start-AzVM -ResourceGroupName $RgName -Name $vmName


#Stop a VM
Stop-AzVM -ResourceGroupName $RgName -Name $vmName


#Restart a running VM
Restart-AzVM -ResourceGroupName $RgName -Name $vmName


#For which VM's is Auto-Shutdown configured?
(Get-AzResource -ResourceType Microsoft.DevTestLab/schedules -Expandproperties).Properties | Select-Object -Property targetResourceId, taskType, status | Format-List


I am absolutely aware that this is nothing overwhelming. But I would like to share a few experiences with you.


I hope this article was useful. Best regards, Tom Wechsler


P.S. All scripts (#PowerShell, Azure CLI, #Terraform, #ARM) that I use can be found on github! https://github.com/tomwechsler


0 Replies