最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

in javascript how to call powershell script - Stack Overflow

programmeradmin4浏览0评论

I have a powershell script and I run on powershell like :

.\download-packages-license.ps1

But I want to call the javascript file before these lines.

var json =fs.readFileSync('../../dev/licenses/AllLicenses.json', 'utf8');
var options = {pact: true, ignoreComment: true, spaces: 4};
var result = convert.json2xml(json, options);

I could not anything in stackoverflow except : How to run a powershell script from javascript? So pls help thanks

I have a powershell script and I run on powershell like :

.\download-packages-license.ps1

But I want to call the javascript file before these lines.

var json =fs.readFileSync('../../dev/licenses/AllLicenses.json', 'utf8');
var options = {pact: true, ignoreComment: true, spaces: 4};
var result = convert.json2xml(json, options);

I could not anything in stackoverflow except : How to run a powershell script from javascript? So pls help thanks

Share Improve this question asked Mar 5, 2019 at 12:26 AslıAslı 1151 gold badge3 silver badges9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

I think this will work for you -

var spawn = require("child_process").spawn;
spawn("powershell.exe",[".\download-packages-license.ps1"]);

You can work with : Node-Powershell

Code Snippet :

const Shell = require('node-powershell');

const ps = new Shell({
  executionPolicy: 'Bypass',
  noProfile: true
});

ps.addCommand('echo node-powershell');
ps.invoke()
.then(output => {
  console.log(output);
})
.catch(err => {
  console.log(err);
});

JScript

Simple way.

Works great. Suitable for simple operations, but loses some data when receiving line feeds as a single \n instead of the expected \r\n. Split by \r\n misinterprets the array which leads to the need to reformat the array of strings. Basically, I just wrote this, so there may be other problems.

var codepage='windows-1251';/*US-Europe-1252 and Js file in that codepage*/
var toPStext='Hello.\nПроверка русских буковок.';
var shell=new ActiveXObject('WScript.Shell');
var std=shell.Exec("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy Bypass -mand \
    $OutputEncoding = [Console]::outputEncoding = [System.Text.Encoding]::GetEncoding('"+codepage+"'); \
    Write-Output '"+toPStext+"'");
var output = std.StdOut.ReadAll().split('\r\n');// split('\n') - leads to the loss of some data
if (output.length>0){WScript.echo(output)}
//var x=WScript.StdIn.ReadLine();   

Line by line.

Unfortunately, powershell does not accept external data as sequences of lines, unlike cmd.exe with /q /k options, which simplifies this code, but will turn around problems with multiline output code. Of course, if necessary, you can transfer the code as a base64 string to powershell

var codepage='windows-1251';/*US-Europe-1252 and Js file in that codepage*/
var toPStext='Hello.\nПроверка русских буковок.';
var shell=new ActiveXObject('WScript.Shell');
var output=[],errors=[],WshRunning=0,WshFinished=1,WshFailed=2,i=0,tryCount=0;
var std=shell.Exec("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy Bypass -mand \
$OutputEncoding = [Console]::outputEncoding = [System.Text.Encoding]::GetEncoding('"+codepage+"'); \
Write-Output '"+toPStext+"'");
do{
    if (std.Status==WshFailed){
        errors.push('String '+i+' data read error: \n   '+std.StdErr.ReadLine());
        tryCount++
    }
    else if(std.Status==WshRunning){
        output.push(std.StdOut.ReadLine());
        tryCount=0;
        WScript.Echo('Running ...')
    }
    else if(std.Status==WshFinished){
        var last=std.StdOut.ReadLine();
        if(last.length>0){output.push(last)};last=undefined;
        tryCount=21;
        WScript.Echo('Finished ...')
    }i++;
}while(tryCount<21);        
if (output.length>0){WScript.echo(output)}
if (errors.length>0){WScript.echo(errors)}
var x=WScript.StdIn.ReadLine();
发布评论

评论列表(0)

  1. 暂无评论