简体   繁体   中英

Powershell_ise doesn't refresh modification done outside

How to refresh Powershell_ise for contents modified outside the IDE.

Most of the time i would have opened both Powershell_ise and notepad++

If i does changes in Powershell_ise , notepad++ asks for reload but if i modify in notepad++ there is no way to refresh in Powershell_ise.

Whether any way to refresh the content or am i overlooking any feature which provides this?

This post is old, but I figured I'd post this as google brought me here with the same issue.

I eventually just wrote this little function which doesn't do exactly what the OP wanted, but maybe other googlers will find it useful:

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')

Its not perfect, but its good enough for me for now. All is does is create a key binding that closes,reopens, and then executes the current file. Its a bit jarring though because when you run it you'll lose your current cursor position when the file is closed and reopened. I'm sure you could store the column and line position of the cursor and restore it when reloading, but I'm too lazy to bother with that for the time being.

Edit: I accidentally posted an older non-working version of my code. Updated with working version.

PowerShell ISE does not support refreshing the changed files automatically. It is not there even in ISE v3.

There is connect suggestion on this topic: https://connect.microsoft.com/PowerShell/feedback/details/711915/open-ise-files-should-update-when-edited-externally

However, this can be done using PowerShell ISE Object model and PowerShell eventing. Explore $psise.CurrentFile and $psise.CurrentPowerShellTab.Files collection. This must give you enough information to write your own simple addon.

Here is a different spin on red888's script:

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

It restores the position of the caret after reload. I removed the line

iex $PsISE.CurrentPowerShellTab.Files.Editor.Text

As I didn't need it and its also not the same as running the script (and so results in strange behavior of statements like $script:MyInvocation.MyCommand.Path ).

Incidentally, if you put this code in your ISE profile it will automatically run when you first load the ISE. The ISE profile is just a powershell script whose location is given by the $profile variable.

Here are some commands that create the profile if it doesn't exist, and then opens it. Run it from inside the ISE:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM