有人可以解释为什么这个脚本会引发异常吗?
$byteArray = @(1,2,3)
write-Output ( "{0:X}{1:X}{2:X}" -f $byteArray )
write-Output ( $byteArray.Length -ge 3 )
write-Output ( "{0:X}{1:X}{2:X}" -f $byteArray )
Run Code Online (Sandbox Code Playgroud)
基本上,我正在创建一个数字数组,格式化数组,然后检查其长度并再次格式化它.
第一种格式成功,但第二种格式抛出异常.
123
True
--------------------------------------------------------------------------------
POWERSHELL EXCEPTION
EXCEPTION TYPE:System.Management.Automation.RuntimeException
MESSAGE:Error formatting a string: Index (zero based) must be greater than or equal to zero and less than the size of the argument list..
POSITION:
At line:4 char:36
+ write-Output ( "{0:X}{1:X}{2:X}" -f <<<< $byteArray )
--------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
这绝对很奇怪。作为解决方法,您可以使用
"{0:X}{1:X}{2:X}" -f @($byteArray)
Run Code Online (Sandbox Code Playgroud)
即使在访问 . 的成员之后,这似乎仍然有效$byteArray。
另一种可能的解决方法是将格式化字符串保存到变量中并重新使用它。
至于为什么访问该属性后它不起作用Length我不知道。
| 归档时间: |
|
| 查看次数: |
3686 次 |
| 最近记录: |