The ISE environment for Powershell

Welcome Forums General PowerShell Q&A The ISE environment for Powershell

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #109933

    Participant
    Points: 195
    Rank: Participant

    I have just experienced a fairly weird session with Powershell/ISE.

    I have been working on a script of about 300 lines including comments (60% comments).

    As I repeatedly ran the script with errors the several few times until I got it "error free", I had to manually delete a folder which was created by this script because Powershell execution fails on a Remove-Item — claiming it is in use and therefore cannot be deleted.  This occurred several times and sometimes I just cancelled out and started all over.

    After a few more trials, my simple hash tables were yielding empty strings – these are "vanilla" hash tables that do nothing more than sequentially map 1,2,3 to file1, file2 and file3 which are in different subdirectories (i.e., path names).

    I struggled with just about everything to no avail — because the value results of the hash tables were yielding empty results, my queries also yielded fatal errors complaining that the filepath parameter could not be resolved.

    After nearly 3 hours of insignifcant code changes here and there, in desperation, I exitted PS, rebooted my PC and everything suddenly just worked, i.e., those error messages just went away.

    Is there some "dynamic" in Powershell/ISE in which — and I am saying this very crudely, as a beginner — the "state" of what you've "previously done" after repeated runs of the same script (many incomplete or aborted, plus changes here and there)  is "carried over" and persists in a way that distorts the debugging of the script?

    I don't know if this is makign sense, but would appreciate any tips or recommendations on this rather bizarre experience.

    Thanking you in advance.

     

     

     

  • #109943

    Participant
    Points: 298
    Helping Hand
    Rank: Contributor

    Yep, ISE persists variables across script runs, and also between running individual lines and full scripts. It's extremely difficult to work with for me.

    I typically use VS Code for editing nowadays, which does something similar where you can define variables  or run individual lines from your script in the console, but when it comes time to run the script as a whole it seems to do that in its own session to avoid the weird conflicts ISE creates.

    • #109949

      Participant
      Points: 195
      Rank: Participant

      Thanks Mr Joel Sallow ... this has been a real eye-opener.  I spent about 3 hours trying to figure the weirdest behavior whereby my simple variables just showed empty values, struggling with the usual "where did I go wrong".  Even re-typed the same lines over again in case I had copied over some un-displayable characters.  The reboot cleared everything in the ISE environment.  Now it seems I will need 2 PCs or 2 Logins on the same PC (and 2 separate monitors/screens), one for dev, one for running/testing.

      My sincerest gratitude for your timely intervention.

  • #109945

    Participant
    Points: 332
    Helping Hand
    Rank: Contributor

    Programming / scripting 101.

    Always, always dispose of stuff you create and the end of a script run.  It's called garbage collection for a really good reason. 8^}

    Name your vars with a prefix unique to the script.  Remove the vars by your prefix when done

    $ponTargetComputerName = $env:COMPUTERNAME
    
    $ponTargetComputerName
    Labhost001
    
    Get-Variable -Name '*pon*'
    
    Name Value
    ---- -----
    ponTargetComputerName Labhost001
    
    Remove-Variable -Name '*pon*' -Force -Verbose
    VERBOSE: Performing the operation "Remove variable" on target "Name: ponTargetComputerName".
    
    $ponTargetComputerName
    
    • #109952

      Participant
      Points: 195
      Rank: Participant

      Thank you Sir/Madam, this is very helpful and educational.  Your inputs are highly appreciated.

      Best,

  • #109954

    Participant
    Points: 332
    Helping Hand
    Rank: Contributor

    As for this...

    The reboot / restarting the tools clears everything in the PS/ISEVSCode environment, as it should.  Now it seems I will need 2 PCs or 2 Logins on the same PC (and 2 separate monitors/screens), one for dev, one for running/testing.

    No, no you don't. Especially that two PC thing, two logons, sure. More on that below.

    Just start multiple instances of the console host / ISE or VSCode as a different user identity.

    I do this daily, but only with two accounts. My normal user logon (unelevated so I can test as a normal user), and an elevated Administrator (where I do all my dev/test) using RunAs or using the properties shortcuts.

    So, Create two set of shortcuts for PS / ISE / VSCode. Leave one set at the default and set the other to use Admin on startup via the properties advanced settings.

    Still, as noted, clean-up behind yourself after each script run. The natural effect for closing and restarting PS/ISE/VSCode, is that it clears all stuff you were using, and sets up back to the defaults, unless you added stuff to you PS/ISE/VSCode profiles. I have the ISE on one screen always and VSCode on the other. I use the ISE mostly and only VSCode as needed.

    With the ISE, you don't even have to close it, even if you don't clean-up after yourself. Just open an new ISE tab, and within the same ISE GUI you have two different instances running, though both using the same user identity. You can open many PS tab, and they are all independent of one another as is each of their own script panes/files.

    You can literally have the same code open in each tab, and they will run independently, but if you make a physical change to the file in one tab, because both tabs are acting on the same file.

    https://docs.microsoft.com/en-us/powershell/scripting/core-powershell/ise/how-to-create-a-powershell-tab-in-windows-powershell-ise?view=powershell-6

     

    https://mikefrobbins.com/2014/05/19/open-scripts-in-a-new-tab-in-the-powershell-ise-using-psedit

    • #109960

      Participant
      Points: 195
      Rank: Participant

      Thanks very much for these tips.

      I was just wondering as an immediate reaction, will 2 instances as outlined in your advice mean 2 possibly "settings" on such "environment variables", e.g., env:$PSModulePath which are changeable.

      Thanks again in any case, this is quite helpful to know.

The topic ‘The ISE environment for Powershell’ is closed to new replies.