Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Danjuanlab opened this issue Jun 15, 2022 · 1 comment

Comments

@Danjuanlab
Copy link

Danjuanlab commented Jun 15, 2022

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
Copy link

我发现了一种方法

    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)
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants