How Connect to a Azure Windows VM and run a remote script with PowerShell?

Viewed 5227

I am familiar with Linux envs and using SSH to run remote scripts and programs and automatic scripts from my desktop.

I would like to have a similar workflow with Windows VMs that I have on my Azure Account. However, I can´t find a straight forward instructions on how to build my local PowerShell scripts.

I need only to connect to a VM and call some scripts within it.

The best I could find would be this guide from MS https://docs.microsoft.com/en-us/azure/virtual-machines/windows/winrm

Or this a litte older blog post.

http://fabriccontroller.net/using-remote-powershell-with-windows-azure-virtual-machines/

2 Answers

I ran into a lot of trouble using the accepted answer, and found I wanted to use SSL in my remote execution. I could not find anywhere this was succinctly put, so here's what worked for me. Essentially, use the built-in Azure command to enable remote PowerShell on the VM, and then run secure remote sessions to your heart's content!

Invoke-AzureRmVMRunCommand -ResourceGroupName $vmResourceGroupName -Name $vmName -CommandId 'EnableRemotePS'
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $secureStringPassword
$sessionOptions = New-PSSessionOption -SkipCACheck -SkipCNCheck                 
Invoke-Command -ComputerName $ipAddress -Credential $cred -UseSSL -SessionOption $sessionOptions -FilePath $scriptPath
Related