Karush Logo

Powershell Remoting

Transfer files to Azure VM using Powershell

As part of my project to create a blog, I want to be able to easily create markdown (.md) files and upload them to web server.

There are a number of ways to do this. I could just add them to my web site code and re-publish the code each time. I could set up an FTP process. I could use the "Local Resources" feature via Remote Desktop (RDP).

Each of these are fine but more effort than I wanted for a simple file upload. On Linux there is this handy utility called scp (secure copy) that makes this pretty simple. I'm sure there is a third-party something for this on Windows but now we are back to more effort...

I use Powershell at work for some basic tasks, so I decided to see if I could make make Powershell Remoting work.

Powershell Remoting allows you to make a remote connection to a server via the command line - similar to RDP but command line based instead of GUI based.

Here is what I've done:

On my local PC:

  • Powershell is already installed on Windows 10. If you aren't familiar with Powershell think of it as a modern version of the legacy command prompt (cmd.exe).
  • Run Powershell as an Administrator (right-click Run as Administrator)
  • Run these commands:

Enable-PSRemoting -SkipNetworkProfileCheck -Force

(The -Force bypasses the "are you sure prompts", the -SkipNetworkProfileCheck allows it to work if your "network category" your you PC is "public")

  • Now since in this case we are connecting to an Azure VM we need to tell Powershell that we trust the VM, so enter:

Set-Item WSMan:localhost\client\trustedhosts -value "[IP address of the VM]"

  • Then confirm it's added by viewing it with:

Get-Item WSMan:\localhost\Client\TrustedHosts

Now the local PC is configured. Let's work on the server.

On the Azure VM Server:

  • Go to portal.azure.com, login, and select your existing VM
  • Select Security -> Networking
  • Select Add Inbound Port Rule
    • Source: select IP Address: enter your external IP address as is shown by WhatIsMyIP.com or similar website. This will lock down traffic to only your IP address.
    • Source Port Ranges: enter *
    • Destination: Any
    • Destination Port Ranges: 5985-5986 (5986 is for when SSL based secure remoting is used - not covered here)
    • Protocol: Any
    • Action: Allow
    • Priority: something like 1000 (lower than the "deny" options in the list)
    • Name: Powershell
  • Click Add

If you are running the software based Windows Firewall on your VM, you will also want to allow inbound connections on ports 5985-5986 (not covered here).

  • Now enable Windows Remoting on you VM with this command

Enable-PSRemoting -Force

You should see some status/config text. If you see a red error message something isn't configured correctly.

That's it. Now Windows Remoting is configured. Test it on your local PC with this command in Powershell:

Test-WsMan [IP address of the VM]

Now connect to your VM via the command line:

Enter-PSSession -cn "[IP address of the VM]" -credential [your server login]

You will then be prompted with a Windows dialog to enter your password for the remote server. If successful you will then see the command prompt in Powershell change to something like:

[IP address of the VM]: PS C:\Users\your login\Documents>

Note that you are now executing command on the remote VM. Enter "dir" for example. Enter "exit" to exit.

Powershell scripting is another topic, but I use the Powershell ISE to then transfer files with a script like this:

$creds = Get-Credential $session = New-PSSession -ComputerName "IP address of the VM" -Credential $creds Copy-Item "C:\localpath\powershellremoting.md" -Destination "C:\remotepath\blog" -ToSession $session -Force