TechShizz | Tech Guides for everything in IT

Using PowerShell to Add Drivers / Packages / Install Roles and Features in a .wim file

The following reference folders that contain the relevent drivers/packages. Ensure you have the driver/package in the right location e.g. .\drivers

### DISM - Deployment Image Servicing and Management PowerShell Cmdlets ###

## .wim ##

# view cmdlets in DISM module
Get-Command -Module DISM

# view images within a .wim
Get-WindowsImage -ImagePath .\images\install.wim
Get-WindowsImage -ImagePath .\images\nanoserver.wim

# mount server core datacenter image
Mount-WindowsImage -ImagePath .\images\install.wim -Path .\mount -Index 3

# add drivers (.inf)
Get-WindowsDriver -Path .\mount
Add-WindowsDriver -Path .\mount -Driver .\drivers\iaStorAC.inf

# add packages (.msu or .cab)
Get-WindowsPackage -Path .\mount
Add-WindowsPackage -Path .\mount -PackagePath .\updates\windows10.0-kb3150513.msu

# install roles and features
Get-WindowsOptionalFeature -Path .\mount -FeatureName dhcpserver
Enable-WindowsOptionalFeature -Path .\mount -FeatureName dhcpserver
Disable-WindowsOptionalFeature -Path .\mount -FeatureName dhcpserver

# dismount and commit changes
Dismount-WindowsImage -Path .\mount -Save


## .vhd/vhdx ##

# mount nano server datacenter vhd
Mount-WindowsImage -ImagePath .\image\nanoserver.wim -Path .\mount -Index 2

# add iis package
Add-WindowsPackage -Path .\mount -PackagePath d:\nanoserver\packages\microsoft-nanoserver-iis-package.cab

# dismount and commit changes
Dismount-WindowsImage -Path .\mount -Save

 

 

Connecting to and Configuring Nano Servers

Before we can connect to a Nano server we need to add it to the trusted hosts list of the server you are connecting from.

Set-Item WSMan:\localhost\Client\TrustedHosts "192.168.1.100"

Then to create a new session to the server

$nano - New-PSSession -ComputerName 192.168.1.100 -Credential (Get-Credential)

Next, to join the Nano server to the Domain we need to use the Offline domain join method to create a blob file. This command should be run on the DC, so Exit-PSSession from the session above if still connected.

djoin.exe /provision /domain testlab.local /machine NanoServer1 /savefile .\djoin.blob

Next we need to copy this blob file to the C:\ on the NanoServer1. We can pass this file right into the session variable we made earlier.

Copy-Item -Path .\djoin.blob -DestinationPath C:\ -Tosession $nano

Next we connect back to the nano server to complete the join.

Enter-PSSession -Session $nano

Next we need to complete the join using the blob file on the NanoServer1.

djoin.exe /requestodj /loadfile C:\djoin.blob /windowspath C:\windows /localos

Once complete just reboot the nano server - You should then be able to log onto it with the domain administrator credentials.

Its also good practice to remove the IP from the trusted hosts list that we added earlier like this. 

Set-Item WSMan:\localhost\Client\TrustedHosts ""

Now that the NanoServer1 is a member of the domain, we can now run a remote powershell session to it via it's DNS name like this, and if we're logged in with adminsitrative credentials we dont even need to add the Get-Credential cmdlet.

Enter-PSSession -ComputerName NanoServer1

Installing Packages

To see a list of all Package Providers. These are online repositories that PowerShell can download packages from.

Find-PackageProvider

To find what packages a particular provider has run

Find-Package -ProviderName NanoServerPackage

where "nanoserverpackage" is the name of the desired package provider.

Next we need to install the package provider top use it.

Install-PackageProvider NanoServerPackage

Once installed we need to Import the Package Provider, like importing a module but it's a different command. 

Import-PackageProvider NanoServerPackage

Next in this example we want to install the storage package. Using the above commands to find this we should be able to run

Install-Package -Name Microsoft-NanoServer-Storage-Package

A reboot will be required.

Once rebooted we can connect to and manage this Nano server from Server Manager simply by adding it like any other server. 

Installing Nano Servers

To install a Nano server we need the Server 2016 ISO mounted and attached to the HV Host. we then run a series of PowerShell commands to installed the Nano Server.

#Import Nano server PS Module
Import-Module D:\NanoServer\NanoServerImageGenerator

#Create a basic NanoServer VHDX
New-NanoServerImage -MediaPath D: -BasePath .\Base -TargetPath .\NanoServer1.vhdx -DeploymentType Guest -Edition Datacenter -ComputerName NanoServer1 -AdministratorPassword (ConvertToSecureString -String 'Pa$$w0rd' -AsPlainText -Force)

