I will explore Enter-AzVM cmdlet in this blog article which is a part of az.ssh module and establishes a connection over SSH port 22. In this article I will show how to setup windows server to accept SSH connection. Before you start, make sure you've using below commands.:
- Installed OpenSSH Server on your Windows Server
- Started and enabled the sshd service
- Opened firewall port 22 for SSH
## Install OpenSSH Server on windows ## Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 ############################################################################# ## Start and enable ssh service ## Start-Service sshd Set-Service -Name sshd -StartupType 'Automatic' ############################################################################# ## Allow Firewall port 22 for SSH ## if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) { Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..." New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 } else { Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists." }
There are other methods if instead of SSH you want to connect over WinRM you can check previous blog: How to Run PowerShell Commands on Azure VMs Remotely
Next, update your Network Security Group (NSG) to allow inbound SSH traffic, Here is simple PowerShell script, just feed the data in variables.
$vmName = 'devvm' $resourceGroupName = 'vdev.vcloud-lab.com' $nsgName = 'devvm-nsg' $nsgRuleConfig = @{ Name = 'Allow-SSH' Description = 'Allow SSH Port 22' Access = 'Allow' Protocol = '*' Direction = 'Inbound' Priority = '2000' SourceAddressPrefix = '*' SourcePortRange = '*' DestinationAddressPrefix = '*' DestinationPortRange = '22' } Get-AzNetworkSecurityGroup -Name $nsgName -ResourceGroupName $resourceGroupName | Add-AzNetworkSecurityRuleConfig @nsgRuleConfig | Set-AzNetworkSecurityGroup
I can confirm network security group rule is updated to allow port 22 SSH.
Finally use Enter-AzVM command to connect to Azure VM and you are connected to Azure Windows VM over SSH. Just to know this is interactive method for non interactive way check my previous blog: How to Run PowerShell Commands on Azure VMs Remotely.
If incase command not found make sure you Install-Module az.ssh which is one of the require ment.
Enter-AzVM -Name $vmName -ResourceGroupName $resourceGroupName -LocalUser azureadmin
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




