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

6/11/2024 PM Publish #11182

Merged
merged 2 commits into from
Jun 11, 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,6 +1,6 @@
---
description: How to validate an argument using a script
ms.date: 09/16/2021
ms.date: 06/11/2024
ms.topic: reference
title: How to validate an argument using a script
---
Expand All @@ -16,8 +16,8 @@ must return `$true` for every value piped to it.

## 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.
- Add the ValidateScript attribute as shown in the following code. This example specifies a script
to validate that the input value is an odd number.

```csharp
[ValidateScript("$_ % 2", ErrorMessage = "The item '{0}' did not pass validation of script '{1}'")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: The PowerShell $null often appears to be simple but it has a lot of nuances. Let's take a close look at $null so you know what happens when you unexpectedly run into a null value.
ms.custom: contributor-KevinMarquette
ms.date: 11/16/2022
ms.date: 06/11/2024
title: Everything you wanted to know about $null
---
# Everything you wanted to know about $null
Expand Down Expand Up @@ -298,12 +298,13 @@ if ( $value.count -eq 1 )
}
```

## Empty null
## Enumerable null

There is one special type of `$null` that acts differently than the others. I am going to call it
the empty `$null` but it's really a [System.Management.Automation.Internal.AutomationNull][System.Management.Automation.Internal.AutomationNull]. This
empty `$null` is the one you get as the result of a function or script block that returns nothing (a
void result).
the enumerable null but it's really a
[System.Management.Automation.Internal.AutomationNull][System.Management.Automation.Internal.AutomationNull].
This enumerable null is the one you get as the result of a function or script block that returns
nothing (a void result).

```powershell
PS> function Get-Nothing {}
Expand All @@ -330,18 +331,19 @@ PS> $containnull.count
```

You can have an array that contains one `$null` value and its `count` is `1`. But if you place
an empty result inside an array then it's not counted as an item. The count is `0`.
an empty array inside an array then it's not counted as an item. The count is `0`.

If you treat the empty `$null` like a collection, then it's empty.
If you treat the enumerable null like a collection, then it's empty.

If you pass in an empty value to a function parameter that isn't strongly typed, PowerShell coerces
the nothing value into a `$null` value by default. This means inside the function, the value will be
treated as `$null` instead of the **System.Management.Automation.Internal.AutomationNull** type.
If you pass in an enumerable null to a function parameter that isn't strongly typed, PowerShell
coerces the enumerable null into a `$null` value by default. This means inside the function, the
value is treated as `$null` instead of the **System.Management.Automation.Internal.AutomationNull**
type.

### Pipeline

The primary place you see the difference is when using the pipeline. You can pipe a `$null`
value but not an empty `$null` value.
value but not an enumerable null value.

```powershell
PS> $null | ForEach-Object{ Write-Output 'NULL Value' }
Expand Down Expand Up @@ -476,9 +478,9 @@ those scenarios earlier in this post.
### No results scenario

It's important to know that different functions and commands handle the no results scenario
differently. Many PowerShell commands return the empty `$null` and an error in the error stream. But
others throw exceptions or give you a status object. It's still up to you to know how the commands
you use deal with the no results and error scenarios.
differently. Many PowerShell commands return the enumerable null and an error in the error stream.
But others throw exceptions or give you a status object. It's still up to you to know how the
commands you use deal with the no results and error scenarios.

## Initializing to $null

Expand Down Expand Up @@ -511,8 +513,8 @@ function Do-Something
}
```

The expectation here is that `Get-Something` returns either a result or an empty `$null`. If there
is an error, we log it. Then we check to make sure we got a valid result before processing it.
The expectation here is that `Get-Something` returns either a result or an enumerable null. If
there's an error, we log it. Then we check to make sure we got a valid result before processing it.

The bug hiding in this code is when `Get-Something` throws an exception and doesn't assign a value
to `$result`. It fails before the assignment so we don't even assign `$null` to the `$result`
Expand Down