How to start azure VM automatically without automation account - azure

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.

Related

Can I run powershell commands from Azure DevOps to vm's in Azure?

How am I able to run PowerShell commands against a Windows 2016 vm in Azure without using winrm or pssession where I have to install\configure a self signed cert?
Does anyone know
If I understand your query correctly, you want to run PowerShell commands on Azure VM
In such a case, Azure DevOps service will not know where to direct the request, hence out of the box this is not possible.
However what you can do is, you can setup a Self hosted agent on this machine(as long it is connected to the internet).
Then you can run the task on a self hosted agent, and choose to run a powershell task. (This might need you to whitelist some devops ranges on your firewall).
Other than that, there are several ways to run commands and scripts on Azure VMs depending on the design you need to implement.
You can use Azure portal to execute predefined, custom PowerShell commands on a VM using Virtual Machine blade in Azure portal.
Also please take a look at these docs for more reference :
Run Powershell Cmds
Run Cmd
Better to use Azure Automation account than Azure DevOps if you want to do PowerShell.
Read this on how to create an automation account.
See here how to create a Runbook in Azure Automation using PowerShell.
If you are not required to run only one command frequently, you can simply run any PowerShell command just from the portal using Run command, please take a look at the screenshot.
Can I run powershell commands from Azure DevOps to vm's in Azure?
Run PowerShell scripts in your Windows VM by using Run Command provides two possible directions:
1.Azure CLI way:
You can use Azure CLI task in azure devops pipeline to run az vm start
and az vm run-command invoke commands. The az vm start will start the VM, the second command will then run the PS commands.
2.Powershell way:
You can use Azure Powershell task in azure devops pipeline to run Start-AzVM and Invoke-AzVMRunCommand commands.
Note:
It requires several seconds even when starting VM in web portal, so it's recommended to have 10~15 seconds as interval between these two commands(Start VM and Run PS). eg:
Try Start-Sleep -s 15.

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 :)

Schedule Azure VM to turn on, run a script and turn off

I have a multi-container app that I need to run twice a day at specific times for about 30 mins each time. To save costs, I would like to:
Turn on a Linux VM on Microsoft Azure
Run a script that runs docker-compose up -d, waits 30 mins, then calls docker-compose stop
Turn off the VM
I'd love for this to happen automatically on a cron schedule.
So far, I've failed to find any Azure solution that can do all of this. Azure automation allows you to turn on/off VMs but it doesn't allow you to run a sh script on it after you've turned it on. Azure Logic apps allows you to spin up containers on a schedule but not with docker-compose.
Any idea on how I might accomplish this in a way that doesn't involve me having a VM that is always on?
No worries , you can use this AZ powershell command to run sh script on your Azure VMs directly via Azure automation runbook with prarm : -CommandId 'RunShellScript'
For AzureRM commands , refer to here
I have tested on my side and it works perfectly for me .

Run a script on VM before it's deleted in 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.

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