I need to read some data using Powershell using Node.js. I tried several different packages to use the powershell command in Node.js and get data from the console, but every time I get data from Powershell the data is trimmed.
For example, my command is:
Get-ChildItem -Exclude *procesy | sort CreationTime -desc
The correct results in the Powershell window are:
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
But I get the trimmed results in Node.js console window:
C:\!Temp\ExplorerFolderManager>npm run start
> [email protected] 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
I tried a node-cmd package (trimmed data from powershell console). With child_process I get trimmed data also:
const xxxx = spawn('powershell.exe', ['Get-ChildItem', '-Path', dir], {
});
xxxx.stdout.on('data', (data) => {
onePrintStringData = data.toString();
console.log(onePrintStringData);
});
or
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}`);
});
Using the node-powershell package this same - trimmed data :/
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);
});
convertto-jsoncmdlet to convert the object to the json format and have node.js read that in from powershell. Something like(Get-ChildItem -Exclude *procesy | sort CreationTime -desc) | ConvertTo-json. Alternatively, you could have powershell use list format, and painfully scrape that content into a table of your own in node.js, ofc.