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:localhostClientTrustedHosts -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:localhostClientTrustedHosts -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:localhostClientTrustedHosts -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 servernameAdministrator -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 servernameAdministrator
    
  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 "DemoSuperBook"
    $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 servernameAdministrator  

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 "DemoSuperBook"
$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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.147.87