powershell-utils 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +50 -11
- package/index.js +33 -15
- package/package.json +1 -1
- package/readme.md +63 -12
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {ExecFileOptions} from 'node:child_process';
|
|
1
|
+
import type {ExecFileOptions, ExecFileSyncOptions} from 'node:child_process';
|
|
2
2
|
|
|
3
3
|
export type ExecutePowerShellOptions = ExecFileOptions & {
|
|
4
4
|
/**
|
|
@@ -9,6 +9,15 @@ export type ExecutePowerShellOptions = ExecFileOptions & {
|
|
|
9
9
|
readonly powerShellPath?: string;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
+
export type ExecutePowerShellSyncOptions = ExecFileSyncOptions & {
|
|
13
|
+
/**
|
|
14
|
+
Path to PowerShell executable.
|
|
15
|
+
|
|
16
|
+
@default powerShellPath()
|
|
17
|
+
*/
|
|
18
|
+
readonly powerShellPath?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
12
21
|
export type ExecutePowerShellResult = {
|
|
13
22
|
readonly stdout: string;
|
|
14
23
|
readonly stderr: string;
|
|
@@ -70,17 +79,9 @@ export function executePowerShell(
|
|
|
70
79
|
|
|
71
80
|
export namespace executePowerShell {
|
|
72
81
|
/**
|
|
73
|
-
Standard PowerShell arguments that prefix the encoded command
|
|
74
|
-
|
|
75
|
-
Use these when manually building PowerShell execution arguments for `spawn()`, `execFile()`, etc.
|
|
82
|
+
Standard PowerShell arguments that prefix the encoded command: `['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand']`
|
|
76
83
|
|
|
77
|
-
|
|
78
|
-
```
|
|
79
|
-
import {executePowerShell} from 'powershell-utils';
|
|
80
|
-
|
|
81
|
-
const arguments_ = [...executePowerShell.argumentsPrefix, encodedCommand];
|
|
82
|
-
childProcess.spawn(powerShellPath(), arguments_);
|
|
83
|
-
```
|
|
84
|
+
Exposed for debugging or for advanced use cases where you need to customize the arguments. For most cases, use `createArguments()` instead.
|
|
84
85
|
*/
|
|
85
86
|
export const argumentsPrefix: readonly string[];
|
|
86
87
|
|
|
@@ -119,4 +120,42 @@ export namespace executePowerShell {
|
|
|
119
120
|
```
|
|
120
121
|
*/
|
|
121
122
|
export function escapeArgument(value: unknown): string;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
Create the full arguments array for PowerShell execution.
|
|
126
|
+
|
|
127
|
+
Combines `argumentsPrefix` with the encoded command. Useful when using `spawn()`, `execFile()`, or other process execution methods.
|
|
128
|
+
|
|
129
|
+
@param command - The PowerShell command.
|
|
130
|
+
@returns Array of arguments ready to pass to a process spawner.
|
|
131
|
+
|
|
132
|
+
@example
|
|
133
|
+
```
|
|
134
|
+
import {spawn} from 'node:child_process';
|
|
135
|
+
import {powerShellPath, executePowerShell} from 'powershell-utils';
|
|
136
|
+
|
|
137
|
+
const args = executePowerShell.createArguments('Get-Process');
|
|
138
|
+
spawn(powerShellPath(), args);
|
|
139
|
+
```
|
|
140
|
+
*/
|
|
141
|
+
export function createArguments(command: string): string[];
|
|
122
142
|
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
Execute a PowerShell command synchronously.
|
|
146
|
+
|
|
147
|
+
@param command - The PowerShell command to execute.
|
|
148
|
+
@returns The stdout output as a string.
|
|
149
|
+
|
|
150
|
+
@example
|
|
151
|
+
```
|
|
152
|
+
import {executePowerShellSync} from 'powershell-utils';
|
|
153
|
+
|
|
154
|
+
const stdout = executePowerShellSync('Get-Process');
|
|
155
|
+
console.log(stdout);
|
|
156
|
+
```
|
|
157
|
+
*/
|
|
158
|
+
export function executePowerShellSync(
|
|
159
|
+
command: string,
|
|
160
|
+
options?: ExecutePowerShellSyncOptions
|
|
161
|
+
): string;
|
package/index.js
CHANGED
|
@@ -24,20 +24,29 @@ export const canAccessPowerShell = async () => {
|
|
|
24
24
|
return canAccessCache;
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
+
const argumentsPrefix = [
|
|
28
|
+
'-NoProfile',
|
|
29
|
+
'-NonInteractive',
|
|
30
|
+
'-ExecutionPolicy',
|
|
31
|
+
'Bypass',
|
|
32
|
+
'-EncodedCommand',
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
const encodeCommand = command => Buffer.from(command, 'utf16le').toString('base64');
|
|
36
|
+
|
|
37
|
+
const escapeArgument = value => `'${String(value).replaceAll('\'', '\'\'')}'`;
|
|
38
|
+
|
|
39
|
+
const createArguments = command => [...argumentsPrefix, encodeCommand(command)];
|
|
40
|
+
|
|
27
41
|
export const executePowerShell = async (command, options = {}) => {
|
|
28
42
|
const {
|
|
29
43
|
powerShellPath: psPath,
|
|
30
44
|
...execFileOptions
|
|
31
45
|
} = options;
|
|
32
46
|
|
|
33
|
-
const encodedCommand = executePowerShell.encodeCommand(command);
|
|
34
|
-
|
|
35
47
|
return execFile(
|
|
36
48
|
psPath ?? powerShellPath(),
|
|
37
|
-
|
|
38
|
-
...executePowerShell.argumentsPrefix,
|
|
39
|
-
encodedCommand,
|
|
40
|
-
],
|
|
49
|
+
createArguments(command),
|
|
41
50
|
{
|
|
42
51
|
encoding: 'utf8',
|
|
43
52
|
...execFileOptions,
|
|
@@ -45,14 +54,23 @@ export const executePowerShell = async (command, options = {}) => {
|
|
|
45
54
|
);
|
|
46
55
|
};
|
|
47
56
|
|
|
48
|
-
executePowerShell.argumentsPrefix =
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
'Bypass',
|
|
53
|
-
'-EncodedCommand',
|
|
54
|
-
];
|
|
57
|
+
executePowerShell.argumentsPrefix = argumentsPrefix;
|
|
58
|
+
executePowerShell.encodeCommand = encodeCommand;
|
|
59
|
+
executePowerShell.escapeArgument = escapeArgument;
|
|
60
|
+
executePowerShell.createArguments = createArguments;
|
|
55
61
|
|
|
56
|
-
|
|
62
|
+
export const executePowerShellSync = (command, options = {}) => {
|
|
63
|
+
const {
|
|
64
|
+
powerShellPath: psPath,
|
|
65
|
+
...execFileOptions
|
|
66
|
+
} = options;
|
|
57
67
|
|
|
58
|
-
|
|
68
|
+
return childProcess.execFileSync(
|
|
69
|
+
psPath ?? powerShellPath(),
|
|
70
|
+
createArguments(command),
|
|
71
|
+
{
|
|
72
|
+
encoding: 'utf8',
|
|
73
|
+
...execFileOptions,
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
};
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -13,11 +13,9 @@ npm install powershell-utils
|
|
|
13
13
|
```js
|
|
14
14
|
import {executePowerShell, powerShellPath} from 'powershell-utils';
|
|
15
15
|
|
|
16
|
-
// Execute a PowerShell command
|
|
17
16
|
const {stdout} = await executePowerShell('Get-Process');
|
|
18
17
|
console.log(stdout);
|
|
19
18
|
|
|
20
|
-
// Get PowerShell path
|
|
21
19
|
console.log(powerShellPath());
|
|
22
20
|
//=> 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'
|
|
23
21
|
```
|
|
@@ -64,7 +62,6 @@ Execute a PowerShell command.
|
|
|
64
62
|
```js
|
|
65
63
|
import {executePowerShell} from 'powershell-utils';
|
|
66
64
|
|
|
67
|
-
// Execute a PowerShell command
|
|
68
65
|
const {stdout} = await executePowerShell('Get-Process');
|
|
69
66
|
console.log(stdout);
|
|
70
67
|
```
|
|
@@ -99,16 +96,9 @@ Character encoding for stdout and stderr.
|
|
|
99
96
|
|
|
100
97
|
Type: `string[]`
|
|
101
98
|
|
|
102
|
-
Standard PowerShell arguments that prefix the encoded command
|
|
99
|
+
Standard PowerShell arguments that prefix the encoded command: `['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand']`
|
|
103
100
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
```js
|
|
107
|
-
import {executePowerShell} from 'powershell-utils';
|
|
108
|
-
|
|
109
|
-
const arguments_ = [...executePowerShell.argumentsPrefix, encodedCommand];
|
|
110
|
-
childProcess.spawn(powerShellPath(), arguments_);
|
|
111
|
-
```
|
|
101
|
+
Exposed for debugging or for advanced use cases where you need to customize the arguments. For most cases, use `createArguments()` instead.
|
|
112
102
|
|
|
113
103
|
### executePowerShell.encodeCommand(command)
|
|
114
104
|
|
|
@@ -151,3 +141,64 @@ const escaped = executePowerShell.escapeArgument("it's a test");
|
|
|
151
141
|
// Use in command building
|
|
152
142
|
const command = `Start-Process ${executePowerShell.escapeArgument(appName)}`;
|
|
153
143
|
```
|
|
144
|
+
|
|
145
|
+
### executePowerShell.createArguments(command)
|
|
146
|
+
|
|
147
|
+
Returns: `string[]`
|
|
148
|
+
|
|
149
|
+
Create the full arguments array for PowerShell execution.
|
|
150
|
+
|
|
151
|
+
Combines `argumentsPrefix` with the encoded command. Useful when using `spawn()`, `execFile()`, or other process execution methods.
|
|
152
|
+
|
|
153
|
+
#### command
|
|
154
|
+
|
|
155
|
+
Type: `string`
|
|
156
|
+
|
|
157
|
+
The PowerShell command.
|
|
158
|
+
|
|
159
|
+
```js
|
|
160
|
+
import {spawn} from 'node:child_process';
|
|
161
|
+
import {powerShellPath, executePowerShell} from 'powershell-utils';
|
|
162
|
+
|
|
163
|
+
const args = executePowerShell.createArguments('Get-Process');
|
|
164
|
+
spawn(powerShellPath(), args);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### executePowerShellSync(command, options?)
|
|
168
|
+
|
|
169
|
+
Returns: `string`
|
|
170
|
+
|
|
171
|
+
Execute a PowerShell command synchronously.
|
|
172
|
+
|
|
173
|
+
```js
|
|
174
|
+
import {executePowerShellSync} from 'powershell-utils';
|
|
175
|
+
|
|
176
|
+
const stdout = executePowerShellSync('Get-Process');
|
|
177
|
+
console.log(stdout);
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
#### command
|
|
181
|
+
|
|
182
|
+
Type: `string`
|
|
183
|
+
|
|
184
|
+
The PowerShell command to execute.
|
|
185
|
+
|
|
186
|
+
#### options
|
|
187
|
+
|
|
188
|
+
Type: `object`
|
|
189
|
+
|
|
190
|
+
The below option and also all options in Node.js [`child_process.execFileSync()`](https://nodejs.org/api/child_process.html#child_processexecfilesyncfile-args-options) are supported.
|
|
191
|
+
|
|
192
|
+
##### powerShellPath
|
|
193
|
+
|
|
194
|
+
Type: `string`\
|
|
195
|
+
Default: `powerShellPath()`
|
|
196
|
+
|
|
197
|
+
Path to PowerShell executable.
|
|
198
|
+
|
|
199
|
+
##### encoding
|
|
200
|
+
|
|
201
|
+
Type: `string`\
|
|
202
|
+
Default: `'utf8'`
|
|
203
|
+
|
|
204
|
+
Character encoding for the output.
|