Skip to content

Commit 2b77e6a

Browse files
authored
Merge pull request #20 from nfedyk/feature/support-empty-argument-values
Added support for the empty arguments passed to the command
2 parents ad374a0 + 73abe7c commit 2b77e6a

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,48 @@ There is also option to run Docker based tests. You need to configure `environme
6262

6363
Exchange online tests will be skipped if the connection is not available.
6464

65+
### Empty Argument Values Support
66+
67+
As of version 1.1.5, the module now supports passing empty string values to PowerShell command arguments when explicitly configured. This is useful for optional parameters that need to be passed as empty values rather than omitted entirely.
68+
69+
To enable empty value support for a command argument, set the `empty` property to `true` in the argument configuration:
70+
71+
```javascript
72+
const commandRegistry = {
73+
'myCommand': {
74+
command: "Get-Content {{{arguments}}}",
75+
arguments: {
76+
'Path': {},
77+
'Filter': {
78+
empty: true, // Allow empty string values
79+
},
80+
},
81+
return: {
82+
type: "text",
83+
}
84+
}
85+
};
86+
```
87+
88+
When `empty: true` is set, the argument will accept empty string values and include them in the generated PowerShell command:
89+
90+
```javascript
91+
// This will generate: Get-Content -Path './test.txt' -Filter ''
92+
await psCommandService.execute("myCommand", {
93+
Path: "./test.txt",
94+
Filter: "" // Empty string value is now allowed
95+
});
96+
```
97+
98+
6599

66100
### <a id="history"></a>History
67101

68102
```
103+
v1.1.5 - 2025-09-19
104+
- Added support for empty argument values in commands via 'empty' property
105+
- Fixed argument value bleed into the next empty argument
106+
69107
v1.1.4 - 2024-11-22
70108
- Extended testing and fixed escaping reserved variables and special characters in commands
71109
@@ -75,6 +113,7 @@ v1.1.3 - 2024-11-14
75113
v1.1.2 - 2022-07-06
76114
- Added support for usage of reserved powershell variables in commands [$null, $true, $false]
77115
116+
78117
v1.1.1 - 2020-12-07
79118
- Fixed bug import of custom commands if provided for certificate based auth
80119

psCommandService.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ PSCommandService.prototype._generateCommand = function(commandConfig, argument2V
282282
if ((argument.hasOwnProperty('valued') ? argument.valued : true)) {
283283

284284
var isQuoted = (argument.hasOwnProperty('quoted') ? argument.quoted : true);
285+
var isEmpty = (argument.hasOwnProperty('empty') ? argument.empty : false);
285286
var passedArgValues = argument2ValueMap[argumentName];
286287

287288
if (!(passedArgValues instanceof Array)) {
@@ -314,7 +315,7 @@ PSCommandService.prototype._generateCommand = function(commandConfig, argument2V
314315
}
315316

316317
// append the value
317-
if (valueToSet && valueToSet.trim().length > 0) {
318+
if (valueToSet !== null && valueToSet !== undefined && (isEmpty || valueToSet.trim().length > 0)) {
318319

319320
// sanitize
320321
valueToSet = this._sanitize(valueToSet,isQuoted);

test/unit.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ const commandRegistry = {
9696
command: "Get-Content {{{arguments}}}",
9797
arguments: {
9898
'Path': {},
99+
'Filter': {
100+
empty: true,
101+
},
99102
},
100103
return: {
101104
type: "text",
@@ -652,4 +655,55 @@ describe("test PSCommandService w/ o365CommandRegistry", function () {
652655
}, 5000);
653656
}
654657
});
658+
it("Should test empty value support", async function () {
659+
this.timeout(10000);
660+
const statefulProcessCommandProxy = new StatefulProcessCommandProxy({
661+
name: "Powershell pool",
662+
max: 1,
663+
min: 1,
664+
idleTimeoutMS: 30000,
665+
666+
logFunction: logFunction,
667+
processCommand: "pwsh",
668+
processArgs: ["-Command", "-"],
669+
processRetainMaxCmdHistory: 30,
670+
processCwd: null,
671+
processEnvMap: null,
672+
processUid: null,
673+
processGid: null,
674+
initCommands: initCommands,
675+
validateFunction: (processProxy) => processProxy.isValid(),
676+
});
677+
678+
const psCommandService = new PSCommandService(
679+
statefulProcessCommandProxy,
680+
commandRegistry,
681+
myLogFunction
682+
);
683+
try {
684+
const newResult = await psCommandService.execute("setContent", {
685+
Path: "./test.txt",
686+
Value: "Test",
687+
Filter: ""
688+
});
689+
assert.equal(newResult.command.trim(), "Set-Content -Path './test.txt' -Value 'Test'");
690+
assert.equal(newResult.stderr, "");
691+
const getResult = await psCommandService.execute("getContent", {
692+
Path: "./test.txt",
693+
Filter: ""
694+
});
695+
assert.equal(getResult.command.trim(), "Get-Content -Path './test.txt' -Filter ''");
696+
assert.equal(getResult.stderr, "");
697+
assert.equal(getResult.stdout, "Test");
698+
} catch (e) {
699+
assert.fail(e);
700+
} finally {
701+
await psCommandService.execute("removeItem", {
702+
Path: "./test.txt",
703+
});
704+
setTimeout(() => {
705+
statefulProcessCommandProxy.shutdown();
706+
}, 5000);
707+
}
708+
});
655709
});

0 commit comments

Comments
 (0)