rannn505 / child-shell

Node.js bindings 🔗 for shell
http://rannn505.github.io/child-shell/
MIT License
299 stars 71 forks source link

Text decoding error when returning to resolve characters containing non-English #151

Open Danjuanlab opened 2 years ago

Danjuanlab commented 2 years ago

I want to use powershell to list the installed programs on the computer and determine if the user has installed the target program. But when running in vscode, when the return value contains Chinese, the decoding error of the result leads to garbled characters.

After some inquiries, I know that the problem is because the default encoding in the Chinese system is gbk. According to the existing solutions on the Internet, either modify the registry of the local computer or modify the language of the computer. But neither solution can be used as a software tool for customers.

OS:win11 64bit node-powershell:5.0.1

Here is my demo code.

import { PowerShell } from "node-powershell";
const powershellInstance = async () => {
  const ps = new PowerShell({
    debug: true,
    outputEncoding:'utf-8',
    executableOptions: {
      '-ExecutionPolicy': 'Bypass',
      '-NoProfile': true,
    },
  });

  try {
    const Name = ('^Google Chrome');
    const printCommand = PowerShell.command `Get-ItemProperty -Path "HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*" | Select-Object DisplayName, DisplayVersion, InstallDate, Version`;
    const  result = await ps.invoke(printCommand);

    console.log('11',result);
  } catch (error) {
    console.error(error);
  } finally {
    await ps.dispose();
  }
};
powershellInstance();

luanma luanma2

chendongde310 commented 4 months ago

我发现了一种方法

    const ps = new PowerShell({
        debug: false,
        outputEncoding:'binary',
        executableOptions: {
            '-ExecutionPolicy': 'Bypass',
            '-NoProfile': true,
        },
    });

            // 执行 PowerShell 命令并获取输出
        const printCommand = PowerShell.command`Get-WmiObject Win32_Process -Filter "name = 'xxx.exe'" | Select-Object  CommandLine`;
        const output =   await ps.invoke(printCommand);

        // console.log(output.stdout)
        const decodedDataLocal = iconv.decode( output.stdout , 'utf-8');
        const decodedData = iconv.decode(Buffer.from(decodedDataLocal, 'binary'), 'GBK');

        console.log(decodedData)