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](/images/post_22/05-azure-portal-patricksdemostorage.png)
This file share contains the application “consoleapp-windows.exe”:
![05-azure-portal-azworkshop-file-share-consoleapp](/images/post_22/05-azure-portal-azworkshop-file-share-consoleapp.png)
This simple console application just prints “Hi, I’m a Windows Console App!”:
![04-visualstudio-console-app](/images/post_22/04-visualstudio-console-app.png)
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](/images/post_22/03-powershell-az-list-vms.png)
Or by selecting “virtual machines” in the Azure Portal:
![azure-portal-virtualmachines-overview](/images/post_22/01-azure-portal-virtualmachines-overview.png)
The details of the virtual machine can be seen in the picture below:
![02-azure-portal-virtualmachines-details](/images/post_22/02-azure-portal-virtualmachines-details.png)
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](/images/post_22/06-azure-portal-azworkshop-file-share-connect.png)
Select “Windows”, choose a drive letter of your choice and finally click at “Show Script”:
![07-azure-portal-azworkshop-file-share-create-script](/images/post_22/07-azure-portal-azworkshop-file-share-create-script.png)
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](/images/post_22/08-azure-portal-azworkshop-file-share-copy-script.png)
Switch back to the virtual machine and there choose “Run command”…
![09-azure-portal-az-demo-vm-run-command](/images/post_22/09-azure-portal-az-demo-vm-run-command.png)
…and select “RunPowerShellScript”:
![10-azure-portal-az-demo-vm-run-command-powershell](/images/post_22/10-azure-portal-az-demo-vm-run-command-powershell.png)
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](/images/post_22/11-azure-portal-az-demo-vm-run-command-powershell-run.png)
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](/images/post_22/12-azure-portal-az-demo-vm-run-command-powershell-result.png)
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](/images/post_22/13-powershell-mount-script.png)
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](/images/post_22/14-powershell-mount-script-run-with-az-cli.png)
That’s how to use the Azure CLI command for that use case.
References
learn.microsoft.com - Azure CLI