【问题标题】:Visual Studio Code $psise equivalentVisual Studio Code $psise 等价物
【发布时间】:2017-10-31 03:23:22
【问题描述】:

我正在寻找一种在 VSCode 中复制 $psISE 功能的方法,至少就在编辑器中发现打开的活动文件的文件名而言。

作为背景:

我正在尝试迁移到使用 VSCode 进行 powershell 编辑而不是 ISE,因为 VSCode 是未来。我们在迁移某些工作流(特别是签名脚本)时遇到问题。在 ISE 中,我创建了一个使用 $psise.currentfile.fullpath 的函数,以便我们可以轻松地签署在 ISE 中打开的文件。我想为 vscode 重新创建它,但我没有找到任何方法来确定打开的文件是什么。我们倾向于以交互方式运行它(即,我们在 ISE 中进行测试,将其放到我们想要在另一个盒子上测试的位置,然后通过键入 Set-CoSAuthenticodeSignature 对其进行签名,这是一个包装 set-authenticodesignature 的自定义函数,但具有默认设置并自动查找文件,如果我们不指定),所以我们不想一直输入路径名,特别是因为我们现在不需要。

谢谢!

【问题讨论】:

    标签: powershell visual-studio-code


    【解决方案1】:

    我认为它还没有实现($psise)。您可以在vscode-powershell VSCode 项目上提出问题。 similar one 已提交。

    至少你可以使用类似的东西 $context = [Microsoft.Powershell.EditorServices.Extensions.EditorContext]$psEditor.GetEditorContext()

    然后你就可以访问当前文件了... $context.CurrentFile.

    有关介绍,另请参阅this video...

    【讨论】:

    • 这正是我所需要的!谢谢!要获取文件的路径,它实际上是$context.CurrentFile.Path。您的解决方案为我提供的信息比我要求的要多,这太棒了!谢谢!
    • 作为单个命令:$psEditor.GetEditorContext().CurrentFile.Path 返回当前文件的完整路径。
    • VSCode PowerShell 的扩展 $psEditor 变量是 ISE 的 $psISE 变量的模拟(名称 $psISE 在 VSCode 中不合适)。跨度>
    【解决方案2】:
    $ScriptPath=''
    if($ScriptPath -eq '' -and $MyInvocation.MyCommand.CommandType -eq 'ExternalScript'){$ScriptPath = (Split-Path -Path $MyInvocation.MyCommand.Source                  -Parent)} # Running from File
    if($ScriptPath -eq '' -and (Test-Path variable:psISE)) {                             $ScriptPath = (Split-Path -Path $psISE.CurrentFile.FullPath                     -Parent)} # Running from ISE
    if($ScriptPath -eq '' -and (Test-Path variable:psEditor)) {                          $ScriptPath = (Split-Path -Path $psEditor.GetEditorContext().CurrentFile.Path   -Parent)} # Running from Visual Studio Code
    

    【讨论】:

    • 使用支持信息改进您的答案,以便更好地理解。
    猜你喜欢
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    相关资源
    最近更新 更多