Creating a remote PowerShell Direct shortcut

So here is the scenario:

  1. I have a remote Hyper-V server running Nano Server.
  2. I have PowerShell remoting setup to manage this system (details on how to do this here: https://technet.microsoft.com/en-us/windows-server-docs/get-started/manage-nano-server).
  3. I then have a number of Nano Server virtual machines running on the this system.

What I want is to be able to easily access PowerShell in each of these virtual machines.  What I want to avoid is configuring each one to have PowerShell Remoting configured – and to have that port exposed on the network.  The simple solution here is to use PowerShell Direct.

What I have actually done is to create a simple little script like this:

$VM = "NS1"
$HVServer = "hyperv.test.com"
$c = get-credential
$session = New-PSSession -ComputerName $HVServer -Credential $c
invoke-command -Session $session {Enter-PSSession -VMName $using:VM -credential $using:c}
Enter-PSSession $session

Then I create a shortcut that launches:

Powershell.exe -NoExit -File C:\Users\benja\Desktop\NS1.PS1

Now when I double click on that shortcut – I get a prompt for my credentials, and I get connected directly to my virtual machine.  First a PowerShell session is created on the Hyper-V server – then a second session is created to the virtual machine.  Finally I am connected to it.

Nice and simple.

Cheers,
Ben