question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PSUseProcessBlockForPipelineCommand does not recognize use of $input within an End block

See original GitHub issue

Steps to reproduce

Invoke-ScriptAnalyzer -IncludeRule PSUseProcessBlockForPipelineCommand `
	-ScriptDefinition 'Param([Parameter(ValueFromPipeline=$true)][psobject] $Item); End {$input |ConvertTo-Json}'

Expected behavior

(no output)

Actual behavior

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseProcessBlockForPipelineCommand Warning                 1     Command accepts pipeline input but has not defined a
                                                                  process block.

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.4
PSEdition                      Core
GitCommitId                    7.3.4
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.21.0

Issue Analytics

  • State:closed
  • Created 4 months ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
JamesWTruhercommented, May 9, 2023

@brianary using $input in the end is not something that we recommend and will likely lead to misunderstandings as the following example shows.

function f1 {
    param ([parameter(ValueFromPipeline=$true)]$a)
    BEGIN   { "input value: '$input' `tinput count: $($input.count) `tvalue of a: '$a'" }
    PROCESS { "input value: '$input' `tinput count: $($input.count) `tvalue of a: '$a'" }
    END     { "input value: '$input' `tinput count: $($input.count) `tvalue of a: '$a'" }
}

when run, the output is as follows:

PS> 1..3 | f1
input value: '' 	input count: 0 	value of a: ''
input value: '1' 	input count: 1 	value of a: '1'
input value: '2' 	input count: 1 	value of a: '2'
input value: '3' 	input count: 1 	value of a: '3'
input value: '' 	input count: 0 	value of a: '3'

As you can see, $input has no value in both the BEGIN and END blocks and a has the last value that $a was set to in the END block.

This rule is expressing the appropriate expectation that if you have pipelined parameters, you need to handle them in a PROCESS block. The rule violation has nothing to do with $input in the END block, only that you have failed to handle the pipeline variable in the appropriate block.

0reactions
brianarycommented, May 10, 2023

@JamesWTruher Yes, you can’t mix $input in multiple blocks, as I said, because it consumes the values.

function f1 {
    param ([parameter(ValueFromPipeline=$true)]$a)
    END     { "input value: '$input' `tinput count: $($input.count) `tvalue of a: '$a'" }
}
input value: '1 2 3'    input count: 3  value of a: '3'
Read more comments on GitHub >

github_iconTop Results From Across the Web

[Repost] PSUseProcessBlockForPipelineCommand does ...
[Repost] PSUseProcessBlockForPipelineCommand does not recognize use of $input within an End block #1915. Open. brianary opened this issue on May ...
Read more >
Advanced PowerShell Functions: Begin to Process to End
Use this block to setup the function by initializing objects such as variables, database connections, or arrays that will be used throughout the ......
Read more >
public/New-UcmOffice365User.ps1 0.1.2-alpha
This function accepts both parameter and pipline input .OUTPUTS ... "Warning" : User already exists, creation was skipped ... SIG # End signature...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found