2016 Hyper-V and PowerShell live [Quick Guide]

8:32 pm November 30, 201612396

I. introduction

new version of Hyper-V appeared at the same time as Windows Server 2016 is accompanied by a feature called “ PowerShell Direct “, it allows you to run a command on a virtual machine by disregarding the network layer and management settings remotely. ”

For PowerShell Direct, there are two ways to connect:

it is possible to open a PowerShell session remotely on a VM through an interactive session via ‘ Enter-PSSession ‘ (or New-PSSession to create a session and then use it). This mode of operation means that then we are left with a console connected to the VM, which is not useful if you want to only run a command.

It’s already present mode to connect to a server via PowerShell, except that in PowerShell live and access to VMs, specific settings will be used.

  • Run a script or a direct command

If you only perform an action without going into an interactive session, it will go directly through the commandlet ‘ Invoke-Command “, with specific settings here also.” This can be useful, for example, retrieve the IP address of the virtual machine, get a list of installed roles, or perform an action and features said… All without an interactive console on the VM.

II. Direct PowerShell: Requirements

using PowerShell directly implies the respect of some prerequisites, I tells you without further delay.

  • The user account with which you are logged on the Hyper-V must have the permissions of the Group administrators of Hyper-V
  • target the virtual machine must be started and running on the Hyper-V host on which you run PowerShell live
  • of the login credentials (login/password) of the virtual machine target will be necessary to authenticate

concerning the physical host OS and the virtual host OS , there are also requirements:

  • Hyper-V OS: Windows 10 or Windows Server 2016
  • OS VM: Windows 10 or Windows Server 2016

in other words, for now use will be limited because it’s exclusive to Windows Server 2016 and Windows 10, so hopefully it’s included at least in Windows Server 2012/2012 R2 soon – if it is possible…

III. Direct PowerShell: Syntax and example

we will look at the syntax of the two modes of PowerShell live, then we’ll take an example every time so that you see the operation.

  • PowerShell live: Interactive Mode

in interactive mode on a VM, used the commandlet ‘ Enter-PSSession “with the parameter”VMName”name VM or ‘VMId’ corresponding to ID unique to the VM. Either one or the other.

Which will give, for the name of the VM:

 Enter-PSSession - VMName  

and for the VM ID:

 Enter-PSSession - VMId  

for example:

 Enter-PSSession - VMId "bbc37868-a2f6-4d46-b1bb-5eb28ab90aaa" 

it will be sufficient to return the credentials to connect to the VM, and voila!

When the interactive session will be opened on the VM, you can interact in PowerShell with the virtual machine, as if the console was open during the local on the VM.

For example, we could list the installed roles and features:

Get-WindowsFeature |? { $_. InstallState – eq ‘Installed’}

Note: when the VM session is established, the PowerShell console prompt begins with the name of the VM. If the VM is called “ADDS-01” it will be indicated: “[ADDS-01]: PS C:UsersAdministrateurDocuments >”

move on to using the script in PowerShell Direct mode, you’ll quickly understand the difference in operation.

  • PowerShell live: Mode script

VMName and VMId parameters are also used with Invoke-Command to use in script PowerShell Direct mode. This command will add the “ScriptBlock” parameter that will contain a command you want to run on the VM.

So the syntax with the name of the VM will be:

 Invoke-Command - VMName  - ScriptBlock {} 

and with the ID:

 Invoke-Command - VMId  - ScriptBlock {} 

for example:

 Invoke-Command - VMId "bbc37868-a2f6-4d46-b1bb-5eb28ab90aaa"-ScriptBlock {Get-WindowsFeature |? {$_.}} InstallState - eq "Installed"}} 

here again, simply enter the identifiers and voila. We could also specify the identifiers in the Invoke-Command (or Enter-PSSession) command with the parameter ‘ Credential “.” To win some time already in the entry for example:

 Invoke-Command - VMId "bbc37868-a2f6-4d46-b1bb-5eb28ab90aaa"-ScriptBlock {Get-WindowsFeature |? {$_.}} InstallState - eq "Installed"}} - Credential (Get-Credential - UserName "ADDS-01Administrateur"-"Enter password" Message) 

to execute a complete script on the virtual machine, that’s all simple there is directly a parameter to specify the path to a .ps1 script. We use the – FilePath parameter, like this:

 Invoke-Command-"bbc37868-a2f6-4d46-b1bb-5eb28ab90aaa"-FilePath C:ScriptsMonScript.ps1

Vous VMId notice in interactive mode, there are two commands (or three if you count the logout command) while in script mode everything is done in the same command.

To complete (or almost), here is a screenshot that shows the rendering when you want to list the features and roles installed on a VM, with each of the two methods:

powershell-direct-2

for those who are wondering how to retrieve the ID of a virtual machine, just select the ‘Id’ property, this is for example how to list the VMs in a table with the name and the ID associated :

 Get - VM | FT Name, Id 

which will give for example:

powershell-direct-3