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

1/31/2024 PM Publish #10845

Merged
merged 1 commit into from
Jan 31, 2024
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
@@ -1,7 +1,7 @@
---
description: Describes how to create and use functions in PowerShell.
Locale: en-US
ms.date: 01/20/2023
ms.date: 01/31/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7.4&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about Functions
Expand Down Expand Up @@ -87,7 +87,7 @@ A function includes the following items:
- Any number of named parameters (optional)
- One or more PowerShell commands enclosed in braces `{}`

For more information about the `Dynamicparam` keyword and dynamic parameters in
For more information about the `dynamicparam` keyword and dynamic parameters in
functions, see [about_Functions_Advanced_Parameters][10].

## Simple functions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Explains how to add parameters to advanced functions.
Locale: en-US
ms.date: 06/22/2023
ms.date: 01/31/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.4&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about Functions Advanced Parameters
Expand Down Expand Up @@ -82,7 +82,7 @@ function Get-Date_Func {
}
}

[cultureinfo]::CurrentCulture = 'de-DE'
[CultureInfo]::CurrentCulture = 'de-DE'

# This German-format date string doesn't work with the invariant culture.
# E.g., [datetime] '19-06-2018' breaks.
Expand Down Expand Up @@ -200,7 +200,7 @@ they can be difficult for users to discover. To find a dynamic parameter, the
user must be in the provider path, use the **ArgumentList** parameter of the
`Get-Command` cmdlet, or use the **Path** parameter of `Get-Help`.

To create a dynamic parameter for a function or script, use the `DynamicParam`
To create a dynamic parameter for a function or script, use the `dynamicparam`
keyword.

The syntax is as follows:
Expand All @@ -222,7 +222,7 @@ function Get-Sample {
[CmdletBinding()]
param([string]$Name, [string]$Path)

DynamicParam
dynamicparam
{
if ($Path.StartsWith("HKLM:"))
{
Expand All @@ -246,7 +246,8 @@ function Get-Sample {
}
```

For more information, see the documentation for the [RuntimeDefinedParameter][02] type.
For more information, see the documentation for the
[RuntimeDefinedParameter][02] type.

## Attributes of parameters

Expand Down Expand Up @@ -739,7 +740,7 @@ For more information, see [about_Functions_Argument_Completion][11].

The **ArgumentCompleter** attribute allows you to add tab completion values to
a specific parameter. An **ArgumentCompleter** attribute must be defined for
each parameter that needs tab completion. Like **DynamicParameters**, the
each parameter that needs tab completion. Like **dynamicparameters**, the
available values are calculated at runtime when the user presses <kbd>Tab</kbd>
after the parameter name.

Expand Down Expand Up @@ -917,10 +918,10 @@ PowerShell generates an error if any value is outside that range.

The **ValidateRangeKind** enum allows for the following values:

- **Positive** - A number greater than zero.
- **Negative** - A number less than zero.
- **NonPositive** - A number less than or equal to zero.
- **NonNegative** - A number greater than or equal to zero.
- `Positive` - A number greater than zero.
- `Negative` - A number less than zero.
- `NonPositive` - A number less than or equal to zero.
- `NonNegative` - A number greater than or equal to zero.

In the following example, the value of the **Attempts** parameter must be
between zero and ten.
Expand Down Expand Up @@ -1273,7 +1274,7 @@ intended for external usage.
- [about_Functions_OutputTypeAttribute][13]

<!-- link references -->
[01]: ./about_comment_based_help.md
[01]: about_comment_based_help.md
[02]: /dotnet/api/system.management.automation.runtimedefinedparameter
[03]: /dotnet/standard/base-types/composite-formatting#composite-format-string
[04]: /dotnet/standard/base-types/composite-formatting#format-string-component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Defines what a script block is and explains how to use script blocks in the PowerShell programming language.
Locale: en-US
ms.date: 11/28/2023
ms.date: 01/31/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_script_blocks?view=powershell-7.4&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about Script Blocks
Expand Down Expand Up @@ -50,8 +50,8 @@ keyword to assign named parameters, as shown in the following syntax:
> In a script block, unlike a function, you can't specify parameters outside
> the braces.

Like functions, script blocks can include the `DynamicParam`, `Begin`,
`Process`, and `End` keywords. For more information, see [about_Functions][02]
Like functions, script blocks can include the `dynamicparam`, `begin`,
`process`, and `end` keywords. For more information, see [about_Functions][02]
and [about_Functions_Advanced][01].

## Using Script Blocks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ To support dynamic parameters, the following elements must be included in the cm

### Interface

[System.Management.Automation.IDynamicParameters](/dotnet/api/System.Management.Automation.IDynamicParameters).
This interface provides the method that retrieves the dynamic parameters.
[System.Management.Automation.IDynamicParameters][03]. This interface provides the method that
retrieves the dynamic parameters.

For example:

`public class SendGreetingCommand : Cmdlet, IDynamicParameters`

### Method

[System.Management.Automation.IDynamicParameters.GetDynamicParameters](/dotnet/api/System.Management.Automation.IDynamicParameters.GetDynamicParameters).
This method retrieves the object that contains the dynamic parameter definitions.
[System.Management.Automation.IDynamicParameters.GetDynamicParameters][04]. This method retrieves
the object that contains the dynamic parameter definitions.

For example:

Expand Down Expand Up @@ -107,14 +107,18 @@ public class SendGreetingCommandDynamicParameters
}
```

For a complete example of a cmdlet that supports dynamic parameters, see [How to Declare Dynamic Parameters](./how-to-declare-dynamic-parameters.md).
For a complete example of a cmdlet that supports dynamic parameters, see
[How to Declare Dynamic Parameters][01].

## See also

[System.Management.Automation.IDynamicParameters](/dotnet/api/System.Management.Automation.IDynamicParameters)
- [System.Management.Automation.IDynamicParameters][03]
- [System.Management.Automation.IDynamicParameters.GetDynamicParameters][04]
- [How to Declare Dynamic Parameters][01]
- [Writing a Windows PowerShell Cmdlet][02]

[System.Management.Automation.IDynamicParameters.GetDynamicParameters](/dotnet/api/System.Management.Automation.IDynamicParameters.GetDynamicParameters)

[How to Declare Dynamic Parameters](./how-to-declare-dynamic-parameters.md)

[Writing a Windows PowerShell Cmdlet](./writing-a-windows-powershell-cmdlet.md)
<!-- link references -->
[01]: ./how-to-declare-dynamic-parameters.md
[02]: ./writing-a-windows-powershell-cmdlet.md
[03]: /dotnet/api/System.Management.Automation.IDynamicParameters
[04]: /dotnet/api/System.Management.Automation.IDynamicParameters.GetDynamicParameters
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ title: How to Declare Dynamic Parameters
---
# How to Declare Dynamic Parameters

This example shows how to define dynamic parameters that are added to the cmdlet at runtime. In this example, the `Department` parameter is added to the cmdlet whenever the user specifies the `Employee` switch parameter. For more information about dynamic parameters, see [Cmdlet Dynamic Parameters](./cmdlet-dynamic-parameters.md).
This example shows how to define dynamic parameters that are added to the cmdlet at runtime. In this
example, the `Department` parameter is added to the cmdlet whenever the user specifies the
`Employee` switch parameter. For more information about dynamic parameters, see
[Cmdlet Dynamic Parameters][02].

## To define dynamic parameters

1. In the cmdlet class declaration, add the [System.Management.Automation.Idynamicparameters](/dotnet/api/System.Management.Automation.IDynamicParameters) interface as shown.
1. In the cmdlet class declaration, add the [System.Management.Automation.Idynamicparameters][03]
interface as shown.

```csharp
public class SendGreetingCommand : Cmdlet, IDynamicParameters
```

2. Call the [System.Management.Automation.Idynamicparameters.Getdynamicparameters*](/dotnet/api/System.Management.Automation.IDynamicParameters.GetDynamicParameters) method, which returns the object in which the dynamic parameters are defined. In this example, the method is called when the `Employee` parameter is specified.
1. Call the [System.Management.Automation.Idynamicparameters.Getdynamicparameters*][04] method,
which returns the object in which the dynamic parameters are defined. In this example, the method
is called when the `Employee` parameter is specified.

```csharp
public object GetDynamicParameters()
Expand All @@ -31,7 +37,8 @@ This example shows how to define dynamic parameters that are added to the cmdlet
private SendGreetingCommandDynamicParameters context;
```

3. Declare a class that defines the dynamic parameters to be added. You can use the attributes that you used to declare the static cmdlet parameters to declare the dynamic parameters.
1. Declare a class that defines the dynamic parameters to be added. You can use the attributes that
you used to declare the static cmdlet parameters to declare the dynamic parameters.

```csharp
public class SendGreetingCommandDynamicParameters
Expand All @@ -49,7 +56,9 @@ This example shows how to define dynamic parameters that are added to the cmdlet

## Example

In this example, the `Department` parameter is added whenever the user specifies the `Employee` parameter. The `Department` parameter is an optional parameter, and the ValidateSet attribute is used to specify the allowed arguments.
In this example, the `Department` parameter is added whenever the user specifies the `Employee`
parameter. The `Department` parameter is an optional parameter, and the ValidateSet attribute is
used to specify the allowed arguments.

```csharp
using System;
Expand Down Expand Up @@ -126,10 +135,14 @@ namespace SendGreeting

## See Also

[System.Management.Automation.Runtimedefinedparameterdictionary](/dotnet/api/System.Management.Automation.RuntimeDefinedParameterDictionary)

[System.Management.Automation.Idynamicparameters.Getdynamicparameters*](/dotnet/api/System.Management.Automation.IDynamicParameters.GetDynamicParameters)

[Cmdlet Dynamic Parameters](./cmdlet-dynamic-parameters.md)

[Windows PowerShell SDK](../windows-powershell-reference.md)
- [System.Management.Automation.RuntimeDefinedParameterDictionary][05]
- [System.Management.Automation.IDynamicParameters.GetDynamicParameters*][04]
- [Cmdlet Dynamic Parameters][02]
- [Windows PowerShell SDK][01]

<!-- link references -->
[01]: ../windows-powershell-reference.md
[02]: cmdlet-dynamic-parameters.md
[03]: xref:System.Management.Automation.IDynamicParameters
[04]: xref:System.Management.Automation.IDynamicParameters.GetDynamicParameters
[05]: xref:System.Management.Automation.RuntimeDefinedParameterDictionary
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,5 @@ The following example shows the `DynamicParameters` element of the `Encoding` dy
```

<!-- link references -->
[01]: ./how-to-add-parameter-information.md
[02]: /dotnet/api/microsoft.powershell.commands.filesystemcmdletproviderencoding
[01]: how-to-add-parameter-information.md
[02]: xref:Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding
Loading
Loading