【发布时间】:2015-02-02 16:11:38
【问题描述】:
我正在运行一个从批处理中调用的 PowerShell 脚本,该批处理将文件名作为第一个参数传递。 在我使用的脚本中:
$file = Get-Item -LiteralPath $args[0]
但当文件名包含引号(即:my'file.txt)时,get-item 会触发错误。我试图删除-LiteralPath 参数,但问题是一样的。
脚本的语法是
$file = Get-Item -LiteralPath $args[0] 写主机 $file cmd /c 暂停
如果我针对 my'file.txt 运行脚本,我会得到:
- C:\m\tag\testscript.ps1 'C:\m\tag\my'file.txt'
- CategoryInfo : ParserError: (:String) [], ParentContainsErrorRecordException
- FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
【问题讨论】:
-
了解错误是什么很有用。如果在调用
Get-Item之前删除了该引号,那么您将收到FileNotFound类型异常,这不是 PowerShell 的错误,但可能是正在消耗该字符的批处理。 -
Thnaks Matt:我收到一条错误消息,提示 TerminatorExpectedAtEndOfString。我正在就我的问题提供更多细节
标签: powershell