gpt4 book ai didi

powershell - PowerShell null不会被传递

转载 作者:行者123 更新时间:2023-12-02 23:45:05 26 4
gpt4 key购买 nike

因此,您学会使用PowerShell避免的第一个陷阱是该foreach:

$null | foreach { "hello" }

将执行一次。但是,我试图向同事解释这个概念,并且发生了以下事情:
PS> $a = 1..9 | ? {$_ -eq 10}
PS> $a | % { "hello" }
PS>

我试图理解以下内容,但 $a似乎是 $null,简单明了。
PS> $a = 1..9 | ? {$_ -eq 10}
PS> $a -eq $null
True
PS> $a.gettype()
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $a.gettype()
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

PS> $a | gm
gm : No object has been specified to the get-member cmdlet.
At line:1 char:6
+ $a | gm
+ ~~
+ CategoryInfo : CloseError: (:) [Get-Member], InvalidOperationException
+ FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand

PS> $a | % { "hello" }
PS> $null | % { "hello" }
hello
PS>

是否有两种不同的null类型? $a真的是由于某种原因而被我混淆的数组吗?

最佳答案

关于此的一些历史-

给定PS > $a = 1..9 | ? {$_ -eq 10}
Powershell 2.0行为

PS > $a | % { "hello" }
hello
PS > $null | % { "hello" }
hello

Powershell 3.0行为-变量为null,但内置$ null仍会迭代时,已“解决”该问题
PS > $a | % { "hello" }
PS > $null | % { "hello" }
hello

在3.0版的 ForEach-Object documentation中,他们添加了以下内容-

Because Windows PowerShell treats null as an explicit placeholder, the ForEach-Object cmdlet generates a value for $null, just as it does for other objects that you pipe to it.


PS C:\> 1, 2, $null, 4 | ForEach-Object {"Hello"} 
Hello
Hello
Hello
Hello

$ null的about_automatic_variables文档也已更新为3.0。

Powershell 2.0 $ null文档
$NULL
Contains a NULL or empty value. You can use this variable to
represent NULL in commands and scripts instead of using the string
"NULL". The string can be interpreted as TRUE if it is converted to a
non-empty string or a non-zero integer.

Powershell 3.0 $ null文档
$NULL
$null is an automatic variable that contains a NULL or empty value. You
can use this variable to represent an absent or undefined value in commands
and scripts.

Windows PowerShell treats $null as an object with a value, that is, as an
explicit placeholder, so you can use $null to represent an empty value in a
series of values.

For example, when $null is included in a collection, it is counted as one of
the objects.

C:\PS> $a = ".dir", $null, ".pdf"
C:\PS> $a.count
3

If you pipe the $null variable to the ForEach-Object cmdlet, it generates a
value for $null, just as it does for the other objects.

PS C:\ps-test> ".dir", "$null, ".pdf" | Foreach {"Hello"}
Hello
Hello
Hello

As a result, you cannot use $null to mean "no parameter value." A parameter
value of $null overrides the default parameter value.

However, because Windows PowerShell treats the $null variable as a placeholder,
you can use it scripts like the following one, which would not work if $null
were ignored.

$calendar = @($null, $null, “Meeting”, $null, $null, “Team Lunch”, $null)
$days = Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"
$currentDay = 0

foreach($day in $calendar)
{
if($day –ne $null)
{
"Appointment on $($days[$currentDay]): $day"
}

$currentDay++
}

Appointment on Tuesday: Meeting
Appointment on Friday: Team lunch

注意-他们还使 foreach关键字的行为与3.0中的ForEach-Object相同(跳过$ null值变量,但将$ null内置变量转换为可迭代的值),但是他们没有在文档中提及新行为,例如他们使用ForEach-Obect。

关于powershell - PowerShell null不会被传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25657560/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com