从PowerShell脚本执行Node.js脚本

时间:2019-05-24 23:51:35

标签: javascript node.js powershell powershell-v2.0

我正在尝试从PowerShell脚本执行nodejs脚本。我正在将一些参数传递给Powershell并传递给nodejs。

在将它们传递给nodejs之前,我将它们绑定到JSON对象中。除以下情况外,它在大多数情况下都可以正常工作: 如果Powershell中参数的输入之一在两个单词之间有空格。 例如。

$requestBody='{"name":"Kiran","Place":"San Diego"}'
//Here I have space in "San Diego" if I pass "SanDiego" it worked.

PowerShell代码

$Header = '{"xyz-qer-01":"Tew23343"}'

$endPoint="https://r0zvc2xdx7.execute-api.us-west-2.amazonaws.com"
$accessKey="<>"
$screteKey="<>"
$method='GET'
$requestBody='{"name":"Kiran","Place":"SanDiego"}'
$pathTemplate='/dev/network-provisioning/api/v1/subnets'
$additionalParam='{"header":{"xyz-qer-01":"Tew23343"}}'

$Filename = AWSSigner.js;

$Param = @{"endPoint"=$endPoint;"accessKey"=$accessKey;"screteKey"=$screteKey;"method"=$method;"requestBody"=$requestBody | ConvertFrom-Json;"pathTemplate"=$pathTemplate;"additionalParam"=$additionalParam | ConvertFrom-Json} | ConvertTo-Json -Compress

$Param2 = $Param | ConvertTo-Json -Compress
Write-Output = $Param2


 $Result = C:\Users\kp250041\Desktop\Myfiles\node C:\Users\kp250041\Desktop\Myfiles\AWSSigner.js $Param2
 Write-Output $Result

Node JS代码

let awsbody = process.argv[2].split('\\').join(' ') || '{}';
console.log(awsbody);
var obj = JSON.parse(awsbody);

console.log("Main Request:" + "\n");
console.log(obj);

console.log("\n" + "obj.endPoint      - " + obj.endPoint);
console.log("obj.awsAccessKey         - " + obj.accessKey);
console.log("obj.awsSecreteKey        - " + obj.screteKey);
console.log("obj.awsPathTemplate      - " + obj.pathTemplate);
console.log("obj.method               - " + obj.method);
console.log(obj.additionalParam);
console.log(obj.requestBody);

这是错误

 C:\Users\kp250041\Desktop\Myfiles\node : undefined:1
 At line:21 char:11
 + $Result = C:\Users\kp250041\Desktop\Myfiles\node C:\Users\kp250041\De ...
 +           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 + CategoryInfo          : NotSpecified: (undefined:1:String) [], 
  RemoteException
  + FullyQualifiedErrorId : NativeCommandError

  {"method":"GET","pathTemplate":"/dev/network- provisioning/api/v1/subnets","accessKey":"AKIAJ4Y5DGRDNBQ2HBYA","additionalParam":{"header":{"xyz-qer-01":"Tew


 SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Object.<anonymous> 
(C:\Users\kp250041\Desktop\Myfiles\AWSSigner.js:18:16)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
at startup (internal/bootstrap/node.js:285:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

0 个答案:

没有答案