Azure VM turn on and off from inside itself - azure

The end goal is to create a Jenkins job that starts an Azure VM on and then turn it off. I have seen commands online that state how to turn VMs on and off. I have a master Windows VM. Can I start this VM from inside itself using Azure CLI? If it can be done then I can add it into Windows batch command of Jenkins job.

Can I start this VM from inside itself using Azure CLI?
As far as I know, Azure VM is based on hyper-v virtualization technology refer to this. I don't think you can start a virtualization machine from VM itself whatever it's using Azure CLI or not. You need to start it from hyper-v host or hyper-v management UI.
The Run Command feature uses the virtual machine (VM) agent to run PowerShell scripts within an Azure Windows VM. I have tried the Azure windows VM, I can stop or start Azure VM but for deallocated VM, I can not start it using this method.
az vm run-command invoke --command-id RunPowerShellScript --name <vmname> -g <vmrg> --scripts 'shutdown /s /t 0'
az vm run-command invoke --command-id RunPowerShellScript --name <vmname> -g <vmrg> --scripts 'restart'
If you don't from VM itself. Usually, you can use Azure CLI 2.0: Quickly Start / Stop ALL VMs,
# usage example
az vm start --name MyVM --resource-group MyVMGroup
az vm deallocate --name MyVM --resource-group MyVMGroup
az vm stop --name MyVM --resource-group MyVMGroup
Moreover, for Azure VM, the Start/Stop VMs during off-hours feature make you start or stops enabled Azure VMs. You could refer to this blog for more details.

Related

Azure VM run-command fails with piped commands

I am trying to install Azure CLI on an ubuntu VM running in Azure.
I am using the run-command cli operation to execute the command
az vm run-command invoke --command-id RunShellScript --name trainingVM-1 --resource-group azure-privsec --scripts "curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash"
When running a non-piped command, the command works fine but when I run the above piped command to install azure-cli, it fails with the error:
Could not retrieve credential from local cache for service principal 8bf34d22-7230-47e7-907e-e0df201xxxxx. Please run 'az login' for this service principal.
One of the reasons could be that I am using an old version of Azure cli(2.29.2). For now, I dont have the leverage to upgrade the azure-cli version.
What can be the best way to install azure-cli on my VM using the run-command cli call.
Edit:
Splitting into two commands like this works for me but I am still trying to see if we can consolidate it into a single command:
az vm run-command invoke --command-id RunShellScript --name trainingVM-1 --resource-group azure-privsec --scripts "wget -O /tmp/file.sh https://aka.ms/InstallAzureCLIDeb"
az vm run-command invoke --command-id RunShellScript --name trainingVM-1 --resource-group azure-privsec --scripts "sudo bash /tmp/file.sh"
Tested in Azure PowerShell and Azure DevOps pipeline, your first Azure VM run-command run successfully.
Use Azure PowerShell, I can run this command directly without any issue.
Use Azure CLI task in the DevOps pipeline and use a Microsoft-hosted agent.
Please ensure that the service principal corresponding to the Azure Resource Manager connection(Service Connection) has sufficient permission for your Azure VM, such as owner or contributor permissions.
Then I can see the command run successfully.
There are some docs for your reference:
Manage service connections
Connect to Microsoft Azure

Read entire file from Azure VM, via Azure CLI

I'm trying to retrieve a docker log for an application, from an Azure Devops Pipeline. In a pipeline step, I can run a bash script on the VM that's hosting the application, using the Azure CLI like this. This reads the docker log from the VM hosting the application:
az vmss run-command invoke --command-id RunShellScript --instance-id 0 -n aks-myservicename-12345678-vmss -g my-resource-group --scripts "docker logs [container_id]" > testing.txt
The problem is that the output of az vmss run-command invoke is limited to a max 4096 bytes - see https://learn.microsoft.com/en-us/azure/virtual-machines/linux/run-command#restrictions
Is there any way I can read the entire docker log?
The following reference documents an --output-blob-uri parameter that can be used with az vm run-command create. I'm wondering if that gives a way to output the docker log to a file that I can then access by using another Azure CLI step. I haven't found any documentation though on how this works https://learn.microsoft.com/en-us/cli/azure/vmss/run-command?view=azure-cli-latest

unable to SSH into Azure Ubuntu VM after reboot

I have been using this VM (Azure ubuntu) for almost a month but had to restart to increase the disk size which was successful. I have preserved the IP address. But now unable to SSH. I also "reset configuration only" from the "Reset Password option". Also tried "test connection" within Azure Portal and that fails as well. But I am still able to access MySQL database which is running on the same VM. What am I doing wrong?
Finally, it worked. All I had to do was a reset via the CLI:
az vm user reset-ssh --resource-group myresourcegroup --name myvm
https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/vmaccess

Could not login to azure instance created using a managed data disk

I created a virtual machine using the following command in azure cli.
az vm create --resource-group Test_Group --location eastus --name myVM2 --os-type linux --attach-os-disk disk1
I installed grub on disk1 and all necessary things for booting.I am facing an issue in connecting to the instance using ssh.

VM Agent is either unavailable, or not installed, which may prevent VMAccess from Running

I created a VM using a managed data disk but there is a problem of VMAgent for connecting through ssh.
Command is:
az vm create --resource-group Test_Group --location eastus --name myVM2 --os-type linux --attach-os-disk disk1
The error is:
VM Agent is either unavailable, or not installed, which may prevent VMAccess from Running.
If I try to install a new VMAgent it throws an error of MultipleVMAgents not allowed and current enablevmaccess extension shows status(unavailable).

Resources