This weekend I stumbled across an issue when trying to mount a driver package (WIM file) in WinPE via the Mount-WindowsImage cmdlet, part of the Dism PowerShell module from WinPE. The error that was shown was:
Mount-WindowsImage : The 'Mount-WindowsImage' command was found in the module 'Dism', but the module could not be loaded.
The Real Error
After trapping the Mount-WindowsImage command in a try/catch block, it showed that the real error was related to that the Dism module couldn't be imported. The error was:
Could not load file or assembly 'file:///X:\Windows\system32\WindowsPowerShell\v1.0\Modules\Dism\Microsoft.Dism.PowerShell.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
Only at PXE boot
Interesting enough, the error only happened when PXE booting my machines, not when using a boot media (ISO) of the same boot image. After some research it turned out that somehow the Microsoft.Dism.PowerShell.dll file got corrupted during boot, which should be impossible, but still happened. Big thanks to John Wall at Just Another IT Blog for leading me in the right direction.
I'm still working on figuring out the root cause of why the Microsoft.Dism.PowerShell.dll file is getting corrupted during PXE boot (only), but for now I simply replaced the file early in the task sequence.
Note #1: Just replacing the file was not enough, I had to delete the existing one first.
Note #2: To make sure PowerShell didn't try importing the module and locking it from deletion, I used two run command line actions that used cmd.exe
Add three actions to your task sequence:
1. Connect to Network Folder (where you have a working copy of the Microsoft.Dism.PowerShell.dll)
2. Run Command Line action (I named itDelete Corrupt Dism Module), with the following command:
cmd /c del X:\Windows\system32\WindowsPowerShell\v1.0\Modules\Dism\Microsoft.Dism.PowerShell.dll
3. Run Command Line action (I named it Copy Dism Module), with the following command:
xcopy H:\WinPE_PowerShell_Modules_ADK_21H2\Dism\Microsoft.Dism.PowerShell.dll X:\Windows\system32\WindowsPowerShell\v1.0\Modules\Dism\ /y