Skip to content

TechDirectArchive

Hands-on IT, Cloud, Security & DevOps Insights

  • Home
  • About
  • Advertise With US
  • Reviews
  • Contact
  • Toggle search form
Home » AWS/Azure/OpenShift » Running PowerShell remotely on Azure VMs
  • Connect to FTP Server
    Secure FTP Login Issue: NAT Router Configuration for Passive Mode and Port Forwarding Windows Server
  • deactivateandreactivate
    How to deactivate and reactivate a Slack user JIRA|Confluence|Apps
  • Featured image Microsoft Outlook
    How to Recall an Email in Microsoft Outlook Network | Monitoring
  • reset
    How to Reset a Snom Phone Network | Monitoring
  • Upstream branch in Git 1
    Set up and use an upstream branch in Git Version Control System
  • screenshot 2020 04 07 at 21.02.45
    Trend Micro WorryFree Business Security Server Setup Anti-Virus Solution
  • Snapshot
    Create or delete snapshots on VMware Workstation Backup
  • multcloud
    Best Way to Backup Dropbox to Box JIRA|Confluence|Apps

Running PowerShell remotely on Azure VMs

Posted on 28/02/202325/03/2023 Raphael Gab-Momoh By Raphael Gab-Momoh No Comments on Running PowerShell remotely on Azure VMs
featureimagepshell 1

Azure PowerShell is a command line interface(CLI) tool is used to manage and automate Azure resources. It can also be used to interact with Azure services and resources, including virtual machines, storage accounts, and databases. It offers a set of cmdlets. This article will help you learn the various ways of running PowerShell remotely on Azure Virtual machines. With a special focus on the Run Command and Custom Script Extension for VM. Other useful articles can be found here in the following guides. How to Enable Two-Factor Authentication for SSH in Linux, how to use Azure Compute Gallery, Azure Monitor,  how to Add a User to the Sudoers List in Linux and how to install and Uninstall Discord in a Linux System.

Requirements to run PowerShell remotely on Azure VMs

To be able to follow this guide, there are certain things that are needed and some are listed below. He is another interesting guide on how to set up a third-party DNS server on a Linux Server.

  1. A tenant and subscription to Microsoft Azure  with the necessary rights, such as Owner role  or Contributor role
  2. CLI for PowerShell & Azure and Powershell modules
  3. Azure Virtual Machine

Executing Run Command in Azure Portal

On an Azure Windows VM, execute the following PowerShell script using Run Command:

  1. In the Azure portal, deploy your windows virtual machine
  2. Navigate to the virtual machine resource that you deployed in step 1
  3. From the list of menu items on the left side of the portal, Select Operations > Run Command from the menu. From the list of commands, pick RunPowerShellScript.
runcommand
run command pane

Next, in the Run Command Script pane, we typed the PowerShell script text that we want to execute on the server. The example that follows installs IIS, When finished, click the run button as shown below

running powershell remotely on azure-run command script
PowerShell scripts in the VM

While executing, it shows there is a message at the top of the run screen to show that “Script execution in progress…” However,  The output of the script we ran is as shown below:

output
output

Executing Run Command in Azure Powershell

Run Command can also be accessed through PowerShell by using the invoke-AzVMrunCommand function. This command is a component of the collection of Azure PowerShell modules called Az.ComputeRun a PowerShell application using this command:

  1. click on cloud shell and open the PowerShell window
  2. if you are not using the cloud shell make sure you log in to your Azure tenant Log into your Azure using the Connect-AzAccount command.
  3. Specify the –ResourceGroupNameand –VMName of the virtual machine when using the Invoke-AZVmRunCommand and Scriptpath and finally, the command-Id

Sample code

Invoke-AzVmRunCommand `
     -ResourceGroupName "myCoolrg" `
     -VMName "techdirectvm" `
     -CommandId "RunPowerShellScript" `

     -ScriptPath "C:\ps\Install-IIS.ps1"
     
running powershell remotely on azure-provising-succeeded
provisioning with script succeeded

Executing the run command with Azure CLI

