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

How to do it...

Method 1, using the Nano Server Recovery Console:

  1. Launch Hyper-V management console, and locate the VM running Nano Server.
  2. Double-click the VM to bring up the recovery console.
  3. Enter the username administrator and the password that you defined when you created the VM.
  4. Nano Server then display basic information about the server with a menu of options. Choose Networking from this menu by pressing Enter, then press Enter again to choose the default adapter.
  5. Your Nano Server's IP configuration is displayed, with key navigation options displayed at the bottom of the screen. Note your IP address to use to connect later in this recipe.
  1. Press F11 at this screen to configure your IP address, if desired. Then press F4 to change from DHCP to static IP, and use the Tab key to move between the IP Address, Subnet Mask, and Default Gateway fields, and enter the desired values.

Method 2, using PowerShell Direct:

  1. From the Hyper-V host, open PowerShell ISE. List the VMs:
      Get-VM -Name N*
  1. Store the Nano Server VM name and administrator credential in variables:
      $NanoComputerName = 'NANO1'
      $Credential = Get-Credential `
          -Message "Enter administrator password for target VM:" `
          -UserName administrator
  1. Get the running processes using Invoke-Command via PowerShell Direct:
      Invoke-Command -VMName $NanoComputerName -Credential $Credential 
-ScriptBlock { Get-Process }
  1. Enter an interactive PowerShell remoting session via PowerShell Direct:
      Enter-PSSession -VMName $NanoComputerName -Credential $Credential
  1. You are connected just like that in a PowerShell remoting session! Create and use a test folder in your Nano server:
      New-Item -ItemType Directory -Path C:\foo `
-ErrorAction SilentlyContinue
Set-Location C:\foo
  1. Gather information about your server using the new Get-ComputerInfo cmdlet:
      Get-ComputerInfo -Property CsName, WindowsEditionId, 
OSServerLevel, `
OSType, OSVersion, WindowsBuildLabEx, BiosBIOSVersion
  1. Examine $PSVersionTable, noting the value of the PSEdition property:
      $PSVersionTable
  1. Get the IP Address of your Nano Server, noting it for later recipe steps:
      Get-NetIPAddress -AddressFamily IPV4 -InterfaceAlias Ethernet | 
          Select-Object -ExpandProperty IPAddress
  1. If required, change the IP Address of your Nano Server, and display the new IP:
      New-NetIPAddress -InterfaceAlias 'Ethernet' `
                       -IPAddress 10.10.10.131 `
                       -PrefixLength 24 `
                       -DefaultGateway 10.10.10.254
      Get-NetIPAddress -InterfaceAlias 'Ethernet' -AddressFamily IPv4
  1. If required, set the DNS of your Nano Server, and display the DNS information:
      Set-DnsClientServerAddress -InterfaceAlias 'Ethernet' `
                                 -ServerAddresses 10.10.10.10, 
10.10.10.11
Get-DnsClientServerAddress
  1. Exit your remoting session:
      Exit-PSSession

Method 3, Using PowerShell Remoting:

  1. PowerShell remoting requires that the remoting target computer IP should be among the TrustedHosts defined on your computer. Add the IP Address of the Nano Server to our computer's TrustedHosts and verify the value:
      $NanoServerIP = '10.10.10.131'
      Set-Item -Path WSMan:\localhost\Client\TrustedHosts `
-Value $NanoServerIP -Force
Get-Item -Path WSMan:\localhost\Client\TrustedHosts
  1. Verify WSMan connectivity to the Nano Server:
      Test-WSMan -Path $NanoServerIP
  1. Connect via PowerShell remoting to the Nano Server:
      Enter-PSSession -ComputerName $NanoServerIP `
-Credential $Credential
  1. Use Get-ComputerInfo to inspect the Nano Server:
      Get-ComputerInfo -Property CsName, WindowsEditionId, 
OSServerLevel, OSType, OSVersion,
WindowsBuildLabEx, BiosBIOSVersion
  1. Exit your remoting session:
      Exit-PSSession

Method 4, Using WMI with the CIM cmdlets:

  1. Create a new CIM session on the Nano Server, and view the $CimSession object:
      $CimSession = New-CimSession -Credential $Credential `
-ComputerName $NanoServerIP
$CimSession
  1. Examine the properties of the Win32_ComputerSystem CIM class:
      Get-CimInstance -CimSession $CimSession `
-ClassName Win32_ComputerSystem |
Format-List -Property *
  1. Count the CIM classes available:
      Get-CimClass -CimSession $CimSession | Measure-Object
  1. View the running processes using the CIM_Process WMI class and a WMI query:
      Get-CimInstance -CimSession $CimSession `
-Query "SELECT * from CIM_Process"
  1. Remove your CIM Session:
      Get-CimSession | Remove-CimSession