【发布时间】:2021-01-04 10:32:03
【问题描述】:
我需要使用 Node.js 使用 Powershell 读取一些数据。我尝试了几个不同的包来使用 Node.js 中的 powershell 命令并从控制台获取数据,但是每次从 Powershell 获取数据时,数据都会被修剪。
比如我的命令是:
Get-ChildItem -Exclude *procesy | sort CreationTime -desc
Powershell 窗口中的正确结果是:
PS C:\Data> Get-ChildItem -Exclude *procesy | sort CreationTime -desc
Directory: C:\Users\Misiek\Desktop\Skrypty\!Backup
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 01.01.2021 03:22 966 01.01.2021 03-22-52 - katalogi Explorer.txt
-a---- 01.01.2021 03:22 5784 01.01.2021 03-22-52 - otwarte procesy.txt
-a---- 01.01.2021 03:32 1010 01.01.2021 03-32-54 - katalogi Explorer.txt
-a---- 01.01.2021 03:32 5478 01.01.2021 03-32-54 - otwarte procesy.txt
-a---- 01.01.2021 03:42 1064 01.01.2021 03-42-55 - katalogi Explorer.txt
-a---- 01.01.2021 03:42 5820 01.01.2021 03-42-55 - otwarte procesy.txt
-a---- 01.01.2021 03:52 1064 01.01.2021 03-52-57 - katalogi Explorer.txt
-a---- 01.01.2021 03:52 5460 01.01.2021 03-52-57 - otwarte procesy.txt
-a---- 01.01.2021 04:02 1064 01.01.2021 04-02-58 - katalogi Explorer.txt
-a---- 01.01.2021 04:02 5462 01.01.2021 04-02-58 - otwarte procesy.txt
-a---- 01.01.2021 04:13 1064 01.01.2021 04-13-00 - katalogi Explorer.txt
-a---- 01.01.2021 04:13 5464 01.01.2021 04-13-00 - otwarte procesy.txt
-a---- 01.01.2021 04:23 1064 01.01.2021 04-23-01 - katalogi Explorer.txt
-a---- 01.01.2021 04:23 5462 01.01.2021 04-23-01 - otwarte procesy.txt
-a---- 01.01.2021 04:33 1064 01.01.2021 04-33-03 - katalogi Explorer.txt
-a---- 01.01.2021 04:33 5462 01.01.2021 04-33-03 - otwarte procesy.txt
-a---- 01.01.2021 04:43 1064 01.01.2021 04-43-05 - katalogi Explorer.txt
-a---- 01.01.2021 04:43 5462 01.01.2021 04-43-05 - otwarte procesy.txt
但我在 Node.js 控制台窗口中得到了修剪后的结果:
C:\!Temp\ExplorerFolderManager>npm run start
> ExplorerFolderManager@1.0.0 start C:\!Temp\ExplorerFolderManager
> electron .
Directory: C:\Users\Misiek\Desktop\Skr
ypty\!Backup
Mode LastWriteTime Length
---- ------------- ------
-a---- 01.01.2021 03:22 966
-a---- 01.01.2021 03:22 5784
-a---- 01.01.2021 03:32 1010
-a---- 01.01.2021 03:32 5478
-a---- 01.01.2021 03:42 1064
-a---- 01.01.2021 03:42 5820
-a---- 01.01.2021 03:52 1064
-a---- 01.01.2021 03:52 5460
-a---- 01.01.2021 04:02 1064
-a---- 01.01.2021 04:02 5462
-a---- 01.01.2021 04:13 1064
-a---- 01.01.2021 04:13 5464
-a---- 01.01.2021 04:23 1064
-a---- 01.01.2021 04:23 5462
-a---- 01.01.2021 04:33 1064
-a---- 01.01.2021 04:33 5462
-a---- 01.01.2021 04:43 1064
-a---- 01.01.2021 04:43 5462
我尝试了一个 node-cmd 包(从 powershell 控制台修剪数据)。 使用 child_process 我还得到修剪数据:
const xxxx = spawn('powershell.exe', ['Get-ChildItem', '-Path', dir], {
});
xxxx.stdout.on('data', (data) => {
onePrintStringData = data.toString();
console.log(onePrintStringData);
});
或
exec('powershell "Get-ChildItem -Path ' + dir + ' -Exclude *procesy.txt"', (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
使用相同的 node-powershell 包 - 修剪数据:/
const ps = new Shell({
executionPolicy: 'Bypass',
noProfile: true
});
ps.addCommand('powershell "Get-ChildItem -Path ' + dir + ' -Exclude *procesy.txt"');
ps.invoke()
.then(output => {
console.log(output);
})
.catch(err => {
console.log(err);
});
【问题讨论】:
-
为什么要从 JS 调用 powershell 命令?为什么不把整个东西写在 powershell 脚本中(或者把整个东西写在 JS 中)?
-
@Olian04 我想使用 Powershell,因为我已经有一个命令来获取文件并使用修改日期进行排序。使用 Node.js 会更难一些:/
-
Powershell 本身在根据表格格式的配置方式和窗口的列(宽度)将表格格式的数据输出到 shell 窗口时会产生奇怪的结果。 Likley node.js 正在实例化一个尺寸减小的窗口,而 powershell 表现不佳,或者 node.js 无法很好地处理 powershell 如何转移数据。仅在 PS 中或仅在 node.js 中编写此代码可能会产生更好的结果
-
另外,如果您真的想对 node.js 执行 ps,您可以使用
convertto-jsoncmdlet 将对象转换为 json 格式并让 node.js 从 powershell 中读取。像(Get-ChildItem -Exclude *procesy | sort CreationTime -desc) | ConvertTo-json这样的东西。或者,您可以使用 powershell 使用列表格式,然后在 node.js 中痛苦地将这些内容刮到您自己的表格中。 -
谢谢大家,我选择了JS方式:)
标签: javascript node.js powershell cmd console