这是令人难以置信的事情。这是test.ps1
文件中的 PowerShell 代码片段:
Set-StrictMode -Version 2
mkdir c:\tmp\1 # same with 'md c:\tmp\1'
开始cmd.exe
,使用脚本导航到文件夹test.ps1
并运行它:
c:\tmp>powershell ".\test.ps1"
这会产生以下错误:
The variable '$_' cannot be retrieved because it has not been set.
At line:50 char:38
+ $steppablePipeline.Process($_ <<<< )
+ CategoryInfo : InvalidOperation: (_:Token) [], ParentContainsEr
rorRecordException
+ FullyQualifiedErrorId : VariableIsUndefined
为什么?
它在从 PowerShell 控制台启动但不是 cmd.exe 时有效。我在更大的脚本中发现了这个错误。这是一个WTF的时刻。
这个简单的脚本有什么问题?