Open Danjuanlab opened 2 years 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)
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.