I want to write a script in Azure VM which will checkout the code from my github repository and and copys the code to my Azure VM C: drive.
Could you please throw some lights on it about what could be the approach for this?
Thanks
So far we download the code and by RDC we add the code to VM drive.
What I want to do that, I want to write a script which will copy the code from my github repository to my windows virtual machine drive.
I have not much idea about how to go with this. What could be the possible approach?
I would recommend you look at the Custom Script Extension for Azure. You can get started at this blog: Custom Script Extension
The basics will be to:
Write a powershell script that pulls from your github repo (this may help get you started Powershell GitHub Download)
Upload powershell script to Azure Blob Store
Set-AzureVMCustomScriptExtension on your VM and Update-AzureVM
Get-AzureVM -Name $name -ServiceName $servicename | Set-AzureVMCustomScriptExtension -StorageAccountName $storageaccount -StorageAccountKey $storagekey -ContainerName $container -FileName 'file1.ps1','file2.ps1' -Run 'file.ps1' | Update-AzureVM
Other than the script contents mentioned in 1. above, this is a good walk-through: Custom Script Extension Example
The other option would be to use the Azure Powershell Desired State Configuration extension, but it may be overkill for what you're looking for:
Azure Powershell DSC
Related
There is a Windows VM hosted on Azure. On this VM is a .bat file which I need to execute from outside. The start of this process should be possible within an Azure Function.
Does anyone have an idea about this?
I tried to execute the .bat file via Azure Cloud Shell and it worked but I found multiple commands and I am not sure which is the best one and which are usable in an Azure Function.
Best regards
You can write Azure Functions using Powershell as the programming language. Then, import Az.Compute and use the following to invoke commands in your vm from the Azure Functions:
Invoke-AzVmRunCommand `
-ResourceGroupName "rg-azpsremote" `
-VMName "server1" `
-CommandId "RunPowerShellScript" `
-ScriptPath "C:\temp\x.ps1"
ps: you can call the bat file from ps1 file.
Is there a way to trigger installer inside Azure VM with an API call or hook to get the installer running? Something like a PowerShell which I can be executed remotely, I just need to pass a parameter to it which it will pass the installer.
I need something simple and quick to get going,
I have looked into Custom Script Extension but I don't see how can I trigger it. Not sure what is the right thing to search for.
There are a couple options available for running commands in a VM.
While the Custom Script Extension is useful for configuration or management tasks, the Run command feature is very useful in that it is available even when the machine is not reachable. You can also run a Hybrid Runbook Worker with your custom scripts stored in an Automation account.
Refer to the following docs for more info:
Run scripts in your Windows VM
Run Command
You can use Invoke-AzVmRunCommand to do this. The script needs to be local to where the cmdlet is being run.
Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath 'sample.ps1' -Parameter #{param1 = "var1"; param2 = "var2"}
I have a bizsparkplus subscription. I followed the below link but could not find the capture button:
https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-windows-classic-capture-image/#next-steps
Any suggestions?
Until yesterday, I found nothing in Azure's new portal that can help capture an image from a VM,
but today, out of the blue, I found a "Capture" button right inside the VM's Overview tab :) (will appear only for VMs with "Managed Disks")
It is super easy to capture an image (takes like a minute), but you have to connect ssh on the VM first & run this command (which will delete user's home folder) as per this official article:
sudo waagent -deprovision+user -force
then you can use the button.
-- Warning --
Capturing an image from your VM will stop it and mark it as generalized which won't let you start this VM ever again, because being generalized is an irreversible process "by design"! .. the Sh***y thing is, they didn't even put a warning on the button!, so be careful with that!
This feature is not available yet in the new Azure Portal. You have to options : Azure Resource explorer or PowerShell.
Here is an example in powershell. In this example the custom image will be saved in the VM storage account. The vm custom image will be stored in the following location "System/Microsoft.Compute/Images/templates/***.vhd". :
$vmResourceGroup = "iaas-rg";
$vmName = "ubuntu";
$destinationContainerName = "templates";
$vhdNamePrefix = "template";
$sampleOutupTemplatePath = "C:\Templates\ImagesGeneralized\sampleOutputTemplateUbuntu.json";
Login-AzureRmAccount
#Dellocate the VM
Stop-AzureRmVM -ResourceGroupName $vmResourceGroup -Name $vmName
#Generalize the vm
Set-AzureRmVM -ResourceGroupName $vmResourceGroup -Name $vmName -Generalized
# Save the custom vm Image
Save-AzureRmVMImage -ResourceGroupName $vmResourceGroup -VMName $vmName -DestinationContainerName $destinationContainerName -VHDNamePrefix $vhdNamePrefix -Path $sampleOutupTemplatePath
The second option is to use Azure Resource Explorer, you can execute the operations manually * :
*To execute those operations, the mode "read/write" must be selected in Azure Resource Explorer.
Regards,
You can able to capture the classic virtual machines in new portal. There is an option for capturing. attaching is the screenshot.
In case anyone else looking for this topic, I was researching it and found some azure docs here:
Create a managed image of a generalized VM in Azure
which explains generalizing is a one way operation (and why)
and how to deal with that by creating a VM copy first
Create a Windows VM from a specialized disk by using PowerShell
Using the option 3 in the article.
I have created 2 Virtual Machines (VMs) using RM (Resource Manager) deployment method. How can I clone a VM using Azure PowerShell scripts?
You can follow the documentation guide here: https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-windows-capture-image/
After you Open the Azure PowerShell and login to your Azure account.
You can find the subscriptions your Azure account has by using the command Get-AzureRmSubscription.
Next you will need to deallocate the resources used by this virtual machine.
Stop-AzureRmVM -ResourceGroupName YourResourceGroup -Name YourWindowsVM
Next you need to set the status of the virtual machine to Generalized. Note that you will need to do this because the generalization step above (sysprep) does not do it in a way that Azure can understand.
Set-AzureRmVm -ResourceGroupName YourResourceGroup -Name YourWindowsVM -Generalized
Next, Capture the virtual machine image to a destination storage container using this command.
Save-AzureRmVMImage -ResourceGroupName YourResourceGroup -VMName YourWindowsVM -DestinationContainerName YourImagesContainer -VHDNamePrefix YourTemplatePrefix -Path Yourlocalfilepath\Filename.json
The -Path variable is optional and can be used to save the JSON template locally. The -DestinationContainerName variable is the name of the container that you want to hold your images in. The URL of the image stored will be similar to https://YourStorageAccountName.blob.core.windows.net/system/Microsoft.Compute/Images/YourImagesContainer/YourTemplatePrefix-osDisk.xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.vhd. It will be created in the same storage account as that of the original virtual machine.
The Azure documentation offers a detailed guide to do this, so follow the instructions and in case of trouble, you can ask me!
BEWARE! When you sysprep your machine and make it generalized, you CANNOT UnDO this!!!
This means that you VM will stay stopped (deallocated) and you can create infinite cloned from this, but you cannot start this again, so make sure it is not in production :)
After switching to the new Azure portal, I see all resources of mine are listed as classic resources. I am googling around to see if it's possible to have classic VM and migrate it to ARM and just hadn't seen anything that stated it was possible.
I tried deleting the class VM but keep the VHD to check if I can attach it to the ARM VM. The result said it doesn't work.
Is there any way that can be used to achieve this ?
Unfortunately (and strangely!) there isn't a way to do this via the portal and you have to drop down to Powershell instead (or xplat / Rest)
When I have exported VMs from v1 to v2 I typically make the original vhd file accessible via a blob share (this can be done temporarily for the duration of this command)
You can then deploy a new VM using that vhd as a source image's URI, using
Set-AzureRmVMOSDisk -VM $vm -Name $vmname -VhdUri $uri -CreateOption FromImage -Windows
Alternatively you can copy the vhd to v2 storage and use Attach instead of FromImage
If you wrap that around a standard VM Create script, you should be good to go!
There is a set of powershell scripts to assist in migrating classic vms to the new resource group model - asm-arm
They require the Azure and AzureRM powershell modules - msdn link to installation instructions