Introduction to PowerShell Direct

Table of contents

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 demotest 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.

$cred = Get-Credential

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:

$sess = New-PSSession -VMName w16dc01 -Credential $cred

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

Invoke-Command -Session $sess -ScriptBlock {New-Item -ItemType Directory -Path c: -Name Source}

And copy the file.

Copy-Item -Path 'E:SourceWindows 2016 RTMAMD64-all-windows10.0-kb3192366-x64_af96b0015c04f5dcb186b879f07a31c32cf2e494.msu' -Destination c:Source -ToSession $sess

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

Remove-PSSession -Session $sess
$sess = New-PSSession -VMName w16dc01 -Credential $cred

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

PS> Enter-PSSession -Session $sess

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

[W16DC01]: PS C:UsersAdministratorDocuments> cd c:source

Get the network adapter and rename it to your standards

[W16DC01]: PS C:source> Get-NetAdapter | Rename-NetAdapter -NewName LAN

Get the interface index of the adapter

[W16DC01]: PS C:source> $index = Get-NetAdapter | select -ExpandProperty ifIndex

Set the IP address

[W16DC01]: PS C:source> New-NetIPAddress -InterfaceIndex $index -AddressFamily IPv4 -IPAddress '10.10.54.10' -PrefixLength 24

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

[W16DC01]: PS C:source> $env:COMPUTERNAMEWIN-7U2ISN2QEHJ

Rename the computer.

[W16DC01]: PS C:source> Rename-Computer -NewName W16DC01 -Force

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

Restart the computer.

[W16DC01]: PS C:source> Restart-Computer -Force

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.

Altaro Hyper-V Backup
Share this post

Not a DOJO Member yet?

Join thousands of other IT pros and receive a weekly roundup email with the latest content & updates!

4 thoughts on "Introduction to PowerShell Direct"

  • Jeff Chase says:

    Just wanted to say THANKS Altaro for all the great free Hyper-V related information you continually put out. Has really been helpful in my migration from VMware to Hyper-V

Leave a comment or ask a question

Your email address will not be published. Required fields are marked *

Your email address will not be published. Required fields are marked *

Notify me of follow-up replies via email

Yes, I would like to receive new blog posts by email

What is the color of grass?

Please note: If you’re not already a member on the Dojo Forums you will create a new account and receive an activation email.