Run a script on VM before it's deleted in Azure - azure

Is there a way to run bash script on a VM before it's deleted from Azure? I know you can use CustomScriptExtension for running script when VM is created or started but I couldn't find any information about launching cleanup scripts before VM is deleted.

No, at the time of writing Azure does not support actions like this.

Depending on what you want to do, you may find it useful to subscribe to VM alerts, which can include when it is deleted. You'll have the option to call a webhook that may give you the flexibility to do what you need. Try working within the Azure portal Monitoring Blade to create your own Alert to handle this.

Related

Find a script which can stop shut down Azure Vm from azure portal when the Vm is restarting after the patch applied

Background:
Now I use an Azure Automation to shutdown the Azure Vms,
but I met a problem last time when one VM had been shutdown form the Azure portal
when it was restarting after the applied patch and the result was the VM could not start again.
I want to find if there has a script that can check if the VM is restart/start/shutdown when I shut down the VM from Azure Portal.
Thanks in ad
You can use Azure Automation to schedule all your Start/Stop/Restart jobs. If ever, you need to check when and how your VMs have been restarted and by whom, you could check the Log history from the portal, inside your VM's sections. To automate the display of this using a script, you could use a simple runbook that queries the tables from the access logs of your VM, and triggers an action of your choice...
I hope this could help you resolve your issue :)

How to start azure VM automatically without automation account

We have a Virtual Machine which scheduled to stop automatically on every end of the day.
Next day when we want to connect to it, we need to explicitly login to Azure portal, select Virtual machine and click on start.
I know we can schedule it using automation account (due to some restrictions in office environment cant use automation account) but is there any way we can achieve this?
Thinking of any way of -
Creating a batch file on my local machine which just start the VM
when I run it without logging into Azure portal?
Any other simple setting like we have to schedule auto stop?
Any other hint will be a great help.
Thank you!
you can just use az cli and\or powershell and script the vm to start, its extremely straight forward (something like az vm start -g rg_name -n vm_name).
There are no built-in startup solutions yet.

Delete an Azure Virtual Machine automatically after deployment

To deploy my infrastructure I need to deploy a VM with a custom script extension. The only purpose of the VM, is to execute the script. After the execution of the script the VM should be deleted automatically.
How can this be done?
Additional information:
This is an azure resource manager deployment
the deletion should work in the azure marketplace environment as well.
this probably means you are doing something wrong, you can use Azure Container Instance to run the script and shutdown. it should work with marketplace as well (as far as I know you can have custom container in marketplace offerings).
Marketplace only allows you to use arm templates to deploy stuff, so you cannot really do what you are asking with an arm template. well, you might be able to hack something like that with nested deployments and complete mode, but I doubt that will pass moderation in marketplace.
technically, you can make vm delete itself as a part of the script. again, not something I would advise.

Add a PowerShell script to Azure Marketplace Offer

I would like to publish an Azure Managed Application to the Azure Marketplace. Is it possible to add to the "app.zip" an own PowerShell Script, which executes some additional deployment steps besides the Azure Resource Manager Template?
The Script would invoke the arm template and handle some outputs of the Template
The way to think about these is that you can only do tasks that can be done in a template. Today, there's no way to run an arbitrary script in an ARM template.
That help?
After some research and contacting the MS Support I found two possible solutions:
Using a VM with a Custom Script Extension. Downside: VM needs long to startup and is expensive if we do not delete it afterwards.
Using a Azure Container Instance to run the script. Starts up in about 45 seconds and doesn't cost anything if we don't use it. -> Tutorial

Start and Stop Azure Virtual Machine

I need to start / stop a Azure virtual machine, but I don't know how I can do this. I don't know anything about Azure or Azure scripts.
You have several options.
The first one is by using the Azure Portal. Click on your VM and at the bottom you should see a button to stop the virtual machine. Starting it again works the same way.
The second option is to use PowerShell. John McCabe has posted a simple script on his blog to do this. The most important functions are Start-AzureVM and Stop-AzureVM.
If you want to automate this from a developer perspective (which needs a little bit more work than the PowerShell solution) is by using the Azure Management Libraries. Brady Gaster wrote a great post about it.
The fourth option is to use Microsoft Azure Automation. There you create runbooks for common tasks. In the samples are scripts already included which can start and stop a VM.
You can also create a Timer-triggered Azure Function to start/stop your VM. I've provided the steps here:
Azure Function role like permissions to Stop Azure Virtual Machines
Instead of creating HTTP-triggered Functions, just use Timer-triggered functions instead and edit the PowerShell script content to hardcode the vmname and resourcegroup.
within the Azure portal (I would recommend using the new portal), go into the VM you need, on the dashboard you will see a Stop and Start button, or you can choose to Restart the VM. Hope this helps.

Resources