Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Windows Server 2016 Hyper-V Cookbook

You're reading from   Windows Server 2016 Hyper-V Cookbook Save time and resources by getting to know the best practices and intelligence from industry experts

Arrow left icon
Product type Paperback
Published in Jan 2017
Publisher Packt
ISBN-13 9781785884313
Length 524 pages
Edition 2nd Edition
Languages
Tools
Concepts
Arrow right icon
Authors (4):
Arrow left icon
Patrick Lownds Patrick Lownds
Author Profile Icon Patrick Lownds
Patrick Lownds
Leandro Carvalho Leandro Carvalho
Author Profile Icon Leandro Carvalho
Leandro Carvalho
Leandro Eduardo S Carvalho Leandro Eduardo S Carvalho
Author Profile Icon Leandro Eduardo S Carvalho
Leandro Eduardo S Carvalho
Charbel Nemnom Charbel Nemnom
Author Profile Icon Charbel Nemnom
Charbel Nemnom
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Installing and Managing Hyper-V in Full, Server Core, and Nano Server FREE CHAPTER 2. Migrating and Upgrading Physical and Virtual Servers 3. Managing Disk and Network Settings 4. Saving Time and Cost with Hyper-V Automation 5. Hyper-V Best Practices, Tips, and Tricks 6. Security and Delegation of Control 7. Configuring High Availability in Hyper-V 8. Disaster Recovery for Hyper-V 9. Azure Site Recovery and Azure Backup for Hyper-V 10. Monitoring, Tuning, and Troubleshooting Hyper-V A. Hyper-V Architecture and Components 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:

How it works

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:

How it works
You have been reading a chapter from
Windows Server 2016 Hyper-V Cookbook - Second Edition
Published in: Jan 2017
Publisher: Packt
ISBN-13: 9781785884313
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image