WMIC problem, Ionic 5

In later versions of Windows 10 WMIC seems to be deprecated. For example, if i try ionic info I get an error message:

Error: Command failed: wmic os get Caption

ERROR:
Description = Unspecified error


at makeError (C:\Users\andersp\AppData\Roaming\npm\node_modules\@ionic\cli\node_modules\execa\index.js:174:9)
at Function.module.exports.sync
(C:\Users\andersp\AppData\Roaming\npm\node_modules\@ionic\cli\node_modules\execa\index.js:338:15)
at windowsRelease
(C:\Users\andersp\AppData\Roaming\npm\node_modules\@ionic\cli\node_modules\windows-release\index.js:34:24)
at osName (C:\Users\andersp\AppData\Roaming\npm\node_modules\@ionic\cli\node_modules\os-name\index.js:39:18)
at Environment.getInfo (C:\Users\andersp\AppData\Roaming\npm\node_modules\@ionic\cli\lib\index.js:45:20)
at async InfoCommand.run (C:\Users\andersp\AppData\Roaming\npm\node_modules\@ionic\cli\commands\info.js:29:24)
at async Promise.all (index 0)
at async InfoCommand.execute (C:\Users\andersp\AppData\Roaming\npm\node_modules\@ionic\cli\lib\command.js:79:9)
at async Executor.run (C:\Users\andersp\AppData\Roaming\npm\node_modules\@ionic\cli\lib\executor.js:53:9)
at async Executor.execute
(C:\Users\andersp\AppData\Roaming\npm\node_modules\@ionic\cli\node_modules\@ionic\cli-framework\lib\executor.js:69:13)

This means I can no longer compile or do anything with my project.

I’m using Ionic 4 and Cordova 9. I’m pretty stumped as what to do now.

Regards,
Anders

I’m sorry, what is WMIC? I’m not sure I’ve heard of this before.

It’s a tool used on Windows Server machines to look up/specify different OS specifications (if I understand correctly). WMIC stands for Windows Management Instrumentation Command-line utility. It’s apparently deprecated and MS encourages the use of PowerShell commandlets instead. But it works on an older Win10 installation I have. But in this version it just returns an error.

The specific command it tries to run is: wmic os get caption, to determine which windows server version it’s running on.

The file that throws the error is this one:
C:\Users\andersp\AppData\Roaming\npm\node_modules@ionic\cli\node_modules\windows-release\index.js

And the code that crashes is this bit, from line 33:

	if ((!release || release === os.release()) && ['6.1', '6.2', '6.3', '10.0'].includes(ver)) {
		const stdout = execa.sync('wmic', ['os', 'get', 'Caption']).stdout || '';
		const year = (stdout.match(/2008|2012|2016/) || [])[0];
		if (year) {
			return `Server ${year}`;
		}
	}

If I comment out that bit everything works. It’s the same problem with Cordova, in file:
C:\Users\andersp\AppData\Roaming\npm\node_modules\cordova\node_modules\windows-release\index.js

I’m pretty new to Node, so is “windows-release” a common node module that can be updated?

Anyway, if I comment out those lines it all works again.

1 Like

After some more investigation it looks like this PowerShell command performs the same task:

Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object Caption

So I guess an update to the “windows-release” node module is needed. Which mean it can’t rely on “execa” but needs a PowerShell equivalent.

Updated: execa is still perfectly usable as per my post further down.

1 Like

If I change the code in “windows-release” to the following (use powershell instead of wmic) it works as it should:

	if ((!release || release === os.release()) && ['6.1', '6.2', '6.3', '10.0'].includes(ver)) {
		const stdout = execa.sync('powershell', ['(Get-CimInstance -ClassName Win32_OperatingSystem).caption']).stdout || '';
		const year = (stdout.match(/2008|2012|2016/) || [])[0];
		if (year) {
			return `Server ${year}`;
		}
	}
1 Like

To add just a little something to @landsfiskalen’s excellent detective work, windows-release #14 looks relevant.

1 Like

Ah, nice one! Didn’t see that, but looks about the same as what I came up with. I’m not sure how stable that solution is though. With regards to user privileges and such.

Do you recon someone will implement the powershell solution to “windows-release” in the future? Would be neat, that way I don’t need to change those rows when download the project to another computer. :slight_smile:

Judging from the level of activity on that repo, I can’t honestly say I would feel particularly sanguine about that prospect. I would love to make a PR myself, but the last time I used any version of Windows was 1987, so saying this is not exactly an area of competence for me would be a drastic understatement.

Hahaha, gotcha. Hmmmm, I’m pretty new to the whole git stuff. But I managed to clone it, and make the change, so maybe I’ll try a pull request and see what happens.

I think https://github.com/susam/gitpr is a good cribsheet for the general process, although, it may be a bit like trying to drink from a fire hydrant the first time you go through it.

Whoooa. Ok, now I know what I’ll do tonight. Thanks for all the input and help!

Managed to make a pull request, so fingers crossed.

1 Like

Link to the pull request?

Here you go. :slight_smile:

1 Like

I made a stupid mistake. By just replacing wmic for powershell it might break on older OS:es. So made a new commit to the pull request where I added an error check. So if the wmic call works it works like the old version, but if we get an error it tries the new Powershell command. So code now looks like this:

		let stdout;
		try {
			stdout = execa.sync('wmic', ['os', 'get', 'Caption']).stdout || '';
		} catch (error) {
			stdout = execa.sync('powershell', ['(Get-CimInstance -ClassName Win32_OperatingSystem).caption']).stdout || '';
		}

sindresorhus have accepted my PR, so “windows-release” should be updated soon, and everything should be working again.

1 Like

I can confirm this is now working as expected.

This was a heartening example of open source community contribution in action. Thanks for doing all this and also for keeping us updated here.

1 Like