Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

How it works...

In Method 1, you use the Nano Server Recovery Console from the Hyper-V manager. This technique is useful when the Nano Server has an invalid Ethernet configuration or an unknown IP address.

In step 1 and step 2, from Hyper-V manager, double-click on the NANO1 virtual machine to bring up the Nano Server Recovery Console.

In step 3, you log in with the username administrator and the password you provided during the creation of the VM:

In step 4, you view the summary information on your Nano Server and navigate through the screens using the keyboard, with the menu options displayed at the bottom:

When you select Networking from the initial screen, you see the Network Settings folder, like this:

In step 5, you view the networking configuration of your server, noting the IP address so you can access it later.

In step 6, you can modify your IP address. The default configuration uses DHCP. If your scenario requires it, disable DHCP and define a valid static IP Address, Subnet Mask, and Default Gateway:

In Method 2, you use PowerShell Direct. PowerShell Direct is a new feature introduced in Hyper-V in Windows Server 2016 (and Windows 10). PowerShell Direct enables PowerShell scripting against Windows Server 2016 and Windows 10 virtual machines directly through the hypervisor, without requiring network, firewall, or remoting configuration. PowerShell Direct simplifies deployment and automation scripting for Hyper-V hosted virtual machines.

In step 1, you get the list of VM names from the local Hyper-V server:

In step 2, you store the VM name and credential in a variable for later use in the recipe:

In step 3, you view the running processes from the VM using Invoke-Command with a script block of Get-Process. Note that the -VMName parameter indicates the use of PowerShell Direct:

In step 4, you enter an interactive PowerShell remoting session through PowerShell Direct using the -VMName parameter:

In step 5, you connect to the VM, note the VM name in the prompt and that your current directory is in the default Documents folder. Create and use a test folder in your Nano Server:

In step 6, you gather computer information using a new cmdlet in PowerShell 5.1, Get-ComputerInfo:

In step 7, $PSVersionTable has a PSEdition of Core rather than Desktop. The Core version supports a subset of the features of the full Desktop edition of PowerShell:

In step 8, you get the IPAddress property from the Get-NetIPAddress cmdlet:

In step 9, you can set your IP address:

In step 10, you set your DNS:

In step 11, exit your remoting session. Note the change in the prompt:

With Method 3, you use regular PowerShell remoting. In step 1, you connect to the Nano Server VM. Before you can do so, you need to add the VMs IP address to your TrustedHosts on your Windows 2016 server. Note that you can set this value to an asterisk (*) which enables you to connect to any remoting capable computer:

In step 2, you test your remoting connectivity to the VM using Test-WSMan:

In step 3, you remote into the VM with PowerShell remoting:

In step 4, as you gather computer information. Note that the -Property is optional, leave it out or pass in an asterisk (*) to display a great deal of useful information about the target computer:

In step 5, you end your remoting session:

In Method 4, you use Windows Management Instrumentation (WMI) and the cmdlets in the CimCmdlets module to work with your Nano Server. WMI is an open standard that describes managed IT elements as objects with properties and relationships. The CIMCmdlets module contains the preferred cmdlets you use to manage Windows Servers via WMI. PowerShell in Windows Server 2016 supports both the older WMI cmdlets (contained in the Microsoft.PowerShell.Management module) and the newer CIMCmdlets module. Nano Server ships with CIMCmdlets, not WMI.

In step 1, you create a CIM session to the Nano Server using the credential and IP address you defined earlier, and you store it in the variable $CimSession:

In step 2, within this CIM session, you access an instance of the Win32_ComputerSystem class to view information about the Nano Server:

In step 3, you gather a count of the classes:

In step 4, you query the CIM_Process class:

In step 5, you remove your CIM session: