Book Image

Windows Server 2016 Hyper-V Cookbook - Second Edition

By : Charbel Nemnom, Patrick Lownds, Leandro Carvalho
Book Image

Windows Server 2016 Hyper-V Cookbook - Second Edition

By: Charbel Nemnom, Patrick Lownds, Leandro Carvalho

Overview of this book

Hyper-V 2016 is full of new features and updates. The second of our best-selling Hyper-V books, the Windows Server 2016 Hyper-V Cookbook has it all covered. Brimming with expert solutions and techniques, you?ll have everything you need to master virtualization and Hyper-V Manager. This Hyper-V book is designed to help advanced-level administrators benefit fully from the new Windows Server. With over 80 hands-on recipes, the Hyper-V Cookbook gives you tips, tricks and best practices to deploy, maintain and upgrade your virtual machines.
Table of Contents (19 chapters)
Windows Server 2016 Hyper-V Cookbook - Second Edition
Credits
About the Authors
Acknowledgments
www.PacktPub.com
Customer Feedback
Preface
Index

Managing Nano Server using PowerShell


PowerShell version 5 comes with a lot of features that Nano Server benefit from. The main feature are:

  • Copying files via PowerShell sessions

  • Remote file editing in PowerShell ISE

  • Interactive script debugging over PowerShell session

  • Remote script debugging within PowerShell ISE

  • Remote host process connects and debugs

Getting ready

If you want to manage your Nano Server right now, you can use PowerShell Remoting or if your Nano Server is running in a Virtual Machine you can also use PowerShell Direct which is covered in Chapter 4, Saving Time and Cost with Hyper-V Automation.

How to do it

In order to manage Nano server installation using PowerShell remoting, carry out the following steps:

  1. You might need to start the WinRM service on your desktop to enable remote connections. From the PowerShell console type the following commands:

    net start WinRM
    
  2. In the PS console, type the following, substituting the server name or IP with the appropriate value (using your machine-name is the easiest to use, but if your device is not named uniquely on your network, try the IP address):

    Set-Item WSMan:\localhost\Client\TrustedHosts -Value "servername or IP"
    
  3. If you want to connect multiple devices, you can use comma and quotation marks to separate each machine, as shown here:

    Set-Item WSMan:\localhost\Client\TrustedHosts -Value "servername or IP, servername or IP"
    

    You can also set it to allow it to connect to a specific network subnet using the following command:

    Set-Item WSMan:\localhost\Client\TrustedHosts -Value 10.10.100.*
    
  4. To test Windows PowerShell remoting against Nano Server if it's working, you can use the following command:

    Test-WSMan -ComputerName "servername or IP" -Credential servername\Administrator -Authentication Negotiate
    
  5. Now you can start a session with your Nano Server. From your administrator PS console, type:

    Enter-PSSession -ComputerName "servername or IP" -Credential servername\Administrator
    
  6. Now we will create and deploy two virtual machines on Nano Server Hyper-V host using the PowerShell remoting feature. From your management machine, launch PS console or PS ISE as Administrator, type:

    #region Variables
    
    $NanoSRV = 'NANOSRV-HV01'
    $Cred = Get-Credential "Demo\SuperBook"
    $Session = New-PSSession -ComputerName $NanoSRV -Credential $Cred
    $CimSesion = New-CimSession -ComputerName $NanoSRV -Credential $Cred
    $VMTemplatePath = 'C:\Temp'
    $vSwitch = 'Ext_vSwitch'
    $VMName = 'DemoVM-0'
    
    #endregion
    
    Get-ChildItem -path $VMTemplatePath -filter *.VHDX -recurse | `
    Copy-Item -Destination D:\ -ToSession $Session 
    
    1..2 | % {
    
    New-VM -CimSession $CimSesion -Name $VMName$_ -VHDPath "D:\$VMName$_.vhdx" -MemoryStartupBytes 512MB `
    -SwitchName $vSwitch -Generation 2
    
    Start-VM -CimSession $CimSesion -VMName $VMName$_ -Passthru    
    
    }
    

How it works

In this script, we created a PowerShell session and a CIM session to Nano Server, then we copied VM Templates from the management machine to Nano Server, when the copy is done, we created two Generation 2 VMs and finally started them up.

After a couple of seconds, we launched Hyper-V Manager console and observed the new two VMs running on Nano Server host, as shown in the following screenshot:

As mentioned earlier, if you have installed Nano Server in a Virtual Machine running on a Hyper-V host, you can use PowerShell Direct to directly connect from your local Hyper-V host to your Nano Server VM using the following command:

Enter-PSSession -VMName "VMName" -Credential servername\Administrator  

Moreover, if you have Nano Server as a Hyper-V host, as shown in the preceding example, you could use PowerShell remoting to connect to Nano Server from your management machine and then leverage PowerShell Direct to manage your virtual machines, welcome to Nested PowerShell Remoting (PSRemoting + PSDirect).

To do this use the following command:

#regionVariables
$NanoSRV = 'NANOSRV-HV01' #Nano Server name or IP address
$DomainCred = Get-Credential "Demo\SuperBook"
$LocalCred = Get-Credential "~\Administrator"
$Session = New-PSSession -ComputerName $NanoSRV -Credential $DomainCred
#endregion

Invoke-Command -ComputerName $NanoSRV -Credential $DomainCred -ScriptBlock {
                param ($LocalCred)
                Get-VM
                Invoke-Command -VMName (Get-VM).Name -Credential $LocalCred -ScriptBlock {
                                hostname;Tzutil /g}
} -ArgumentList $LocalCred

In this script, I established a PowerShell session into Nano Server host, and then I used PowerShell Direct to query all VMs and get their hostnames and time zones.

Here is the output: