In this article I will explore running commands remotely on Azure VM without login and this doesn't require full RDP session. This is very useful feature if some information need to fetched or any configuration need to be done without much efforts. If you check on Azure portal navigate to Virtual Machine >> Operations >> Run Command >> click RunPowerShellScript option.
You will see Run Command Script wizard box in the right side, Inside PowerShell Script type commands and click Run button, you will see Output, which is very handy and quick, not requires more efforts.
I can execute same commands directly from my local machine using the Azure PowerShell az module, which is more useful for automation. The Invoke-AzVMRunCommand is the quickest way to run a script inside a VM without needing to configure network ports or WinRM (PSRemoting). It uses the built-in VM agent to execute the code. There are multiple CommandIds, to check the list you can use below ids. (For linux you can use RunShellScript),
Invoke-AzVMRunCommand -Name devvm -ResourceGroupName vdev.vcloud-lab.com -ScriptString 'Get-CimInstance Win32_ComputerSystem | Select-Object -Property Name' -CommandId 'RunPowerShellScript'
Checkout this useful Article: A Step-by-Step Guide to Using Enter-AzVM for Remote Administration Over SSH
Here is the command execution and its output after execution.
Another next exploration is module name PSCloudShellUtility. I found it by default installed on Azure Cloud shell and it can be downloaded directly from below github url.
https://github.com/Azure/CloudShell/tree/master/linux/powershell/PSCloudShellUtility
In the module first command I will use is Enable-AzVMPSRemoting to enable WinRM PowerShell remoting on Azure VM, add listeners and open required ports (5985/5986) in NSG/Firewall. For simplicity to show the demo I will make use of http port for connection over WinRM/WSMan. For production use it is always recommended to use https (Certificate deployment will be required), Once remoting is configured and established use Invoke-AzVMCommand to connect remotely supplying credentials as shown in below example.
Download this script here AzureVMPSRemoting.ps1 or it is also available github.com.
Enable-AzVMPSRemoting -Name devvm -ResourceGroup vdev.vcloud-lab.com -Protocol http -OsType windows $username = 'azureadmin' $password = 'Computer@123' $secureString = ConvertTo-SecureString $password -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential -ArgumentList $username,$secureString Invoke-AzVMCommand -Name devvm -ResourceGroupName vdev.vcloud-lab.com -ScriptBlock {ipconfig} -Credential $credential
As It can be see on the NSG firewall required PSRemoting ports are allowed.
Below is the output after executing command. You might see error while executing command. You can ignore it but if you want to resolve error Set-Item: Access is denied, Run the Invoke cmdlet in PowerShell run as administrator. Which simply updates Azure VM ip in Trusted Hosts in WSMan configuration. And in the next execution you will see error is disappeared.

In the PSremoting example once your work is done you can run Disable-AzVMPSRemoting to cleanup the things and harden the settings.
If you want to connect system from Linux PowerShell to Windows VM you will need to run below commands on Linux PowerShell.
Install-Module -Name PSWSMan -Force # for linux Install-WSMan # for linux Disable-WSManCertVerification -All
Useful Articles
Microsoft PowerShell: Check Windows license activation status
Find next available free drive letter using PowerShell
Copy Files with PowerShell Remoting WINRM Protocol
Powershell Find application window state minimized or maximized
How to Install and Use Microsoft PowerShell on Linux
Configure PowerShell remoting between Windows and Linux
Get-PSRepository WARNING Unable to find module repositories
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send
Creating an internal PowerShell module repository
How to sign PowerShell ps1 scripts
PowerShell Convert MAC address to Link-local address IPv6
PowerShell fix repair The trust relationship between this workstation and the primary domain failed
Resovled issue with PowerShell - Trust relationship Rejoin computers in domain without restart
PowerShell remoting over HTTPS using self-signed SSL certificate
Configure Powershell WinRM to use OpenSSL generated Self-Signed certificate
Powershell WinRM HTTPs CA signed certificate configuration
Powershell Generate Self-signed certificate with Self-Signed Root CA Signer




