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”.

05-azure-portal-patricksdemostorage

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

05-azure-portal-azworkshop-file-share-consoleapp

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

04-visualstudio-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
03-powershell-az-list-vms

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

azure-portal-virtualmachines-overview

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

02-azure-portal-virtualmachines-details

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:

06-azure-portal-azworkshop-file-share-connect

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

07-azure-portal-azworkshop-file-share-create-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:

08-azure-portal-azworkshop-file-share-copy-script

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

09-azure-portal-az-demo-vm-run-command

…and select “RunPowerShellScript”:

10-azure-portal-az-demo-vm-run-command-powershell

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”:

11-azure-portal-az-demo-vm-run-command-powershell-run

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

12-azure-portal-az-demo-vm-run-command-powershell-result

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.

13-powershell-mount-script

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:

14-powershell-mount-script-run-with-az-cli

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

References

learn.microsoft.com - Azure CLI

learn.microsoft.com - Virtual Machines - Run-Command

azure.microsoft.com - Virtual Machines