Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 已修剪Node.js中的powershell命令行_Javascript_Node.js_Powershell_Cmd_Console - Fatal编程技术网

Javascript 已修剪Node.js中的powershell命令行

Javascript 已修剪Node.js中的powershell命令行,javascript,node.js,powershell,cmd,console,Javascript,Node.js,Powershell,Cmd,Console,我需要使用Node.js使用Powershell读取一些数据。我尝试了几个不同的包来使用Node.js中的powershell命令并从控制台获取数据,但每次从powershell获取数据时,数据都会被修剪 例如,我的命令是: Get-ChildItem -Exclude *procesy | sort CreationTime -desc Powershell窗口中的正确结果为: PS C:\Data> Get-ChildItem -Exclude *procesy | sort Cre

我需要使用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_过程,我还可以得到修剪过的数据:

const xxxx = spawn('powershell.exe', ['Get-ChildItem', '-Path', dir], {
});

xxxx.stdout.on('data', (data) => {
    onePrintStringData = data.toString();
  console.log(onePrintStringData);
});

使用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);
});
我想使用Powershell,因为我已经有了一个命令,可以通过使用修改日期进行排序来获取文件。使用Node.js,要做到这一点有些困难:/

不必太难。您可以使用
fs.readdir
获取文件名,然后使用
fs.stat
获取修改日期,然后在修改日期排序,然后提取文件名,如下所示:

const fs=require('fs');
const path=require('path');
const dir=';
const files=fs.readdirSync(dir)
.map(文件名=>({
名称:fileName,
时间:fs.statSync(path.join(dir,fileName)).mtime.getTime()
}))
.sort((a,b)=>a.time-b.time)
.map(v=>v.name);
console.log(文件);

为什么要从JS调用powershell命令?为什么不在powershell脚本中编写整个脚本(或者在JS中编写整个脚本)?@Olian04我想使用powershell,因为我已经有了使用修改日期排序的命令。使用Node.js更难做到:/Powershell本身会根据表格格式的配置和窗口的列(宽度)将表格格式的数据输出到shell窗口中,从而产生奇怪的结果。Likley node.js正在实例化一个减小了大小的窗口,而powershell表现不好,或者node.js无法很好地处理powershell如何移动数据。仅在PS中编写,或仅在node.js中编写可能会产生更好的结果。另外,如果您真的想对node.js执行PS,可以使用
convertto-json
cmdlet将对象转换为json格式,并让node.js从powershell读取该格式。类似于
(getchilditem-Exclude*procesy | sortcreationtime-desc)| converttojson
。或者,您可以让powershell使用列表格式,然后痛苦地将内容刮到node.js、ofc中您自己的表中。谢谢大家,我选择了js方式:)谢谢!现在一切都很好:D只在JavaScript中使用它是更好的方法;)@迈克尔:太好了。请按向上投票箭头旁边的复选标记接受我的答案。这样,其他用户将看到问题得到了令人满意的回答。
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);
});