获取window10电脑系统型号,系统名称,系统制造商等信息

184 阅读1分钟

'powershell.exe Get-CimInstance -ClassName Win32_ComputerSystem' 获取window10电脑系统型号,系统名称,系统制造商等信息

import {spawn} from 'child_process';

// https://stackoverflow.com/questions/10179114/execute-powershell-script-from-node-js
// https://www.windowscentral.com/how-find-computer-model-number-windows-10#check_pc_model_powershell
function win32ComputerSystem() {
    return new Promise((resolve, reject) => {
        const child = spawn('powershell.exe', ['Get-CimInstance -ClassName Win32_ComputerSystem']);
        // const child = spawn('powershell.exe Get-CimInstance', ['-ClassName', 'Win32_ComputerSystem']);
        let chunks = '';
        child.stdout.on('data', function(data) {
            // data === [object Uint8Array]
            chunks += data.toString('utf8').replace(/(\r|\n)/g, '\n');
        });
        child.stderr.on('data', function(data) {
            reject(data);
        });
        child.on('exit', function() {
            chunks = chunks
                .split('\n')
                .map(item => {
                    return item.includes('--') ? '' : item.replace(/( +)/g, ' ').trim();
                })
                .filter(item => item)
                .reduce(
                    (total, item, index) => {
                        if (index === 0) {
                            total.header = item.split(' ');
                        } else {
                            const newItem = {};
                            const value = item.split(' ');
                            total.header.forEach((item, index) => {
                                newItem[item] = value[index];
                            });
                            total.values.push(newItem);
                        }
                        return total;
                    },
                    {header: [], values: []}
                );
            resolve(chunks.values[0]);
        });
        child.stdin.end(); // end input
    });
}

window电脑 - 搜索 - 系统信息 - 系统摘要

image.png