Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3014 - Clarify delay bind script blocks #4285

Merged
merged 2 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,6 @@ Param(
)
```

> [!NOTE]
> A parameter that accepts pipeline input (`by Value`) will enable use of
> **delay-bind** script blocks on all other parameters defined to accept
> pipeline input. The **delay-bind** script block is run automatically during
> ParameterBinding. The result is bound to the parameter. Delay binding
> does **not** work for parameters defined as type `System.Object`, the
> script block is passed through **without** being invoked.
>
> You can read about **delay-bind** script blocks here [about_Script_Blocks.md](about_Script_Blocks.md)

### ValueFromPipelineByPropertyName Argument

The `ValueFromPipelineByPropertyName` argument indicates that the parameter
Expand All @@ -283,6 +273,18 @@ Param(
)
```

> [!NOTE]
> A typed parameter that accepts pipeline input (`by Value`) or
> (`by PropertyName`) enables use of **delay-bind** script blocks on the parameter.
>
> The **delay-bind** script block is run automatically during
> **ParameterBinding**. The result is bound to the parameter. Delay binding
> does **not** work for parameters defined as type `ScriptBlock` or
> `System.Object`, the script block is passed through
> **without** being invoked.
>
> You can read about **delay-bind** script blocks here [about_Script_Blocks.md](about_Script_Blocks.md)

### ValueFromRemainingArguments Argument

The `ValueFromRemainingArguments` argument indicates that the parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,16 @@ For example, you can pipe a value to a **Name** parameter only when the value
has a property called **Name**.

> [!NOTE]
> A parameter that accepts pipeline input (`by Value`) will enable use of
> **delay-bind** script blocks on all other parameters defined to accept
> pipeline input. The **delay-bind** script block is run automatically
> during ParameterBinding. The result is bound to the parameter. Delay
> binding does **not** work for parameters defined as type `System.Object`,
> the script block is passed through **without** being invoked.
> A typed parameter that accepts pipeline input (`by Value`) or
> (`by PropertyName`) enables use of **delay-bind** script blocks on the parameter.
>
> You can read about **delay-bind** script blocks in [about_Script_Blocks](about_Script_Blocks.md).
> The **delay-bind** script block is run automatically during
> **ParameterBinding**. The result is bound to the parameter. Delay binding
> does **not** work for parameters defined as type `ScriptBlock` or
> `System.Object`, the script block is passed through
> **without** being invoked.
>
> You can read about **delay-bind** script blocks here [about_Script_Blocks.md](about_Script_Blocks.md)

