I need to stop all my vm's in azure portal when my balance is 0$ How can i do it? maybe some script?
Asked
Active
Viewed 274 times
2 Answers
1
Unfortunately you can't do that with script for now... The balance (or billing) is not exposed via the Azure REST API (or Azure PowerShell).

Guy
- 1,434
- 1
- 19
- 33
-
Hallo! Ty for answer! Maybe it is some solution to run shut down script when i got billing alert from azure? – Andrey Bu Apr 16 '15 at 10:10
-
Yes, you can try to run a script when a billing alert mail arrives in your mail server... it might work in theory :) – Guy Apr 16 '15 at 10:22
-
1for script on email, we have used IMAP with success in the past. NICE easy c# API (I forget the .NET API we used, but there are plenty).. but you might be able to interact with the alerts (not tried myself) http://blogs.technet.com/b/keithmayer/archive/2014/11/08/scripts-to-tools-automate-monitoring-alert-rules-in-microsoft-azure-with-powershell-and-the-azure-service-management-rest-api.aspx – Steve Drake Apr 16 '15 at 11:27
0
For shutdown, we use a RUNBOOK with the follow code inside.
We run this everyday at 8PM. (You could trigger this from another alert http://www.matthewjbailey.com/create-azure-billing-alert-email/)
workflow ShutDown-AllVMs {
param (
[parameter(Mandatory=$true)]
[String] $VMCredentialName = "ourcred@xyz.com"
)
$Credential = Get-AutomationPSCredential -Name $VMCredentialName
if ($Credential -eq $null) {
throw "Could not retrieve '$VMCredentialName' credential asset. Check that you created this asset in the Automation service."
}
Add-AzureAccount -Credential $Credential
Select-AzureSubscription BizSpark
InlineScript {
Get-azurevm | ? { $_.Status -ne "StoppedDeallocated"} | Stop-AzureVM -Force
}
}
For alerts
Setup the alert
http://www.matthewjbailey.com/create-azure-billing-alert-email/
Monitor the alert
I have not tried all this together, so you may have a 2+2=5 issue :) but have a read of the blogs and you may find that you get 4 :)

Steve Drake
- 1,968
- 2
- 19
- 41
-
Hi! Ty for answer! I know abot this kind of script, but i need to shut down my vm when my ballance is about 0$. – Andrey Bu Apr 16 '15 at 11:18
-
ok, does this help http://www.matthewjbailey.com/create-azure-billing-alert-email/ – Steve Drake Apr 16 '15 at 11:22