Windows PowerShell ISE Launches Old Version

I am writing PowerShell scripts using Windows PowerShell ISE. When I change someting to a script and run the script, the last saved version of the script is not executed, but the older one. Only if I run the script a second time does it use the current version. What can I do to always run the latest version of a script?

+6
source share
3 answers

After making the changes, you need to run the script again by searching it pointwise. Assuming you have a file called MyScript.ps1 in the current directory, in the console, you run the following command:

. .\MyScript.ps1 

If you want to call a specific function in a script, then you can simply do:

 . .\MyScript.ps1 MyFunction 
+1
source

This is a very old question, but I think I came across the same problem. In my case, I defined a function after calling it. It works, but only because "myfunc" still matters from the previous call. If you change “Hello, World!”, You will find that the new value only affects the second attempt.

 Invoke-Command -ScriptBlock ${function:myfunc} function myfunc() { Write-Host "Hello, World!" } 

To fix the problem, simply define the function to which you are trying to call.

 function myfunc() { Write-Host "Hello, World!" } Invoke-Command -ScriptBlock ${function:myfunc} 
+1
source

My experience with ISE caching old files:

ISE behavior is different for PS modules (.psm1) and simple PS scripts (ps1). I am using PS & ISE with Win10Pro.

A) My experience with modules (. PSM1)

  • Download the module file "hello.psm1", already located in the corresponding module in the directory "C: \ Users \ MyUserName \ Documents \ WindowsPowerShell \ Modules \ Hello" on ISE
  • Execute the function "Run selection" (you cannot execute the modules "Run Script")
  • Modify the file, for example. output function "Write-Host" Hello World! "'to" Write-Host "Hello" and save the file.
  • Execute the function “Execute selection” and it will execute the old function, with the old output, for example. "Hello World!". This is also true if I repeat the Make Selection command.
  • Simply, if I leave ISE and download it again, the new function will be executed using the “Select” command.

C) My experience with scripts (.PS1).

  • If I run Run Selection, then the behavior is the same as with modules.
  • If I run "Run Script" once and thereby call the function, the current version of the function is executed. Of course, I call the function (for example, on line number 100) in the "after" file, it is defined (for example, in lines 10-20). So there is no caching.
  • In particular, and therefore, if I execute "Run Script" once, and the executable code does not call this function, and then performs my "Run selection" function, the current version of the function is executed.

C) Here is my workaround to make modular development more comfortable:

  • Simple scripts can be run using Run Script.
  • If I want to develop a module (.PSM1), I call it a simple script (.PS1) at the development stage. I put the file (for example, "hello.ps1") already in the correct module folder, for example. "C: \ Users \ MyUserName \ Documents \ WindowsPowerShell \ Modules \ Hello" for the "Hello" module. Of course, by this I cannot perform functions as functions of a module from the PowerShell console. I just want to use ISE to call functions for testing.
  • Since the modules are not executed as a script, the file does not have executable code, but simply functions (AFAIK). Therefore, I can safely execute "Run Script" on my script.

So, if I modify the module file as part of ISE development, I always run “Run Script” first before I run the function using “Run Selection”.

In doing so, I always execute the current version of the function.

Although my problem was a little different, this question and answers were very helpful for me to find a solution. It is hard to find such questions about ISE caching and useful answers.

Regards Rolf

0
source

Source: https://habr.com/ru/post/954661/


All Articles