Imagine what you could do with Powershell Direct

PowerShell what?!

What is PowerShell Direct? It is a new feature that allows you to manage your virtual machine directly from the Hyper-V host. This feature was shown at Ignite and I was really excited about it. So imagine you have provisioned a VM and you want to manage. But you want to manage it with PowerShell and “remote” directly into the VM to run your scripts, or even push your DSC configuration to the VM. The VM doesn’t need to have connectivity at all to start using this. In this blog post I am going to take one example of what you can do with PowerShell Direct. I found out it only works now on Windows 10 from build 10074 and Server 2016 TP2 build 10074 and your virtual machine has to be also Server 2016 TP2. To use PowerShell Direct you can invoke a command in the VM by running on your Hyper-V host:

Invoke-Command –VMName MyVmName –ScriptBlock { my fancy PowerShell Script } –Credential (Get-Credential)

or to use a interactive session use:

Enter-PSSession –VMName MyVmName –Credential (Get-Credential)

In this demonstration I am going to provision a Domain Controller using Desired State Configuration (DSC) and PowerShell Direct. You need a couple of DSC resources in order to run the DSC configuration. You can download them from https://github.com/Powershell/DSCResources (From the xDscResources folder). Download the xNetworking, xComputerManagement and the xActiveDirectory. I provisioned a VM and ran the OOBE to configure the Administrator password. Then I enabled the Guest Services on the VM because I am doing a Copy-VMFile and that needs the Guest Services in the VM. So you don’t t need to enable it if you just running PowerShell Direct only.

Apply the settings and start the VM. Now open a ISE and copy this script to your ISE. As you can see in the script I have put my source files on the C:\DSC. There is a folder DscResources with the DSC resources I downloaded earlier. This might not be the best practice DSC example that is out on the internet but for my demo to illustrate PowerShell Direct it is working well:

When you have the script copied and the VM running you can start the script. The server will reboot several times to “Make it so”.

Let me in the meantime describe what the script is doing:

The Name of the VM is put in a variable $VM. Then it takes all the DSC resources I have downloaded and copy them to the virtual machine I have created using the Copy-VMFile command.

Then it started the first PowerShell Direct command by moving the copied files to the Powershell Modules folder on the VM. First I put my credentials (the one to login to the VM) in a variable $cred. Then I invoke the command and started to move the copied DSC resources to the Powershell Modules directory.

Next part is to set the DSC Local Configuration Manager of the VM to allow reboot and autocorrect the config when it is “Not so”.

Than final part is to apply the DSC configuration:

And 15 minutes later I have a Domain Controller:

I this example I created with PowerShell Direct in combination with DSC a virtual machine without ever interacting with it. Ok, I used the OOBE wizard in this example, but assume you insert a unattend.xml.

Just imagine what you can achieve with this cool new feature when you need to deploy many VMs and you incorporate Desired State Configuration…

Spread the word. Share this post!