Powershell_ise不会刷新外部修改

时间:2012-01-09 14:21:25

标签: powershell powershell-ise

如何为在IDE外部修改的内容刷新Powershell_ise。

大部分时间我都会打开Powershell_ise和notepad ++

如果我在Powershell_ise中做了更改,notepad ++会要求重新加载,但如果我在notepad ++中修改,则无法在Powershell_ise中刷新。

是否有任何方式刷新内容或我是否忽略了提供此内容的任何功能?

3 个答案:

答案 0 :(得分:4)

这篇文章已经过时了,但我想我发布这篇文章是因为google把我带到了同样的问题。

我最终写了这个小功能,它并没有完全符合OP的要求,但也许其他googlers会发现它很有用:

function Build {
    #Reload file
    $CurrentFile = $psise.CurrentFile
    $FilePath = $CurrentFile.FullPath
    $PsISE.CurrentPowerShellTab.Files.remove($CurrentFile)
    $PsISE.CurrentPowerShellTab.Files.add($FilePath)

    iex $PsISE.CurrentPowerShellTab.Files.Editor.Text
}

$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Clear()
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Reload file and run",{Build},'f4')

它并不完美,但它对我来说已经足够了。一切都是创建一个关闭,重新打开,然后执行当前文件的键绑定。它有点刺耳,因为当你运行它时,当文件关闭并重新打开时,你将丢失当前的光标位置。我确定你可以存储光标的列和行位置,并在重新加载时恢复它,但我暂时懒得打扰它。

编辑:我不小心发布了我的代码的较旧的非工作版本。更新了工作版本。

答案 1 :(得分:3)

PowerShell ISE不支持自动刷新更改的文件。即使在ISE v3中也不存在。

有关此主题的连接建议:https://connect.microsoft.com/PowerShell/feedback/details/711915/open-ise-files-should-update-when-edited-externally

但是,这可以使用PowerShell ISE对象模型和PowerShell事件来完成。探索$ psise.CurrentFile和$ psise.CurrentPowerShellTab.Files集合。这必须为您提供足够的信息来编写您自己的简单插件。

答案 2 :(得分:2)

以下是red888剧本的不同内容:

function Reload {

    $CurrentFile = $psise.CurrentFile
    $FilePath = $CurrentFile.FullPath

    $lineNum = $psise.CurrentFile.Editor.CaretLine
    $colNum = $psise.CurrentFile.Editor.CaretColumn

    $PsISE.CurrentPowerShellTab.Files.remove($CurrentFile) > $null

    $newFile = $PsISE.CurrentPowerShellTab.Files.add($FilePath)

    $newfile.Editor.SetCaretPosition($lineNum,$colNum)
}

$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Clear()
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Reload File",{Reload},'f4') > $null

重新加载后恢复插入符号的位置。我删除了

iex $PsISE.CurrentPowerShellTab.Files.Editor.Text

因为我不需要它,它也与运行脚本不同(因此导致$script:MyInvocation.MyCommand.Path等语句的奇怪行为。

顺便提一下,如果您将此代码放入ISE配置文件中,它将在您首次加载ISE时自动运行。 ISE配置文件只是一个PowerShell脚本,其位置由$profile变量提供。

以下是一些创建配置文件的命令(如果它不存在),然后打开它。从ISE内部运行:

if (!(Test-Path (Split-Path $profile))) { mkdir (Split-Path $profile) } ;
if (!(Test-Path $profile)) { New-Item $profile -ItemType file } ;
notepad $profile
相关问题