#### Accepts Wildcard Characters

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,49 @@ For more information about the call operator, see [about_Operators](about_Operat

## Using delay-bind script blocks with parameters

A **delay-bind** script block allows you to pipe input to a given parameter,
then use script blocks for other parameters using the pipeline variable `$_` to
reference the same object.
A typed parameter that accepts pipeline input (`by Value`) or
(`by PropertyName`) enables use of **delay-bind** script blocks on the parameter.
Within the **delay-bind** script block, you can reference the piped in object
using the pipeline variable `$_`.

```powershell
# Renames config.log to old_config.log
dir config.log | Rename-Item -NewName {"old_" + $_.Name}
```

The additional parameters must accept pipeline input
(`by Value`) or (`by PropertyName`).

In more complex cmdlets, delay-bind script blocks allow the reuse of one piped
in object to populate other parameters.

Notes on **delay-bind** script blocks as parameters:

- You must explicitly specify any parameter names you use with **delay-bind**
script blocks.
- The parameter must not be untyped, and the parameter's type cannot be
`[scriptblock]` or `[object]`.
- You receive an error if you use a **delay-bind** script block without
providing pipeline input.

```powershell
Rename-Item -NewName {$_.Name + ".old"}
```

```Output
Rename-Item : Cannot evaluate parameter 'NewName' because its argument is
specified as a script block and there is no input. A script block cannot
be evaluated without input.
At line:1 char:23
+ Rename-Item -NewName {$_.Name + ".old"}
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [Rename-Item],
ParameterBindingException
+ FullyQualifiedErrorId : ScriptBlockArgumentNoInput,
Microsoft.PowerShell.Commands.RenameItemCommand
```

## See Also

[about_Functions](about_Functions.md)

[about_Functions_Advanced](about_Functions_Advanced.md)

[about_Operators](about_Operators.md)
[about_Operators](about_Operators.md)
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,6 @@ Param(
)
```

> [!NOTE]
> A parameter that accepts pipeline input (`by Value`) will enable use of
> **delay-bind** script blocks on all other parameters defined to accept
> pipeline input. The **delay-bind** script block is run automatically during
> ParameterBinding. The result is bound to the parameter. Delay binding
> does **not** work for parameters defined as type `System.Object`, the
> script block is passed through **without** being invoked.
>
> You can read about **delay-bind** script blocks here [about_Script_Blocks.md](about_Script_Blocks.md)

### ValueFromPipelineByPropertyName Argument

The `ValueFromPipelineByPropertyName` argument indicates that the parameter
Expand All @@ -283,6 +273,18 @@ Param(
)
```

> [!NOTE]
> A typed parameter that accepts pipeline input (`by Value`) or
> (`by PropertyName`) enables use of **delay-bind** script blocks on the parameter.
>
> The **delay-bind** script block is run automatically during
> **ParameterBinding**. The result is bound to the parameter. Delay binding
> does **not** work for parameters defined as type `ScriptBlock` or
> `System.Object`, the script block is passed through
> **without** being invoked.
>
> You can read about **delay-bind** script blocks here [about_Script_Blocks.md](about_Script_Blocks.md)

### ValueFromRemainingArguments Argument

The `ValueFromRemainingArguments` argument indicates that the parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,16 @@ For example, you can pipe a value to a **Name** parameter only when the value
has a property called **Name**.

> [!NOTE]
> A parameter that accepts pipeline input (`by Value`) will enable use of
> **delay-bind** script blocks on all other parameters defined to accept
> pipeline input. The **delay-bind** script block is run automatically
> during ParameterBinding. The result is bound to the parameter. Delay
> binding does **not** work for parameters defined as type `System.Object`,
> the script block is passed through **without** being invoked.
> A typed parameter that accepts pipeline input (`by Value`) or
> (`by PropertyName`) enables use of **delay-bind** script blocks on the parameter.
>
> You can read about **delay-bind** script blocks in [about_Script_Blocks](about_Script_Blocks.md).
> The **delay-bind** script block is run automatically during
> **ParameterBinding**. The result is bound to the parameter. Delay binding
> does **not** work for parameters defined as type `ScriptBlock` or
> `System.Object`, the script block is passed through
> **without** being invoked.
>
> You can read about **delay-bind** script blocks here [about_Script_Blocks.md](about_Script_Blocks.md)

#### Accepts Wildcard Characters

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ title: about_Script_Blocks
# About Script Blocks

## Short description

Defines what a script block is and explains how to use script blocks in
the PowerShell programming language.

Expand Down Expand Up @@ -138,25 +137,49 @@ For more information about the call operator, see [about_Operators](about_Operat

## Using delay-bind script blocks with parameters

A **delay-bind** script block allows you to pipe input to a given parameter,
then use script blocks for other parameters using the pipeline variable `$_` to
reference the same object.
A typed parameter that accepts pipeline input (`by Value`) or
(`by PropertyName`) enables use of **delay-bind** script blocks on the parameter.
Within the **delay-bind** script block, you can reference the piped in object
using the pipeline variable `$_`.

```powershell
# Renames config.log to old_config.log
dir config.log | Rename-Item -NewName {"old_" + $_.Name}
```

The additional parameters must accept pipeline input
(`by Value`) or (`by PropertyName`).

In more complex cmdlets, delay-bind script blocks allow the reuse of one piped
in object to populate other parameters.

Notes on **delay-bind** script blocks as parameters:

- You must explicitly specify any parameter names you use with **delay-bind**
script blocks.
- The parameter must not be untyped, and the parameter's type cannot be
`[scriptblock]` or `[object]`.
- You receive an error if you use a **delay-bind** script block without
providing pipeline input.

```powershell
Rename-Item -NewName {$_.Name + ".old"}
```

```Output
Rename-Item : Cannot evaluate parameter 'NewName' because its argument is
specified as a script block and there is no input. A script block cannot
be evaluated without input.
At line:1 char:23
+ Rename-Item -NewName {$_.Name + ".old"}
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [Rename-Item],
ParameterBindingException
+ FullyQualifiedErrorId : ScriptBlockArgumentNoInput,
Microsoft.PowerShell.Commands.RenameItemCommand
```

## See Also

[about_Functions](about_Functions.md)

[about_Functions_Advanced](about_Functions_Advanced.md)

[about_Operators](about_Operators.md)
[about_Operators](about_Operators.md)
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,6 @@ Param(
)
```

> [!NOTE]
> A parameter that accepts pipeline input (`by Value`) will enable use of
> **delay-bind** script blocks on all other parameters defined to accept
> pipeline input. The **delay-bind** script block is run automatically during
> ParameterBinding. The result is bound to the parameter. Delay binding
> does **not** work for parameters defined as type `System.Object`, the
> script block is passed through **without** being invoked.
>
> You can read about **delay-bind** script blocks here [about_Script_Blocks.md](about_Script_Blocks.md)

### ValueFromPipelineByPropertyName Argument

The `ValueFromPipelineByPropertyName` argument indicates that the parameter
Expand All @@ -282,6 +272,18 @@ Param(
)
```

> [!NOTE]
> A typed parameter that accepts pipeline input (`by Value`) or
> (`by PropertyName`) enables use of **delay-bind** script blocks on the parameter.
>
> The **delay-bind** script block is run automatically during
> **ParameterBinding**. The result is bound to the parameter. Delay binding
> does **not** work for parameters defined as type `ScriptBlock` or
> `System.Object`, the script block is passed through
> **without** being invoked.
>
> You can read about **delay-bind** script blocks here [about_Script_Blocks.md](about_Script_Blocks.md)

### ValueFromRemainingArguments Argument

The `ValueFromRemainingArguments` argument indicates that the parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,16 @@ For example, you can pipe a value to a **Name** parameter only when the value
has a property called **Name**.

> [!NOTE]
> A parameter that accepts pipeline input (`by Value`) will enable use of
> **delay-bind** script blocks on all other parameters defined to accept
> pipeline input. The **delay-bind** script block is run automatically
> during ParameterBinding. The result is bound to the parameter. Delay
> binding does **not** work for parameters defined as type `System.Object`,
> the script block is passed through **without** being invoked.
> A typed parameter that accepts pipeline input (`by Value`) or
> (`by PropertyName`) enables use of **delay-bind** script blocks on the parameter.
>
> You can read about **delay-bind** script blocks in [about_Script_Blocks](about_Script_Blocks.md).
> The **delay-bind** script block is run automatically during
> **ParameterBinding**. The result is bound to the parameter. Delay binding
> does **not** work for parameters defined as type `ScriptBlock` or
> `System.Object`, the script block is passed through
> **without** being invoked.
>
> You can read about **delay-bind** script blocks here [about_Script_Blocks.md](about_Script_Blocks.md)

#### Accepts Wildcard Characters

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,45 @@ For more information about the call operator, see [about_Operators](about_Operat

## Using delay-bind script blocks with parameters

A **delay-bind** script block allows you to pipe input to a given parameter,
then use script blocks for other parameters using the pipeline variable `$_` to
reference the same object.
A typed parameter that accepts pipeline input (`by Value`) or
(`by PropertyName`) enables use of **delay-bind** script blocks on the parameter.
Within the **delay-bind** script block, you can reference the piped in object
using the pipeline variable `$_`.

```powershell
# Renames config.log to old_config.log
dir config.log | Rename-Item -NewName {"old_" + $_.Name}
```

The additional parameters must accept pipeline input
(`by Value`) or (`by PropertyName`).

In more complex cmdlets, delay-bind script blocks allow the reuse of one piped
in object to populate other parameters.

Notes on **delay-bind** script blocks as parameters:

- You must explicitly specify any parameter names you use with **delay-bind**
script blocks.
- The parameter must not be untyped, and the parameter's type cannot be
`[scriptblock]` or `[object]`.
- You receive an error if you use a **delay-bind** script block without
providing pipeline input.

```powershell
Rename-Item -NewName {$_.Name + ".old"}
```

```Output
Rename-Item : Cannot evaluate parameter 'NewName' because its argument is
specified as a script block and there is no input. A script block cannot
be evaluated without input.
At line:1 char:23
+ Rename-Item -NewName {$_.Name + ".old"}
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [Rename-Item],
ParameterBindingException
+ FullyQualifiedErrorId : ScriptBlockArgumentNoInput,
Microsoft.PowerShell.Commands.RenameItemCommand
```

## See Also

[about_Functions](about_Functions.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,6 @@ Param(
)
```

> [!NOTE]
> A parameter that accepts pipeline input (`by Value`) will enable use of
> **delay-bind** script blocks on all other parameters defined to accept
> pipeline input. The **delay-bind** script block is run automatically during
> ParameterBinding. The result is bound to the parameter. Delay binding
> does **not** work for parameters defined as type `System.Object`, the
> script block is passed through **without** being invoked.
>
> You can read about **delay-bind** script blocks here [about_Script_Blocks.md](about_Script_Blocks.md)

### ValueFromPipelineByPropertyName Argument

The `ValueFromPipelineByPropertyName` argument indicates that the parameter
Expand All @@ -283,6 +273,18 @@ Param(
)
```

> [!NOTE]
> A typed parameter that accepts pipeline input (`by Value`) or
> (`by PropertyName`) enables use of **delay-bind** script blocks on the parameter.
>
> The **delay-bind** script block is run automatically during
> **ParameterBinding**. The result is bound to the parameter. Delay binding
> does **not** work for parameters defined as type `ScriptBlock` or
> `System.Object`, the script block is passed through
> **without** being invoked.
>
> You can read about **delay-bind** script blocks here [about_Script_Blocks.md](about_Script_Blocks.md)

### ValueFromRemainingArguments Argument

The `ValueFromRemainingArguments` argument indicates that the parameter
Expand Down
Loading