When running PowerShell remotely on Azure VMs, PowerShell programs and scripts can be executed using the Azure CLI’s az vm run-command invoke command. To execute a PowerShell script, use the following command:

  1. Use Az login if you are accessing from outside the cloud shell on the terminal and make sure you have a valid subscription and are in an Azure tenant
  2. Use the —resource-group and —name parameters of the az vm run-command invoke to define the virtual machine’s name and resource group. Use the At (@) symbol to indicate the script file name when using RunPowerShellScript’s —command-id parameter.

Notice how the path name contains two backslashes (). The Install-IIS.ps1 script used in this example has the same code as the ones used in the earlier instances.

Sample code

az vm run-command invoke \
  --resource-group 'myCoolrg' \
  --name 'techdirectvm' \
  --command-id 'RunPowerShellScript' \
  --scripts @C:\\ps\\Install-IIS.ps1
az-vm-create
running Powershell script in Azure CLI

Azure CLI has some advantages over PowerShell, including the capacity to execute single instructions rather than scripts. You can send specific PowerShell commands in place of a script in the —scripts argument.

Running PowerShell Remotely on Azure VM Using Custom Script Extension

Utilizing a virtual machine with the Custom Script Extension is an additional choice. For post-deployment configurations, the Custom Script Extension gets and runs scripts on Azure virtual machines. The PowerShell scripts are put in a spot where the Azure virtual machine can route to them, like GitHub, Azure Blog storage, or a local file server.

For the VM to be able to view the file, you might need to add firewall rules or Network Security Group exceptions. So, let’s assume that we have a project that we are working on. We have decided to create a storage account and upload the script that we intend to use for our virtual machine to the storage account, here is the code we used for this:

# Define the storage account and container parameters
$storageAccountName = "techdirectscripts"
$storageAccountKey = "QvNwlr05BCGFk8cDhSEbP38pSqfbTQY2wjmwDCcYIdgedfID9/VudqSHcIaFOXmMZhIbHKUlHQnY+AStyoS+5w=="
$containerName = "file1"
$blobName = "install-webserver.ps1"
$localFilePath = "C:\Users\Raph\storage\install-webserver.ps1"

# Connect to the storage account
$ctx = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

# Create the container
New-AzStorageContainer -Name $containerName -Context $ctx

# Upload the script file to the container
Set-AzStorageBlobContent -Container $containerName -Blob $blobName -File $localFilePath -Context $ctx

Output

operation
storage and upload of scripts to azure storage
scrip-upload
Script Uploaded to Azure Storage

Here is the content of the script uploaded to Azure storage & what we need to run

# Define the virtual machine and script parameters
$vmName = "techdirectvm"
$resourceGroup = "myCoolrg"
$scriptName = "install-webserver.ps1"
$scriptURI = "https://mystorageaccount.blob.core.windows.net/scripts/$file1"

# Add the Custom Script Extension to the virtual machine
Set-AzVMCustomScriptExtension -ResourceGroupName myCoolrg `
  -VMName techdirectvm `
  -Location "East US" `
  -FileUri https://techdirectscripts.blob.core.windows.net/boomscripts/file1 `
  -Run "install-webserverscript" `
  -Argument "-webServerName techd2" `
  -Name "CustomScript"

# Start the virtual machine
Start-AzVM -ResourceGroupName myCoolrg -Name techdirectvm

What is left is just to run the “Add the Custom Script Extension to the virtual machine” code above and start the virtual machine.

Executing from Storage Account Blob

Similar to the example above, we have to connect to the storage account created and had access to the content of the script through the storage URL. In Summary, to execute from a storage account blob, the PowerShell script’s blob URI should be placed in the same $fileUris array variable.

Next, Add storageAccountName and storageAccountKey data and others. Lastly, run Set-AzVMExtension, and update the name of the server, resource group, and deployment-specific address as necessary.

Running PowerShell remotely on Azure VMS: Summary

In this article, we analyzed the various there are ways to run a PowerShell script with regards to a virtual machine and some of them are by running directly from the virtual machine using the run command.

By using Azure PowerShell in or outside the portal by running the Invoke-AzVmRunCommand, by using Azure CLI and running the az vm run-command invoke and finally, we talked about uploading the script to Azure cloud storage and running the Set-AzVMCustomScriptExtension.

I hope you found this blog post helpful. If you have any questions, please let me know in the comment session.

Rate this post

Thank you for reading this post. Kindly share it with others.

  • Share on X (Opens in new window) X
  • Share on Reddit (Opens in new window) Reddit
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Facebook (Opens in new window) Facebook
  • Share on Pinterest (Opens in new window) Pinterest
  • Share on Tumblr (Opens in new window) Tumblr
  • Share on Telegram (Opens in new window) Telegram
  • Share on WhatsApp (Opens in new window) WhatsApp
  • Share on Pocket (Opens in new window) Pocket
  • Share on Mastodon (Opens in new window) Mastodon
  • Share on Bluesky (Opens in new window) Bluesky
  • Share on Threads (Opens in new window) Threads
  • Share on Nextdoor (Opens in new window) Nextdoor
AWS/Azure/OpenShift Tags:Azure CLI

Post navigation

Previous Post: How to automatically reopen Windows Apps and Folders upon Startup
Next Post: How to create Confidential VMs in Azure

Related Posts

  • HybridCloudTrust
    WHFB Hybrid Cloud Kerberos Trust Model is now available AWS/Azure/OpenShift
  • Convert PEM to PPK 1
    Convert a PEM Key to a PPK Key on a Linux and Windows AWS/Azure/OpenShift
  • maxresdefault 2 6
    How to create an AMI from the Command line AWS/Azure/OpenShift
  • Storage Explorer
    How to Install Azure Storage Explorer on Windows AWS/Azure/OpenShift
  • Unable to ping an EC2 Instance
    Unable to Ping an EC2 Instance AWS/Azure/OpenShift
  • AWS Budgets
    How to create AWS Budget AWS/Azure/OpenShift

More Related Articles

HybridCloudTrust WHFB Hybrid Cloud Kerberos Trust Model is now available AWS/Azure/OpenShift
Convert PEM to PPK 1 Convert a PEM Key to a PPK Key on a Linux and Windows AWS/Azure/OpenShift
maxresdefault 2 6 How to create an AMI from the Command line AWS/Azure/OpenShift
Storage Explorer How to Install Azure Storage Explorer on Windows AWS/Azure/OpenShift
Unable to ping an EC2 Instance Unable to Ping an EC2 Instance AWS/Azure/OpenShift
AWS Budgets How to create AWS Budget AWS/Azure/OpenShift

Leave a Reply Cancel reply

You must be logged in to post a comment.

Microsoft MVP

VEEAMLEGEND

vexpert-badge-stars-5

Virtual Background

GoogleNews

Categories

veeaam100

sysadmin top30a

  • Connect to FTP Server
    Secure FTP Login Issue: NAT Router Configuration for Passive Mode and Port Forwarding Windows Server
  • deactivateandreactivate
    How to deactivate and reactivate a Slack user JIRA|Confluence|Apps
  • Featured image Microsoft Outlook
    How to Recall an Email in Microsoft Outlook Network | Monitoring
  • reset
    How to Reset a Snom Phone Network | Monitoring
  • Upstream branch in Git 1
    Set up and use an upstream branch in Git Version Control System
  • screenshot 2020 04 07 at 21.02.45
    Trend Micro WorryFree Business Security Server Setup Anti-Virus Solution
  • Snapshot
    Create or delete snapshots on VMware Workstation Backup
  • multcloud
    Best Way to Backup Dropbox to Box JIRA|Confluence|Apps

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,832 other subscribers
  • RSS - Posts
  • RSS - Comments
  • About
  • Authors
  • Write for us
  • Advertise with us
  • General Terms and Conditions
  • Privacy policy
  • Feedly
  • Telegram
  • Youtube
  • Facebook
  • Instagram
  • LinkedIn
  • Tumblr
  • Pinterest
  • Twitter
  • mastodon

Tags

Active Directory AWS Azure Bitlocker Microsoft Windows PowerShell WDS Windows 10 Windows 11 Windows Server 2016

Copyright © 2025 TechDirectArchive

 

Loading Comments...
 

You must be logged in to post a comment.