3

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://learn.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/

10
  • what have you tried? Show your effort. We are not a script delivery service. If you are stuck somewhere then we are here to help you out on that. To begin with go through THIS Commented Jul 19, 2017 at 17:03
  • @RanadipDutta I said I would like to do the same thing one can do with SSH in Linux. Should I link the dozens of youtube videos or MS tutorials I´ve been reading for the past 3 days? The link you posted is the basic to run a VM, but it doesn´t explain how to get access to it, Commented Jul 19, 2017 at 17:32
  • So did you take the steps listed in the first article that you linked? Were their errors? What have you actually tried and what results did you get from those attempts? That first article pretty much sums up the way that I have remotely accessed Azure VMs in the past. Commented Jul 19, 2017 at 18:03
  • @EBGree the problem is that I already have the VM and the MS guide ask to create a new VM with new configuration. Commented Jul 19, 2017 at 18:28
  • Well, that bit is kind of important. You need to have the cert on the VM or you can't connect. You can always remote into the VM and add the cert. Of course if you ever take that VM down and rebuild it you would have to do that step manually again. That is why they have you do it in the ARM template. Commented Jul 19, 2017 at 18:50

2 Answers 2

3

According to your description, we can use New-Pssession to execute script to stop/start service, like this:

$username = 'jason'
$pass = ConvertTo-SecureString -string 'password' -AsPlainText -Force
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $pass
$s = New-PSSession -ConnectionUri 'http://23.99.82.2:5985' -Credential $cred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)
Invoke-Command -Session $s -ScriptBlock {Get-Process PowerShell}

Result like this: enter image description here enter image description here

Another way, we can use Azure custom script extension to run script, we can upload script to Azure storage account, and use Set-AzureRmVMCustomScriptExtension to set custom script:

PS C:\> Set-AzureRmVMCustomScriptExtension -ResourceGroupName "ResourceGroup11" -Location "Central US" -VMName "VirtualMachine07" -Name "ContosoTest" -TypeHandlerVersion "1.1" -StorageAccountName "Contoso" -StorageAccountKey <StorageKey> -FileName "ContosoScript.exe" -ContainerName "Scripts"

But custom script only can run one time, if you want to re-run this script, we should remove it with this command Remove-AzureRmVMCustomScriptExtension, then re-set it. More information about Azure custom script extension, please refer to this link.

Sign up to request clarification or add additional context in comments.

7 Comments

Do I need only to open the 5985 port and start WinRM on my VM?
@ThadeuAntonioFerreiraMelo Yes, we should open port 5985 in NSG inbound rules, and open port 5985 in windows firewall inbound rules. By default, Azure winrm is running, so we just open port then we can connect it via winrm. hope this helpful:)
@ThadeuAntonioFerreiraMelo Just want to confirm the current situations. Please feel free to let us know if you need further assistance.
Yes Jason, it did work. I´m now trying to use PExec to open GUI app remotely.
My app is a GUI app and it seems Invoke-command can´t open a GUI aplication. I our local network and PCs we can use PSExec and run those app remotely. But following the same exemples we have the same problem as this guy stackoverflow.com/questions/45006040/… . I´m following this instructions ss64.com/nt/psexec.html
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.