Convert WindowsImage PS1 File
- 1 Convert-WindowsImage.ps1
- 2 Problem #1; Error message and it won't run
- 3 Problem #2; No error message after using the Set-Execution command and it still won't run
- 4 Problem #3; Similar to #2 with no error message and it still won't run
- 5 Problem #4; The script just stopped during the generation of the VHDX file
- 6 Can it get worse? YES!
- 7 The End
- 8 The Origin and the Real Story's End
- 9 Conclusion
First, the disclaimer. This article is not about a step by step of how to use this utility. Rather it is about the problems I ran into using this utility.
Problem #1; Error message and it won't runBasically I typed the name of the file to run Convert-WindowsImage.ps1 and got a warning and remember immediately that I should have pre-pended .\ to the beginning of it.
Problem #2; No error message after using the Set-Execution command and it still won't run
This issue was related to all of the restrictions and protections Microsoft puts in place to protect us from all the evil in the world and at the same time make the PowerShell completely dysfunctional (or so it seems). You've might have already read about and tried the Set-Execution Policy to Unrestricted or Bypass and still have nothing work as happened to me. The key to figuring this out was a simple comment by a contributor on a forum. He said, "...downloaded from the internet." That made me think about these warning dialogs in Windows 10 that would pop up when I opened videos, music files, graphics, or anything else downloaded from the internet that put up an extra warning alluding to the file being from the internet and asking if I really wanted to open the file.
I did download the Convert-WindowsImage.ps1 file from the internet, but wait, it's just a text file, why don't I just create a new text file on my computer and copy and paste the contents of the original file into the new one? So I did. And it worked as the new file I created wasn't downloaded from the internet, but whose origins were local on my computer.
Problem #3; Similar to #2 with no error message and it still won't run
Another tip from another contributor on some forum noted that the entire Convert-WindowsImage.ps1 file was written as a Function. Go ahead, open up the file in a text editor and even if you don't know anything about programming, you'll see that 'Function' delimiter right at the top. In layman's terms it means if you type .\Convert-WindowsImage.ps1, it won't work. Maybe it did in the past (prior to 2018) with other versions of this script or other version of Windows, which is why there are so many sites that instruct one to simply type .\Convert-WindowsImage.ps1 and you'll be on your merry way. But if you're trying this in 2018 or later and using the latest version of the script (dated 2015 for me), then it won't work.
Another contributor from another pointed out a solution would be to put the same command one was typing at the end of the script file. So I did. And it worked.
IE, if you're typing...
Convert-WindowsImage -SourcePath "F:\WhatEverNamed.ISO" -VHDFormat VHDX -Edition ServerEnterprise -VHDPartition GPT -Verbose
...at a command line and it isn't doing anything, then put that same command at the very, very, very, last line end of the Convert-WindowsImage.ps1 file.T
If you don't know what to put for the -Edition switch, just run it once without the -Edition switch and it will of course error out, but, it will also give you a list of the different 'editions' that are on that DVD, so then add that to the script after the -Edition switch.
Problem #4; The script just stopped during the generation of the VHDX file
Really? All these issue aren't enough? No one to reference here. I just hit the CTRL C key combo to kill the script, but that caused it to continue and complete successfully without any errors. Hmmm. Who cares, it worked.
Can it get worse? YES!
OK! Great! I've got my VHDX file and I'm good to go, right? Of course not. After attaching the VHDX file to the Hyper-V VM, it wouldn't start because the VHDX file was still locked, even after terminating the script and checking Task Manager to see if the VHDX file was still in use. Nope, logging off and logging back on didn't work. Checked what process had the file locked using LockHunter (I'm not affiliated with this person or company, but it is really useful software so consider contributing to them) and it was locked by a process called System.
Don't ever end a process called System, especially if it has a PID (Process ID) of 4, which indicates it started fairly early in the Windows boot process and is probably important. Seems obvious. I learned this one about 20 or so years ago in the Windows NT 4 era.
So, reboot the server? Nope, a better idea. A simple one, just like the 'copy and paste' solution mentioned above, I just copied and pasted the VHDX to a new file. And that file wasn't locked. Virtual machine started with VHDX file!
And in the end it still didn't work. The origin of this story is that I was attempting to convert an ISO image into a VHDX file. I went through all of the above problems only to have it not work. Well, what should I expect, the ISO image wouldn't boot in a generation 2 Hyper-V machine, so why would that same ISO image converted into a VHDX file boot? It wouldn't. So I continued to hammer away at the problem.
And another tip from another contributor indicated that indeed the ISO was successfully booting, but was displaying the "Press any key to boot from CD or DVD..." so quickly there wasn't a chance to press a key, let alone see the message. This seemed credible to me as during booting, newer versions of Windows (8 plus I believe) no longer let one press the F8 key during boot to bring up the boot menu, that is unless one configures that to happen ahead of time, which if one is dealing with a crashed system isn't possible.
The Origin and the Real Story's End
My original objective in this story was to convert a server based on Windows Server 2008 R2 to a UEFI Generation 2 machine hosted on a Windows 2016 Server with Hyper-V.
I had read in Microsoft's documentation that Hyper-V on Server 2016 did support Windows Server 2008 R2. And it did. What I didn't read was the fine print about it requiring that W2K8R2 be configured as a Generation 1 machine, not a Generation 2 machine.
I know, I know, even Vista and maybe XP as a 64 bit machine can boot as a UEFI machine, which is the real difference between a Generation 1 and 2 Hyper-V machine. But it isn't about what is possible, it's about what Microsoft allows with their software. So if you don't like Pepsi, have a Coke, IE, ditch Hyper-V and head on over to VMWare. Keep in mind I still use Hyper-V on all of my servers, so I'm not actually recommending VMWare, I'm just pointing out there are other choices if Microsoft makes you unhappy.
I can be angry about wasting so many hours on a cause that resulted in a unsatisfactory conclusion. Or I can hope that writing this about my little journey will help someone else on theirs. I'll pick the latter.
Also, if things were right in the world and I had the time to put footnotes in this article attributing all of origins of this story to their original authors just like a properly documented story I would. But I don't. So I'll just say thank you to everyone that gave me the ideas for this tech story and if someone else writes something based on this article and doesn't reference me, I won't feel upset. : )