Virtualisation is a great thing – without we’re back to building physical servers and all the fun that entails. When I’m presenting at conferences I’ll often build a number of VMs just for the demo. I couldn’t do that with physical servers – the airline baggage cost would be horrendous when flying across the Atlantic.

Once we’ve built our VMs we need to configure them – maybe using the configuration management tool of your choice or you may fall back on a script. Many people use scripts in their test labs.

The problem with using a script is connecting to the new VM. PowerShell remoting is great, and easy, when your machine and the remote machine are in the same domain. Life gets more difficult when you want to connect to the new VM – especially when you don’t know its name (removing the ability to set the machine name during install is one of the worst things ever). You can put the IP address of the new machine in the trusted hosts file and use the remoting session options to get around the non-domain issue but that’s messy and assumes you know the IP address!

I’ve just had to rebuild my demo\test machine. I normally use a server operating system so the machine is my Hyper-V host and domain controller. Unfortunately, Windows Server 2016 RTM won’t work with my wireless network card. Windows 10 doesn’t have that problem so I have a Windows 10 host (which actually solves some other problems for me) and have created the first VM for my new environment. I need to configure this VM so that it can be my domain controller (among other things). Just to ensure things aren’t too easy it’s a server core installation as well so I can’t use the GUI tools. You could use the VM console but if you start working with Nano server you’ll only have remote connectivity so learning how it works is a useful skill.

My host is Windows 10 with Anniversary and all subsequent updates. This means I’m running PowerShell 5.1. One of the cool new features in PowerShell 5.1 (which is also available on Server 2016) is PowerShell Direct.

PowerShell Direct is a change to some of the remoting cmdlets so that you can use the VM name or ID (GIUD) instead of the computer name. The remoting works across Hyper-V’s vmbus rather than using winrm (WS-Man) for transport. The relevant cmdlets are:

  • Enter-PSSession
  • Get-PSSession
  • Invoke-Command
  • New-PSSession
  • Remove-PSSession

You won’t find this capability on any of the cmdlets, such as Get-Process, with individual remoting options and it’s not available for CIM sessions either.

So – new VM and a number of tasks to perform:

  • Copy the latest cumulative update to the VM and install it
  • Set the IP address
  • Rename the VM
  • Install AD Domain Services so I can turn it into a domain controller

The first task is problematic for new VMs. Making the connection to the host when you don’t have networking configured is usually like pulling a rabbit out of hat. But with PowerShell 5.1 its easy. You’ll probably make a number of connections to the VM so store the credential you’ll be using.

The local administrator account of the VM is a good one to use at this point rather than creating an account and giving it privileges. You can also create a PowerShell remoting session:

Now use the session to create a destination folder on the remote machine.

And copy the file.

Wait. What! Yep, we just copied a file across a PowerShell remoting session. That’s a new feature of PowerShell 5.0. You get -ToSession and -FromSession parameters on Copy-Item. They are mutually exclusive so you can’t copy from one remote machine to another but it’s a simple way to move files between machines.

Run the update and after the reboot you’ll need to recreate your session as the reboot broke it

Next job is to set the IP address and rename the machine. This time we’ll use an interactive session to demonstrate those. First enter the session

Notice that the prompt changes to show you the VM name. Change to another folder to shorten the prompt.

Get the network adapter and rename it to your standards

Get the interface index of the adapter

Set the IP address

 Show the current computer name – assigned by Windows at install.

Rename the computer.

WARNING: The changes will take effect after you restart the computer WIN-7U2ISN2QEHJ.

Restart the computer.

Your VM will reboot and break the connection. Next jobs are to install Active Directory and promote to a domain controller as normal.

The other use case that comes to mind for PowerShell Direct is when you have non-domain machines such as web servers in the DMZ. If you can get the Hyper-V host you’re then able to administer them. In case you think PowerShell Direct is going to solve all your problems there are a few things to remember:

  • Host is Windows 10 or Server 2016 running Hyper-V
  • Guest is Windows 10 or Server 2016
  • Guest must be on the host – no cross-host access
  • Guest must be running
  • Must log onto host as Hyper-V admin
  • Must supply valid credentials for LOCAL account on guest VM.

If you would like to learn more about using PowerShell Direct you should read this – https://www.altaro.com/hyper-v/powershell-script-deploy-vms-configure-guest-os-one-go/ which covers some advanced VM deployments and configuration techniques.

Additionally, if you’re more inclined to video, PowerShell Direct was recently the topic of Andy’s Latest Hyper-V Minute Below.


 Enjoy.