#Create and Start the VM
New-VM -Name NanoServer1 -VHDPath .\NanoServer1.vhdx -MemoryStartupBytes 1GB -Generation 2 | Start-VM

#Begin Remote PowerShell session
Enter-PSSession -VMName NanoServer1

We can run the following to create a Nano Server with features already installed

#Import Nano server PS Module
Import-Module D:\NanoServer\NanoServerImageGenerator

#Create a NanoServer VHDX
New-NanoServerImage -MediaPath D: -BasePath .\Base -TargetPath .\NanoServer1.vhdx -DeploymentType Guest -Edition Datacenter -ComputerName NanoServer1 -InterfaceNameOrIndex Ethernet -Ipv4Address 192.168.16.10 -Ipv4SubnetMask 255.255.255.0 -Ipv4Gateway 192.168.16.1 -Ipv4Dns ("192.168.16.100","8.8.8.8") -Package Microsoft-NanoServer-IIS-Package -AdministratorPassword (ConvertToSecureString -String 'Pa$$w0rd' -AsPlainText -Force)

#View available packages
Get-NanoServerPackage -MediaPath D:

#create and start the VM
New-VM - Name NanoServer1 -VHDPath .\NanoServer1.vhdx -MemoryStartupByte 1GB -SwitchName vSwitch -Generatino 2 | Start-VM

#Start remote PS Session
Enter-PSSession -NMName NanoServer1

 

Remember, there is a GUI tool for this that can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=54065

 

 

 

Migrating Server Roles with SmigDeploy

Get-WindowFeature - Name migration

Install-WindowsFeature -Name migration

cd \windows\system32\servermigrationtools

.\smigdeploy.exe /package /architecture /amd64 /os WS12R2 /path C:\Migration

#copy the Smig files to the Source Machine
Copy-Item -Path C:\Migration\SMT_ws12r2_amd64 -Destination \\srv2012-lab\c$\Migration

.NET 3.5 is required on the source machine for this to work, so the first step (if it is not already installed) is to install it.

#Check is .NET3.5 is installed
Get-WindowsFeature -Name net-framework-core

#Install .NET 3.5 if required. You will need a 2012R2 ISO mounted for the source files

Install-WindowsFeature net-framework-core -source D:\sources\sxs

#Import smig cmdlets into session
Add-PSSnapin Microsoft.Windows.ServerManager.Migration

#Stop DHCP (the role we're migrating)
Stop-Service -Name DHCP

#View roles and features eligible for migration
Get-SmigServerFeature

#Export the migration data
Export-SmigServerSetting -FeatureID DHCP -Path c:\migration\DHCPMigration -verbose

#Finally copy this exported data back over to the target server
Copy-Item -Path C:\migration\DHPCMigration -Destination \\srv2016-lab\c$\migration

Back over on the Target server we now need to Import the settings

#Import smig cmdlets into session
Add-PSSnapin Microsoft.Windows.ServerManager.Migration

#Install DHCP role and import settings
Import-SmigServerSetting -FeatureID DHCP -Path C:\migration\DHCPMigration -verbode -force

 

Using DISM to convert Server 2016 Evaluation License to a normal License

Scenario

We have a copy of server 2016 installed on an evaluation license and we want to license it with our company license. Datacenter edition of Server 2016 is required to do this conversion.

Solution

We can use the Automatic Virtual Machine Activation keys to convert the license. We use DISM to convert the license like this:

dism /online /Set-Edition:ServerStandardCor /ProductKey:C3RCX-M6NRP-6CXC9-TW2F2-4RHYD /AcceptEula

See More on Automatic Virtual Machine Activation https://technet.microsoft.com/en-us/library/dn303421(v=ws.11).aspx

AVMA keys
  

The following AVMA keys can be used for Windows Server 2012 R2.

Edition AVMA key
Datacenter Y4TGP-NPTV9-HTC2H-7MGQ3-DV4TW
Standard DBGBW-NPF86-BJVTX-K3WKJ-MTB6V
Essentials K2XGM-NMBT3-2R6Q8-WF2FK-P36R2

The following AVMA keys can be used for Windows Server 2016.

Edition AVMA key
Datacenter TMJ3Y-NTRTM-FJYXT-T22BY-CWG3J
Standard C3RCX-M6NRP-6CXC9-TW2F2-4RHYD
Essentials B4YNW-62DX9-W8V6M-82649-MHBKQ