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

%3CLINGO-SUB%20id%3D%22lingo-sub-2591186%22%20slang%3D%22en-US%22%3EA%20few%20PowerShell%20(in%20Azure%20from%20virtual%20machines)%20impressions%20summarized!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2591186%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHi%20Azure%20Friends%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20article%20I%20have%20summarized%20a%20few%20tasks%20that%20I%20used%20on%20a%20project.%20This%20is%20certainly%20not%20an%20exhaustive%20summary%20when%20it%20comes%20to%20working%20with%20Azure%20and%20PowerShell%2C%20I%20just%20wanted%20to%20summarize%20a%20few%20points.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20used%20the%20PowerShell%20ISE%20for%20this%20configuration.%20But%20you%20are%20also%20very%20welcome%20to%20use%20Visual%20Studio%20Code%2C%20just%20as%20you%20wish.%26nbsp%3BPlease%20start%20with%20the%20following%20steps%20to%20begin%20the%20deployment%20(the%20Hashtags%20are%20comments)%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23The%20first%20two%20lines%20have%20nothing%20to%20do%20with%20the%20configuration%2C%20but%20make%20some%20space%20below%20in%20the%20blue%20part%20of%20the%20ISE%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ESet-Location%20C%3A%5CTemp%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EClear-Host%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23So%20that%20you%20can%20carry%20out%20the%20configuration%2C%20you%20need%20the%20necessary%20cmdlets%2C%20these%20are%20contained%20in%20the%20module%20Az%20(is%20the%20higher-level%20module%20from%20a%20number%20of%20submodules)%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EInstall-Module%20-Name%20Az%20-Force%20-AllowClobber%20-Verbose%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Log%20into%20Azure%3CBR%20%2F%3E%3CSTRONG%3EConnect-AzAccount%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Select%20the%20correct%20subscription%3CSTRONG%3E%3CBR%20%2F%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EGet-AzContext%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EGet-AzSubscription%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EGet-AzSubscription%20-SubscriptionName%20%22your%20subscription%20name%22%20%7C%20Select-AzSubscription%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Some%20variables%3CBR%20%2F%3E%3CSTRONG%3E%24RgName%20%3D%20%22tw-rg01%22%20%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%24vmName%20%3D%20%22tw-win2019%22%20%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%24Location%20%3D%20%22westeurope%22%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Infos%20about%20the%20vm's%3CBR%20%2F%3E%3CSTRONG%3EGet-AzVM%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Get%20VM%20status%20and%20add%20results%20to%20variable%3CBR%20%2F%3E%3CSTRONG%3E%24AllVMs%20%3D%20Get-AzVM%20-ResourceGroupName%20%24RgName%20-Status%20%7C%20Select-Object%20ResourceGroupName%2CName%2CLocation%2C%20%40%7B%20label%20%3D%20%22VMStatus%22%3B%20Expression%20%3D%20%7B%20%24_.PowerState%20%7D%20%7D%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Display%20the%20results%3CBR%20%2F%3E%3CSTRONG%3E%24AllVMs%20%7C%20Format-Table%20-Auto%20-Wrap%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%23Or%20get%20VM%20status%20where%20server%20names%20match%20VM%20and%20add%20results%20to%20variable%3CBR%20%2F%3E%3CSTRONG%3E%24AllVMs%20%3D%20Get-AzVM%20-ResourceGroupName%20%24RgName%20-Status%20%7C%20Where-Object%20%7B%24_.name%20-match%20%22srv%22%7D%20%7C%20Select-Object%20ResourceGroupName%2CName%2CLocation%2C%20%40%7B%20label%20%3D%20%22VMStatus%22%3B%20Expression%20%3D%20%7B%20%24_.PowerState%20%7D%20%7D%20%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%23Display%20the%20results%3CBR%20%2F%3E%3CSTRONG%3E%24AllVMs%20%7C%20Format-Table%20-Auto%20-Wrap%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%23Display%20results%20in%20new%20window%3CBR%20%2F%3E%3CSTRONG%3E%24AllVMs%20%7C%20Out-GridView%20-Title%20%22Azure%20VMs%22%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%23List%20VMs%20in%20a%20resource%20group%3CBR%20%2F%3E%3CSTRONG%3EGet-AzVM%20-ResourceGroupName%20%24RgName%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Get%20all%20virtual%20machines%20in%20the%20location%3CBR%20%2F%3E%3CSTRONG%3EGet-AzVM%20-Location%20%24Location%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Get%20information%20about%20a%20VM%3CBR%20%2F%3E%3CSTRONG%3EGet-AzVM%20-ResourceGroupName%20%24RgName%20-Name%20%24vmName%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23We%20start%20just%20one%20vm%3CBR%20%2F%3E%3CSTRONG%3EStart-AzVM%20-ResourceGroupName%20%24RgName%20-Name%20%24vmName%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Stop%20a%20VM%3CBR%20%2F%3E%3CSTRONG%3EStop-AzVM%20-ResourceGroupName%20%24RgName%20-Name%20%24vmName%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Restart%20a%20running%20VM%3CBR%20%2F%3E%3CSTRONG%3ERestart-AzVM%20-ResourceGroupName%20%24RgName%20-Name%20%24vmName%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23For%20which%20VM's%20is%20Auto-Shutdown%20configured%3F%3CBR%20%2F%3E%3CSTRONG%3E(Get-AzResource%20-ResourceType%20Microsoft.DevTestLab%2Fschedules%20-Expandproperties).Properties%20%7C%20Select-Object%20-Property%20targetResourceId%2C%20taskType%2C%20status%20%7C%20Format-List%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20am%20absolutely%20aware%20that%20this%20is%20nothing%20overwhelming.%20But%20I%20would%20like%20to%20share%20a%20few%20experiences%20with%20you.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%0A%3CP%3EI%20hope%20this%20article%20was%20useful.%20Best%20regards%2C%20Tom%20Wechsler%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EP.S.%26nbsp%3BAll%20scripts%20(%23PowerShell%2C%20Azure%20CLI%2C%20%23Terraform%2C%20%23ARM)%20that%20I%20use%20can%20be%20found%20on%20github!%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ftomwechsler%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Ftomwechsler%3C%2FA%3E%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2591186%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EHands-on-Labs%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EVirtual%20Machine%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
MVP

 

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
Clear-Host

 

#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
Connect-AzAccount

 

#Select the correct subscription

Get-AzContext

Get-AzSubscription

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

 

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

 

#Infos about the vm's
Get-AzVM

 

#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