gpt4 book ai didi

powershell - 防止胁迫

转载 作者:行者123 更新时间:2023-12-03 00:29:30 26 4
gpt4 key购买 nike

假设:

Function Invoke-Foo {
Param(
[string[]]$Ids
)
Foreach ($Id in $Ids) {
Write-Host $Id
}
}

如果我这样做:
PS> Invoke-Foo -ids '0000','0001'
0000
0001

如果我这样做:
PS> Invoke-Foo -ids 0000,0001
0
1

在第二种情况下,除了使它们成为显式字符串(第一种情况)之外,还有其他方法可以防止强制吗?

最佳答案

不,不幸的是没有。

about_Parsing 帮助文件中:

When processing a command, the Windows PowerShell parser operates
in expression mode or in argument mode:

- In expression mode, character string values must be contained in
quotation marks. Numbers not enclosed in quotation marks are treated
as numerical values (rather than as a series of characters).

- In argument mode, each value is treated as an expandable string
unless it begins with one of the following special characters: dollar
sign ($), at sign (@), single quotation mark ('), double quotation
mark ("), or an opening parenthesis (().


因此,解析器在将任何内容传递给函数之前先评估 0001。我们可以使用 0001方法测试将 ExpandString()视为“可扩展字符串”的效果:
PS C:\> $ExecutionContext.InvokeCommand.ExpandString(0001)
1

关于powershell - 防止胁迫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30786602/

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