Check if Docker Container is running on Azure VM via Powershell - azure

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

Related

Executing Remote Command via Powershell in Azure with no script

I am trying to setup Azure VM's using Powershell but i wish to do it all remotely with no logging into the machine. I have been able to spin up the VM using commands like New-AzVM but now i need to run specific powershell commands e.g. Enable-WindowsOptionalFeature -Online -FeatureName IIS-ManagementConsole
I can see you can run remote scripts using the following but as this is a brand new server there are no scripts on that box to execute.
Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -Name $VmName -CommandId 'RunPowerShellScript' -ScriptPath '<pathToScript>' -Parameter #{"arg1" = "var1";"arg2" = "var2"}
How can i copy scripts to the server remotely so i can use the above command, or run commands without the need for a script file.
You don't need to copy the scripts remotely to the Azure VM. You can run it locally.
Save the PowerShell scripts to a format .ps1 file, then run it on local PowerShell with referencing the path of the script via parameter -ScriptPath.
Enable-WindowsOptionalFeature -Online -FeatureName IIS-ManagementConsole
You also could copy that scripts on the Virtual machine---Run command---RunPowerShellScript on the Azure portal, refer to this.
What you need is to look into the direction of PowerShell Desired State Configuration (DSC)
Here you can find general explanation for this technology
Get started manual for DSC

Custom Data with Azure Windows VM run PowerSell Script

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

How do I restart a windows service on an Azure VM remotely from my office desktop using powershell?

I have a few Windows Server 2019 Core VMs in Azure all running the same windows service.
I would like to run a powershell script from my computer that loops through all the VMs and restarts the service on each.
I have a PS script on each VM which does this so how do I invoke them remotely? Is this possible without using Cloud Shell or Automation, with just a few lines of code? I have Azure powershell tools installed on my computer too.
Please provide answers with a working example.
Thank you
For your requirement, I think the simplest way is to use the Azure PowerShell command to execute the PowerShell script inside the VM, the example command here:
Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath 'sample.ps1' -Parameter #{param1 = "var1"; param2 = "var2"}
To execute the script in each VM, you need to make a loop with PowerShell yourself. By the way, you can also use the Custom Extension for Windows.

Get the right name of the Container related to an Azure Web App

Is it possible to exec a command in a windows container related to a webapp based on a container? I'm not able to find the right name to use on the cli command az app exec. In fact az container list returns me an empty list. However the container exits cause the web app is running. Where can I find the right name to use with az app exec command? I also checked with the name of only one slot in the webapp without success.
The error az app exec returns is:
The Resource 'Microsoft.ContainerInstance/containerGroups/theNameIPRovide' under resource group 'thegroupNameIProvide' was not found.
Unfortunately, there is no Azure CLI command for Web app for Container to exec the command inside the container. But you can use other ways, for example, the API.
There is no CLI command like az app exec. And az container is for ACI, not for the Web App. You can get all CLI command for Web App in az webapp.
Or you can deploy your application in Azure Container Instance, it also can run windows container for you. So that you can use the CLI command az container exec to execute the command inside your application.
Could you have a look at this https://learn.microsoft.com/en-us/cli/azure/container?view=azure-cli-latest#az-container-exec, Couldnt comment, so had to post it in an answer

How do I run multiple commands when deploying a container group?

I am deploying a Container group with the template https://learn.microsoft.com/en-us/azure/templates/microsoft.containerinstance/2018-10-01/containergroups
It has command parameter, but it is just a string and runs one command. I would like to run multiple commands when deploying. Is it possible?
If not, is there a way to run those commands to the container after it has been deployed, using PowerShell?
My usecase:
I need a SFTP server in Azure for customers to be able to send us data. I then poll that with a Logic App.
What I have done:
I found this template to be good for my needs, as it is easier to poll Azure Storage File Share.
https://github.com/Azure/azure-quickstart-templates/blob/master/201-aci-sftp-files
My problem is I have multiple users. Everyone needs their own username/password and their own file share or sub-directory in that share. I also can't understand how to configure multiple users through the environment variable. I tried separating them with ;. It deploys, but the server doesn't respond to requests at all.
I can deploy multiple containers, one for each user, but that doesn't sound like a good idea when the number of customers rises.
Unfortunately, it seems that you cannot run multi-command in one time. See the Restrictions of the exec command for ACI:
Azure Container Instances currently supports launching a single
process with az container exec, and you cannot pass command arguments.
For example, you cannot chain commands like in sh -c "echo FOO && echo
BAR", or execute echo FOO.
I suggest that you can run the command to create an interactive session with the container instance to execute command continuously after you create the ACI.
For Linux:
az container exec -g groupName -n containerName --exec-command "/bin/bash"
For Windows:
az container exec -g groupName -n containerName --exec-command "cmd.exe"

Resources