【发布时间】:2021-08-31 02:06:17
【问题描述】:
我正在使用 powershell v7.1.4。
如果我在提示符下运行 Get-Command,那么 powershell 会发现该命令没有问题 -
如果我将它放在脚本中,Get-Command 将不起作用。
try {
Get-Command pandoc -ErrorAction Stop
} catch {
Write-Error "Could not find pandoc, cannot generate documentation."
Exit 1
}
运行此脚本会生成 -
写入错误:找不到 pandoc,无法生成文档。
那么,为什么 get-command 在脚本上下文中不能正常工作?
编辑 1
回答一些 cmets 的更新。首先是完整的脚本,然后是输出,包括评论中推荐的更新($_ | Format-List -Force)
function VerifyPandocExists {
try { ─╯
Get-Command pandoc -ErrorAction Stop
} catch {
Write-Error "Could not find pandoc"
$_ | Format-List -Force
Exit 1
}
}
pandoc --version
VerifyPandocExists
脚本输出。查看错误输出,我注意到它指出 -
“─╯”一词未被识别为
那个有趣的角色“─╯”看起来像是我的 oh-my-posh 提示中的东西(不确定是否相关......):
pandoc.exe 2.14.2
Compiled with pandoc-types 1.22, texmath 0.12.3.1, skylighting 0.11,
citeproc 0.5, ipynb 0.1.0.1
User data directory: C:\Users\XXX\AppData\Roaming\pandoc
Copyright (C) 2006-2021 John MacFarlane. Web: https://pandoc.org
This is free software; see the source for copying conditions. There is no
warranty, not even for merchantability or fitness for a particular purpose.
Write-Error: C:\Users\XXX\source\repos\ProgrammingStandards\mkdocs.ps1:12
Line |
12 | VerifyPandocExists
| ~~~~~~~~~~~~~~~~~~
| Could not find pandoc.
Exception : System.Management.Automation.CommandNotFoundException: The term '─╯' is not recognized as a
name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext
funcContext, Exception exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame
frame)
at
System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame
frame)
at
System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame
frame)
TargetObject : ─╯
CategoryInfo : ObjectNotFound: (─╯:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at VerifyPandocExists, C:\Users\XXX\source\repos\ProgrammingStandards\mkdocs.ps1: line 2
at <ScriptBlock>, C:\Users\XXX\source\repos\ProgrammingStandards\mkdocs.ps1: line 12
at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}
PSMessageDetails :
【问题讨论】:
-
您确定在您的真实代码中,
Get-Command调用是您的try块中的唯一语句,并且这不是触发@ 的不同 语句987654330@块? -
另外:
pandoc.exe是否在运行Get-Command的 PowerShell 会话的路径中,但不在运行脚本的 PowerShell 会话的路径中? -
@mklement0 是的,脚本只是在 ATM 中包含那段代码。是的 pandoc.exe 在路径中,我可以在脚本中正常运行它。
-
如果您将
$_ | Format-List -Force放在Exit 1语句之前的catch块中,会报告哪些错误细节? -
@mklement0 我更新了 Q 提供了更多详细信息,包括 Format-List 命令。
标签: powershell