forked from MicrosoftDocs/PowerShell-Docs
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…#8096) * Add docs for ValidateScript * Fix title * update TOC Co-authored-by: Chase Wilson <31453523+chasewilson@users.noreply.github.com>
- Loading branch information
1 parent
1eb4764
commit 0e2f82f
Showing
6 changed files
with
155 additions
and
46 deletions.
There are no files selected for viewing
61 changes: 35 additions & 26 deletions
61
reference/docs-conceptual/developer/cmdlet/cmdlet-attributes.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...ce/docs-conceptual/developer/cmdlet/how-to-validate-an-argument-using-script.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
ms.date: 09/16/2021 | ||
ms.topic: reference | ||
title: How to validate an argument using a script | ||
description: How to validate an argument using a script | ||
--- | ||
# How to validate an argument using a script | ||
|
||
This example shows how to specify a validation rule that uses a script to check the parameter | ||
argument before the cmdlet is run. The value of the parameter is piped to the script. The script | ||
must return `$true` for every value piped to it. | ||
|
||
> [!NOTE] | ||
> For more information about the class that defines this attribute, see | ||
> [System.Management.Automation.ValidateScriptAttribute](/dotnet/api/System.Management.Automation.ValidateScriptAttribute). | ||
## To validate an argument using a script | ||
|
||
- Add the ValidateScript attribute as shown in the following code. This example specifies a set of | ||
three possible values for the `UserName` parameter. | ||
|
||
```csharp | ||
[ValidateScript("$_ % 2", ErrorMessage = "The item '{0}' did not pass validation of script '{1}'")] | ||
[Parameter(Position = 0, Mandatory = true)] | ||
public int32 OddNumber | ||
{ | ||
get { return oddNumber; } | ||
set { oddNumber = value; } | ||
} | ||
|
||
private int32 oddNumber; | ||
``` | ||
|
||
For more information about how to declare this attribute, see | ||
[ValidateScript Attribute Declaration](./ValidateScript-attribute-declaration.md). | ||
|
||
## See Also | ||
|
||
[System.Management.Automation.ValidateScriptAttribute](/dotnet/api/System.Management.Automation.ValidateScriptAttribute) | ||
|
||
[ValidateScript Attribute Declaration](./ValidateScript-attribute-declaration.md) | ||
|
||
[Writing a Windows PowerShell Cmdlet](./writing-a-windows-powershell-cmdlet.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
reference/docs-conceptual/developer/cmdlet/validatescript-attribute-declaration.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
ms.date: 09/16/2021 | ||
ms.topic: reference | ||
title: ValidateScript Attribute Declaration | ||
description: ValidateScript Attribute Declaration | ||
--- | ||
# ValidateScript Attribute Declaration | ||
|
||
The `ValidateScript` attribute specifies a script that is used to validate a parameter or variable | ||
value. PowerShell pipes the value to the script, and generates an error if the script returns | ||
`$false` or if the script throws an exception. | ||
|
||
When you use the `ValidateScript` attribute, the value that's being validated is mapped to the `$_` | ||
variable. You can use the `$_` variable to refer to the value in the script. | ||
|
||
## Syntax | ||
|
||
```csharp | ||
[ValidateScriptAttribute(ScriptBlock scriptBlock)] | ||
``` | ||
|
||
### Parameters | ||
|
||
- `scriptBlock` - | ||
([System.Management.Automation.ScriptBlock](/dotnet/api/System.Management.Automation.ScriptBlock)) | ||
Required. The script block used to validate the input. | ||
- `ErrorMessage` - Optional - The item being validated and the validating scriptblock are passed as | ||
the first and second formatting arguments. | ||
|
||
## Remarks | ||
|
||
- This attribute can be used only once per parameter. | ||
- If this attribute is applied to a collection, each element in the collection must match the | ||
pattern. | ||
- The ValidateScript attribute is defined by the | ||
[System.Management.Automation.ValidateScriptAttribute](/dotnet/api/System.Management.Automation.ValidateScriptAttribute) | ||
class. | ||
|
||
## See Also | ||
|
||
[System.Management.Automation.ValidateScriptAttribute](/dotnet/api/System.Management.Automation.ValidateScriptAttribute) | ||
|
||
[Writing a Windows PowerShell Cmdlet](./writing-a-windows-powershell-cmdlet.md) |
42 changes: 24 additions & 18 deletions
42
reference/docs-conceptual/developer/cmdlet/validating-parameter-input.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters