#Azure AUTOMATION VARIABLES

11 messages · Page 1 of 1 (latest)

fringe spoke
#

Forgive me! I am just new to PShell and have been learning with use cases.

I'm trying to complete the following task in azure...

Find all VM's with the deallocated state, save the output to an azure automation variable, turn the VM's on....

I can complete most of these tasks individually but I'm struggling to put it all together.

#---Get all VMs in subscription
$VMs = Get-AzVM -status
#---Get deallocated VMs - This part needs to be replaced with set-azautomationvariable?
$DeallocatedVMs = $VMs | Where-Object { $_.PowerState -Like 'VM deallocated'}
#---Start Deallocated VMs
foreach ($VM in $DeallocatedVMs)
{
Start-AzVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName
}

I am interested to learn! Any assistance is appreciated!

hoary quartz
#

Hi @fringe spoke, just to be sure, you want a single run of your runbook to perform these actions?
Is there a specific reason why you'd want to set it to an Azure Automation Variable?
You can output the start-AzVM output or create a single line which shows in output of the automation job (a run of the runbook), if you'd want to know which VM has booted

fringe spoke
#

Thank you for your reply! My understanding was I could set the value of an automation variable and then call it from another script.

It would be to the status and save the 'deallocated' vm's to the variable and then turn those VM's on.

5 hours later, it should read that variable and turn them back off?

fringe spoke
#

I think I might be onto something..

#

Connect to Azure

Connect-AzAccount

$SubscriptionID = "" #Dev/Test subscription
$ResourseGroupName = ""

Set the Subscription to execute the Powershell Commands in

Set-AzContext -SubscriptionId $SubscriptionID

$VMList = Get-AzVM -status | Where-Object { $_.PowerState -like 'VM deallocated'}

This command sets a new value for the variable named CurrentVMState in the Azure Automation account named

Set-AzAutomationVariable -AutomationAccountName "" -Name "" -ResourceGroupName $ResourseGroupName -Value $VMList -Encrypted $False

This command turns on the deallocated VM's

foreach ($VM in $VMList)
{
Start-AzVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName
}

dry helm
#

which don't have the "Az" prefix, and work automatically in the account you're running in

fringe spoke
#

I removed the names 😄

fringe spoke
#

This command retrieves the value for the variable named CurrentVMState in the Azure Automation account named Test and saves it to $ValueFromAzure

$ValueFromAzure = (Get-AzAutomationVariable -AutomationAccountName "svc-autoupdate-automation" -Name "CurrentDeallocatedVMs" -ResourceGroupName $ResourseGroupName).Value

Only shutdown the VM's that where not powered on before the patching - Gets previous VM recorded state from the AzAutomationVariable "CurrentVMState"

$ValueFromAzure | Stop-AzVM | Where-Object {$_.PowerState -eq "VM deallocated" }

#

This kinda works, it saves the right info to the automation variable. But when it comes to turning those VM's off, "| Where-Object [$_.PowerSate -eq "VM deallocated" it doesn't grab the deallocated VM's from the variable T_T