Home > OS >  Safely get the UUID of a windows machine?
Safely get the UUID of a windows machine?

Time:03-03

I have an adobe 3rd party tool that I’m selling and I’m using the command line on windows to get the UUID for the user using:

wmic path win32_computersystemproduct get uuid

Unfortunately it doesn’t work for a small amount of users who are getting an error that the file requested can’t be found.

Is there a better way to find the UUID of one’s machine? If not, is there a way to modify this code to not throw an error if the file isn’t found? Try catch on my JavaScript side doesn’t seem to catch the error.

Thank you so much!

CodePudding user response:

You may use my wmiClass.bat program version 1.3; just copy the .BAT file from previous link. After that, use it with the standard class name:

wmiClass.bat Win32_ComputerSystemProduct uuid

... or via the wmic.exe alias:

wmiClass.bat CSProduct uuid

This program traps the error if the WMI class or wmic.exe Alias does not exists and ends with an error message and %errorlevel% equal 1.

CodePudding user response:

Thank you everyone! I didn't expect so much help so quickly. My current solution (although lacking) is this:

 if (isWin) {
    var systemRoot = system.callSystem("cmd /c echo %SystemRoot%") || "";
    var rootDrive = systemRoot.substring(0, 2);
    //var isRootDriveValid = rootDrive.length == 2 && rootDrive.charAt(1) == ":";
    var wmicPath = rootDrive   "\\Windows\\System32\\wbem\\WMIC.exe";
    var wmic = new File(wmicPath);

    if (!wmic.exists) {
        // wmic is not installed or not found
        // Do something else instead of system.callSystem()
    } else {
        // wmic is installed
        // system.callSystem("wmic path win32_computersystemproduct get uuid");
    }
    
}

I have tested this with multiple users and it solved the issue for them. I could verify using this method that WMIC.exe was missing on their machine.

However it's worth noting that I barley know anything when it comes to the command line, so please read the comments as they provided a lot of in depth solutions that I will keep trying to learn.

As Mofi mentioned in the comments, callSystem() didn't like me using %SystemRoot%, so I'm first getting it using echo (Is there a better way?). It seems to work, but if someone wants to expand on my lack of knowledge on the topic please do.

Much love.

  • Related