Azure Virtual Machine - Execute PowerShell for mounting a File Share using the Run Command Action and Azure CLI

azure cloud azure-cli azure-vm azure-share devops

The intention of this post is to explain how to run a PowerShell script by using the “Run Command” action and the Azure CLI

1. Introduction

You have provisioned a Virtual Machine in Azure and you would like to run PowerShell on it? This can be done by using the “Run Command” action or by conducting a proper Azure CLI command. This post shows step by step how to do that. As use case serves a simple example: a Windows console application should be called, which is located at a file share.

2. The Components: File Share including the Console App and the Virtual Machine

The picture below shows the storage account named “patricksdemostorage”, including the file share “azworkshop”.


This file share contains the application “consoleapp-windows.exe”:


This simple console application just prints “Hi, I’m a Windows Console App!”:


The idea is now to write a PowerShell script, which mounts the file share at the virtual machine and which calls the console application.

It’s about a Windows virtual machine, which can be listed by conducting following command:

az vm list -d -o table

Or by selecting “virtual machines” in the Azure Portal:


The details of the virtual machine can be seen in the picture below:


3. Executing a PowerShell Script using the “Run Command” action

At first, get the commands for mounting the file share. For that, switch to the file share and select the “Connect” button:


Select “Windows”, choose a drive letter of your choice and finally click at “Show Script”:


This reveals the PowerShell script, which establishes a connection to the file share. Copy the whole content to the clipboard, by clicking at the icon in the right lower corner:


Switch back to the virtual machine and there choose “Run command”…


…and select “RunPowerShellScript”:


Paste now the content in the field and add line 11, which calls the console application:

& R:\consoleapp-windows\consoleapp-windows.exe

After that, trigger the execution by selecting “Run”:


The output reveals, that the share was mounted and that the console application was called:


This concludes the example with regard to execute PowerShell using the “Run Command” action.

4. Executing a PowerShell Script using the Azure CLI

The same can be done by using the Azure CLI. For that, create a .ps1 file including the whole content. Save the file as “mount.ps1” in a directory of your choice.


Finally, use following Azure CLI command for executing the PowerShell script - ensure that you are in the same directory as you’ve saved the “mount.ps1”:

az vm run-command invoke -g az-graz-demo -n az-demo-vm --command-id RunPowerShellScript --scripts "@mount.ps1"

Now you’ll get a similar output, also telling you that the share ws mounted and you can see the content of the console application:


That’s how to use the Azure CLI command for that use case.

References - Azure CLI - Virtual Machines - Run-Command - Virtual Machines