我一直在敲着头,这件事……
创建了这个简单的 PowerShell 脚本:
测试.ps1:
Write-Host $args[0]
Write-Host $($args[0])
Write-Host "$($args[0])"
Run Code Online (Sandbox Code Playgroud)
现在,使用以下参数运行脚本两次:
powershell .\test.ps1 0E2C
powershell .\test.ps1 0E2D
Run Code Online (Sandbox Code Playgroud)
第一次运行返回:
0E2C
0E2C
0E2C
Run Code Online (Sandbox Code Playgroud)
第二次运行返回:
0E2D
0
0
Run Code Online (Sandbox Code Playgroud)
这是为什么 ?为什么 powershell 将 0E2D 转换为 0 ?是否考虑到 0E2D 是一个十六进制值并尝试以某种方式转换它?但为什么,为什么它不对值 0E2C 做同样的事情呢?
编辑:谢谢大家。
我通过使用变量解决了这个问题,并强制它为字符串因此,更改我的 TEST.PS1 :
Write-Host $args[0]
Write-Host $($args[0])
Write-Host "$($args[0])"
[string] $myvar = $args[0]
Write-Host "$myvar"
Run Code Online (Sandbox Code Playgroud)
现在,当我跑步时:
powershell .\test.ps1 0E2D
Run Code Online (Sandbox Code Playgroud)
我现在得到:
0E2D
0
0
0E2D
Run Code Online (Sandbox Code Playgroud)