How can we connect to Azure VM with Private IP from Azure Automation powershell runbook?

Goel, Akanksha 66 Reputation points

Not able to connect to VM:

$Password = "<>"
$User = "<>"
$ComputerName = "<>"
$Command = "hostname"

Test-Connection -ComputerName

$password = ConvertTo-SecureString $Password -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($User, $password)

New-SSHSession -ComputerName $ComputerName -Credential $Cred #Connect Over SSH


Invoke-SSHCommand -Index 0 -Command 'hostname' # Invoke Command Over SSH

Using this script I am getting connection time out.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,159 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,429 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Carlos Villagomez 1,106 Reputation points Microsoft Employee

    Hi @Goel, Akanksha ,

    Thank you for your post! I can see that you are currently running the runbook from an Azure Sandbox which is the default cloud worker rather than a Hybrid Runbook worker.

    Azure sandboxes are like random internet systems because they are used by all Azure customers by default. We do not create individual sandboxes on your local network so it will never be able to connect directly to your VM on a private network. You can however run your Automation runbooks on a Hybrid runbook worker directly on an Azure or non-Azure machine per the following document here.

    As an alternative, you could also issue a command from an Azure Cloud Runbook that runs a script on your Azure VM using Invoke-AzVMRunCommand since the runbook at that point would not be connecting directly to the Azure VM. I have a attached a couple of sample scripts you can use for testing this method as well.

    Please let me know if you have any further questions or concerns.

    Carlos V.

  2. Limitless Technology 39,461 Reputation points

    Hi GoelAkanksha-9319,

    The Script you've provided it seems to have no error, to know exact reason what's the full error you're getting "Unable to Connect to ....Azure AD etc"

    You can troubleshoot you error by following steps mentioned in this Documentation :-

    I would also recommend you to go through and check all steps are followed and configuration are correct or not.


    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments