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

Goel, Akanksha 66 Reputation points
2022-08-25T10:28:12.38+00:00

Not able to connect to VM:

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

Test-Connection -ComputerName 10.164.104.38

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

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

Get-SSHSession

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,114 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,363 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Carlos Villagomez 1,031 Reputation points Microsoft Employee
    2022-08-25T23:07:07.66+00:00

    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.

    Thanks!
    Carlos V.


  2. Limitless Technology 39,351 Reputation points
    2022-08-26T08:55:42.323+00:00

    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 :- https://learn.microsoft.com/azure/automation/troubleshoot/runbooks

    I would also recommend you to go through https://learn.microsoft.com/azure/automation/how-to/private-link-security 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