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

How to do it...

  1. From your Hyper-V host, view the currently installed package providers:
        Get-PackageProvider
  1. View the available package providers online, noting the NanoServerPackage provider:
       Find-PackageProvider | Select-Object -Property Name, Summary | 
Format-Table -AutoSize -Wrap
  1. Install the NanoServerPackage provider:
        Install-PackageProvider -Name NanoServerPackage -Verbose
  1. View the commands included with the provider:
        Get-Command -Module NanoServerPackage
  1. View the available Nano Server packages:
        $NanoPackages = Find-NanoServerPackage | 
Select-Object -Property Name, Description
$NanoPackages | Format-Table -AutoSize -Wrap
  1. Determine which of the available packages you wish to install, store them as an array in the $Installpackages variable and then display that array:
      $InstallPackages = @('Microsoft-NanoServer-Storage-Package',
                           'Microsoft-NanoServer-IIS-Package', 
                           'Microsoft-NanoServer-DSC-Package')
      $InstallPackages
  1. Define the path to the Windows Server 2016 installation media:
    $Server2016InstallationRoot = 'E:\'
  1. Define the path of the NanoServerImageGenerator folder:
      $NanoServerFolder = Join-Path -Path $Server2016InstallationRoot 
-ChildPath 'NanoServer'
$NsigFolder = Join-Path -Path $NanoServerFolder
-ChildPath 'NanoServerImageGenerator'
$NsigFolder
  1. Import the NanoServerImageGenerator module and review the commands contained in that module:
      Import-Module -Name $NsigFolder
      Get-Command -Module NanoServerImageGenerator
  1. Define the folders for the base Nano Server images and the VM images:
      $NanoBaseFolder = 'C:\NanoBase'
      $VMFolder = 'D:\VMs'
  1. Define paths for the Nano Server VM:
      $NanoComputerName = 'NANO2'
      $NanoVMFolder = Join-Path -Path $VMFolder 
-ChildPath $NanoComputerName
$NanoVMPath = Join-Path -Path $NanoVMFolder
-ChildPath "$NanoComputerName.vhdx"
  1. Define the networking parameters:
      $IPV4Address = '10.10.10.132'
      $IPV4DNS = '10.10.10.10','10.10.10.11'
      $IPV4Gateway = '10.10.10.254'
      $IPV4SubnetMask = '255.255.255.0'
  1. Build a hash table $NanoServerImageParameters to hold parameters for the New-NanoServerImage cmdlet:
      $NanoServerImageParameters = @{
          DeploymentType = 'Guest'
          Edition = 'DataCenter'
          TargetPath = $NanoVMPath
          BasePath = $NanoBaseFolder
          DomainBlobPath = $DomainJoinBlobPath
          Ipv4Address = $IPV4Address
          Ipv4Dns = $IPV4DNS
          Ipv4Gateway = $IPV4Gateway
          IPV4SubnetMask = $IPV4SubnetMask
          Package = $InstallPackages
      }
  1. Create a new Nano Server image, passing in configuration parameters using splatting:
      New-NanoServerImage @NanoServerImageParameters
  1. Once complete, review the VM switches available, and define the Hyper-V switch to use:
        Get-VMSwitch | Select-Object -ExpandProperty Name
        $SwitchName = 'Internal'
  1. Create the Nano virtual machine from the newly created VM disk, and start the VM:
      New-VM -VHDPath $NanoVMPath `
-Name $NanoComputerName `
-Path $NanoVMFolder `
-SwitchName $SwitchName `
-Generation 2 -Verbose |
Start-VM