Skip to content

Commit c5d9d2b

Browse files
Use $input automatic variable with ValueFromPipeline parameter
Changed scripts to read from $input instead of Console.In.ReadToEnd() while keeping the ValueFromPipeline parameter attribute as requested. Co-authored-by: adityapatwardhan <[email protected]>
1 parent 28c7fb0 commit c5d9d2b

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

adapters/powershell/Tests/TestAdapter/testadapter.resource.ps1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
param(
55
[Parameter(Mandatory = $true, Position = 0, HelpMessage = 'Operation to perform. Choose from List, Get, Set, Test, Export, Validate.')]
66
[ValidateSet('List', 'Get', 'Set', 'Test', 'Export', 'Validate')]
7-
[string]$Operation
7+
[string]$Operation,
8+
[Parameter(Mandatory = $false, Position = 1, ValueFromPipeline = $true, HelpMessage = 'Configuration or resource input in JSON format.')]
9+
[string]$jsonInput = '@{}'
810
)
911

10-
# Read JSON input from stdin for operations that need it
11-
$jsonInput = if ($Operation -ne 'List') {
12-
[System.Console]::In.ReadToEnd()
13-
if ([string]::IsNullOrWhiteSpace($jsonInput)) {
14-
$jsonInput = '@{}'
12+
# Read JSON input from stdin using $input automatic variable for operations that need it
13+
if ($Operation -ne 'List') {
14+
$stdinData = $input | Out-String
15+
if (-not [string]::IsNullOrWhiteSpace($stdinData)) {
16+
$jsonInput = $stdinData
1517
}
16-
} else {
17-
'@{}'
1818
}
1919

2020
function Write-DscTrace {

adapters/powershell/psDscAdapter/powershell.resource.ps1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
param(
55
[Parameter(Mandatory = $true, Position = 0, HelpMessage = 'Operation to perform. Choose from List, Get, Set, Test, Export, Validate, ClearCache.')]
66
[ValidateSet('List', 'Get', 'Set', 'Test', 'Export', 'Validate', 'ClearCache')]
7-
[string]$Operation
7+
[string]$Operation,
8+
[Parameter(Mandatory = $false, Position = 1, ValueFromPipeline = $true, HelpMessage = 'Configuration or resource input in JSON format.')]
9+
[string]$jsonInput = '@{}'
810
)
911

10-
# Read JSON input from stdin for operations that need it
11-
$jsonInput = if ($Operation -ne 'List' -and $Operation -ne 'ClearCache') {
12-
[System.Console]::In.ReadToEnd()
13-
if ([string]::IsNullOrWhiteSpace($jsonInput)) {
14-
$jsonInput = '@{}'
12+
# Read JSON input from stdin using $input automatic variable for operations that need it
13+
if ($Operation -ne 'List' -and $Operation -ne 'ClearCache') {
14+
$stdinData = $input | Out-String
15+
if (-not [string]::IsNullOrWhiteSpace($stdinData)) {
16+
$jsonInput = $stdinData
1517
}
16-
} else {
17-
'@{}'
1818
}
1919

2020
function Write-DscTrace {

adapters/wmi/wmi.resource.ps1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
param(
55
[Parameter(Mandatory = $true, Position = 0, HelpMessage = 'Operation to perform. Choose from List, Get, Set, Test, Validate.')]
66
[ValidateSet('List', 'Get', 'Set', 'Test', 'Validate')]
7-
[string]$Operation
7+
[string]$Operation,
8+
[Parameter(Mandatory = $false, Position = 1, ValueFromPipeline = $true, HelpMessage = 'Configuration or resource input in JSON format.')]
9+
[string]$jsonInput = '@{}'
810
)
911

10-
# Read JSON input from stdin for operations that need it
11-
$jsonInput = if ($Operation -ne 'List') {
12-
[System.Console]::In.ReadToEnd()
13-
if ([string]::IsNullOrWhiteSpace($jsonInput)) {
14-
$jsonInput = '@{}'
12+
# Read JSON input from stdin using $input automatic variable for operations that need it
13+
if ($Operation -ne 'List') {
14+
$stdinData = $input | Out-String
15+
if (-not [string]::IsNullOrWhiteSpace($stdinData)) {
16+
$jsonInput = $stdinData
1517
}
16-
} else {
17-
'@{}'
1818
}
1919

2020
# Import private functions

resources/PSScript/psscript.ps1

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
param(
55
[Parameter(Mandatory = $true, Position = 0)]
66
[ValidateSet('Get', 'Set', 'Test')]
7-
[string]$Operation
7+
[string]$Operation,
8+
[Parameter(Mandatory = $false, Position = 1, ValueFromPipeline = $true)]
9+
[string]$jsonInput = ''
810
)
911

10-
# Read JSON input from stdin
11-
$jsonInput = [System.Console]::In.ReadToEnd()
12+
# Read JSON input from stdin using $input automatic variable
13+
$stdinData = $input | Out-String
14+
if (-not [string]::IsNullOrWhiteSpace($stdinData)) {
15+
$jsonInput = $stdinData
16+
}
1217

1318
function Write-DscTrace {
1419
param(

0 commit comments

Comments
 (0)