Comments (5)
-
$_
only has a meaningful value with pipeline input, not also when input is provided via arguments. -
Fundamentally, do not rely on
$_
in theprocess
block of advanced script functions / scripts that explicitly declare pipeline-binding parameters - only use the latter.
In your case, this means that your process
block should (a) only act on $Assertions
, and (b) be aware of the fact that $Assertions
, due to its declaration as [hashtable[]]
, is always an array of [hashtable]
instances, even if [hashtable]
instances are provided one by one via the pipeline.
from powershell.
Thanks a lot for enlightening me, @mklement0!
Would you agree that this important fact should be mentioned in the docs at
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_methods#process
Describes how functions that specify the `CmdletBinding` attribute can use the methods and properties that are available to compiled cmdlets.
PS: Yes I'm intentionally using [hashtable[]]
, so I don't need to make this harmonization (if ($Assertions -isnot [array]) $Assertions = @($Assertions)
) in my code.
from powershell.
Glad to hear it helped, @SetTrend , and I agree re the docs; see:
from powershell.
The documentation for the process block says:
The automatic variable
$_
or$PSItem
contains the current object in the pipeline for use in the process block.
Since you are calling the function with a parameter, there is nothing in the pipeline. The function needs to handle the parameter variable. This version of the function works for both pipeline input and parameter input.
function Test-Something {
param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[hashtable[]]$Assertions
)
begin { [hashtable[]]$retValue = @{} }
process {
foreach ($assertion in $Assertions) {
$retValue += $assertion
}
}
end { $retValue }
}
from powershell.
For context, @sdwheeler:
-
This previous comment already explains the issue, and it makes it clear that you shouldn't use
$_
in theprocess
block of advanced functions - use the parameter (variable) declared to be pipeline-binding.- MicrosoftDocs/PowerShell-Docs#10944 (comment) explains why in more detail.
-
@SetTrend had run into the original problem because had relied on the documentation, which confusingly actively suggests using
$_
(which is only appropriate for non-advanced functions).- The fact that the passage you quote from the documentation only mentions the pipeline is technically correct, but doesn't prevent conceptual confusion, as evidenced by the case at hand.
-
He then suggested that the documentation be amended, causing me to create MicrosoftDocs/PowerShell-Docs#10944, which you've recently closed as
resolution-wont-fix
, which is inappropriate in my estimation.
from powershell.
Related Issues (20)
- Invoke-WebRequest fails with โRequest headers must contain only ASCII characters.โ for simple requests HOT 2
- [JEA] Register-PSSessionConfiguration does not "cascade" the -NoServiceRestart parameter to Set-PSSessionConfiguration when automatically calling it.
- Feature: Expose `Set-Location` history stack HOT 11
- Surrogate pairs cause misalignment in Format-Wide HOT 16
- Update behavior for getting wix 3.14 in CI.psm1 HOT 1
- is it defined/documented which newlines are used by Write-Host and Out-File HOT 8
- `Remove-Item` throws "Attempted to divide by zero" on PS 7.5.0-preview.3 HOT 6
- `using namespace` overrides previous `using namespace` uses as interactive HOT 8
- [Windows] No error when using a non-executable file in script line HOT 4
- `Remove-Item` reports incorrect number of removed items in the progress bar HOT 1
- Bug - PowerShell displays an error when loading in Windows Terminal HOT 5
- Show payload being sent when using verbose with Invoke-RestMethod and Invoke-WebRequest HOT 2
- Encode spaces with %20 instead of + when using Invoke-RestMethod and Invoke-WebRequest. HOT 15
- "foreach" alias poorly named, does not reproduce functionality and use cases with ForEach-Object HOT 7
- PowerShell community Power BI dashboard - Colors of Linux and MacOS are the same in graphs HOT 4
- pwsh 7.4.2 no-input unhandled exception -> process exit HOT 4
- CreateOutOfProcessRunspace fails to Open HOT 1
- `exit` does not return ExitCode when using #Requires -Module <ScriptModule> HOT 1
- pwsh Exception not serializable anymore HOT 5
- `Using Script` directive HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from powershell.