【问题标题】:Powershell Get-Command not working in scriptPowershell Get-Command 在脚本中不起作用
【发布时间】: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


【解决方案1】:

如果您查看脚本第 2 行 (try {),会有很多空格,然后以该字符结尾:─╯(请注意问题中代码 sn-p 中的水平滚动条) . IE。 Powershell 只是抱怨脚本中出现意外字符。删除它们,它会起作用。

【讨论】:

  • 非常感谢!不知道我是怎么忽略的。
猜你喜欢
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 2011-04-11
相关资源
最近更新 更多