-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
93 lines (80 loc) · 2.27 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const js2xmlparser = require("js2xmlparser");
const winrmSoapReq = require("nodejs-winrm/src/base-request");
const winrmHttpReq = require("nodejs-winrm/src/http");
function constructReceiveOutputRequest(_params) {
const res = winrmSoapReq.getSoapHeaderRequest({
action: "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive",
shellId: _params.shellId,
});
res["s:Body"] = {
"rsp:Receive": {
"rsp:DesiredStream": {
"@": {
CommandId: _params.commandId,
},
"#": "stdout stderr",
},
},
};
return js2xmlparser.parse("s:Envelope", res);
}
function generatePowershellCommand(_params) {
const args = [];
args.unshift(
"powershell.exe",
"-NoProfile",
"-NonInteractive",
"-NoLogo",
"-ExecutionPolicy",
"Bypass",
"-InputFormat",
"Text",
"-Command",
"\"& {",
_params.command,
"}\"",
);
return args.join(" ");
}
async function getResult(_params) {
const req = constructReceiveOutputRequest(_params);
const result = await (
winrmHttpReq.sendHttp(req, _params.host, _params.port, _params.path, _params.auth)
);
const body = result["s:Envelope"]["s:Body"][0];
const fault = body["s:Fault"];
if (fault) {
return new Error(fault[0]["s:Code"][0]["s:Subcode"][0]["s:Value"][0]);
}
const cmdResult = {
stdout: "",
stderr: "",
exitCode: parseInt(
body["rsp:ReceiveResponse"][0]["rsp:CommandState"][0]["rsp:ExitCode"][0],
10,
),
};
const rspStream = body["rsp:ReceiveResponse"][0]["rsp:Stream"];
if (rspStream) {
rspStream.forEach((stream) => {
if (stream.$.Name === "stdout" && !Reflect.has(stream.$, "End")) {
const bufferString = Buffer.from(stream._, "base64").toString("utf-8");
process.stdout.write(bufferString); // Live Logging
cmdResult.stdout += bufferString;
}
if (stream.$.Name === "stderr" && !Reflect.has(stream.$, "End")) {
const bufferString = Buffer.from(stream._, "base64").toString("utf-8");
process.stderr.write(bufferString); // Live Logging
cmdResult.stderr += bufferString;
}
});
}
if (cmdResult.exitCode === 0) {
return "";
}
return Promise.reject(cmdResult);
}
module.exports = {
getResult,
generatePowershellCommand,
};