Custom Data with Azure Windows VM run PowerSell Script - azure

I am trying to download and install an exe during the provisioning of a Windows VM in Azure cloud. I do not want to use Custom Script Extension but instead I want to use "Custom Data". I cannot find any solid examples on Azure documentation.
In AWS, I found enough resources and I could develop the below PowerShell script and add it to the User Data but that doesn't work on Azure, I tried different variations but with no luck. Has anyone done that before? Is there any clear documentation on that? I read Azure uses Cloud-init but again, no clear examples on how to do that with Cloud-init for a Windows machine, all examples are for Linux.
<powershell>
start-transcript
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest 'https://www.domain-name.com/filename.exe' -OutFile C:\filename.exe
C:\filename.exe --quiet
</powershell>
Any help would be appreciated.

You can inject data into a Windows virtual machine on Azure, but you can't execute it using custom data or cloud init unfrotunately. Execution of the custom data using cloud init is only supported in Ubuntu images.
Source: https://azure.microsoft.com/es-es/blog/custom-data-and-cloud-init-on-windows-azure/
To achieve an execution of a script post provisioning, it depends on how you're provisioning the VM.
In ARM templates you can use custom script extensions: https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows
Using Azure CLI you can execute a script using az vm run-command like this:
az vm run-command invoke --command-id RunPowerShellScript --name win-vm -g my-resource-group \
--scripts 'param([string]$arg1)' \
'Write-Host Hello' \
--parameters 'arg1=kemety'
Sourced from here: https://learn.microsoft.com/en-us/cli/azure/vm/run-command?view=azure-cli-latest

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

Function App runs fine locally but fails when deployed to Azure

Following Microsoft Quick Start for Function App with Go (https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-other?tabs=go%2Cwindows)
I was able to successfully run the code locally on my Windows computer. I continued to follow the steps to deploy the Function App to Azure portal and when try to run the Function App at the /api/HttpExample endpoint. It would fail to execute even though it runs fine locally.
Programming language used : Go 1.17.7
Links to source: https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-other?tabs=go%2Cwindows
Bindings used
I had the same problem (working locally, wouldn't load when deployed), but I am running Linux locally. The error message I got was:
An attempt was made to access a socket in a way forbidden by its access permissions.
I was creating the Azure Function App through the VS Code Azure extension, and the default OS when creating a Function App is Windows:
The clue was to select the Advanced option and select OS->Linux. With the AZ CLI you need to add the option --os-type Linux, e.g.:
az functionapp create --os-type Linux --resource-group hellogorgplinux --consumption-plan-location eastus --runtime node --runtime-version 10 --functions-version 3 --name hellogoapplinux --storage-account hellogostglinux

Check if Docker Container is running on Azure VM via Powershell

I am trying to create a PowerShell Script inside a Function App that checks if a Docker Container is running on an Azure VM. So far I have managed to check wether the VM is running with the following script:
$provisioningState = (Get-AzVM -resourcegroupname $rsgName -name $vmName -Status).Statuses[1].Code
$condition = ($provisioningState -eq "PowerState/running")
However, I am not able to check the Docker status. How can I check from a Function App whether Docker is running on the VM using PowerShell?
Thank you for your help.
If you're wanting to run the command on the VM to check docker then you could use the Run Command and Invoke-AzVMRunCommand.
As this requires the script to be local to where the command is being run you may have to store the script inside the function app or write a wrapper function/module to make use of it.
Docs on that are here:
https://learn.microsoft.com/en-us/azure/virtual-machines/windows/run-command#powershell

How can we run multiple CLI command in Azure Bash together?

I have Multiple Resources to deploy in my Environment using CLI command all together. For Example: I need to create VM, Web App, Redis Cache, etc using a single script.
Instead of creating individual resources can we create all together.
One way is to put all your commands in a .sh shell script. Below is an example of how you can do this:
#!/bin/bash
az group create -n MyResourceGroup -l centralus
az vm create -n MyVm -g MyResourceGroup --image UbuntuLTS
az webapp create -g MyResourceGroup -p MyPlan -n MyUniqueAppName
# More Azure CLI commands to create resources
Then you can run your script inside your local environment bash shell or Azure Cloud Shell like this:
username#Azure:~$ ./script.sh
Another way is to use an ARM template to deploy your resources.

Resources