5

I'm running 64bit Powershell ISE on Windows Server 2008r2.

While debugging I set a variable like:

$dbName = "db1"

I then make the following change

$dbName = "db2"

I step through the script again and even though the debugger steps over the amended line, when I then hover over the $dbName variable it still shows a value of "db1". The only way I can seem to work around this is to restart that ISE - which is a pain!

Can anyone tell me where I'm going wrong please?

* Update *

I have the following function in a module:

Function SampleFunction()
{
    $dbName = "db1"
    write-host $dbName
}

I have a powershell script that imports the moduleL

Import-Module -Name ".\BadPsm.psm1"

From the powershell ISE I place a breakpoint on $dbName = "db1" before executing the function by entering "SampleFunction" in the execution window. All is good and the value "db1" is written to the output window.

I then change the function so that $dbName = "db2". I re-import the module by executing Import-Module -Name ".\BadPsm.psm1" again.

When I execute the function again I hit the breakpoint, step over and can see that $dbName still equals "db1", "db1" is also written to the output window.

In order to help illustrate I've posted a short screencast to youtube here: youtube link

Rob Bowman
  • 7,632
  • 22
  • 93
  • 200
  • 1
    Can you give us the steps that you follow to get the issue? I have tried, and I can't seem to re-create the issue. The closest I got was while debugging, it briefly showing the old value, and refreshing 1 second later to show the new value. – HAL9256 Jan 02 '14 at 19:09
  • 1
    I can't reproduce your problem using the current sample, but it sounds like a variable scope-problem. Please provide a stripped down(remove sensitive info) copy of your script. – Frode F. Jan 02 '14 at 20:27
  • What happens if you run the script rather than using the debugger? – Jimbo Jan 02 '14 at 21:04
  • IIRC F5 runs the script in current scope. Try running the script by typing its name instead, and see if the problem persists. – Bill_Stewart Jan 02 '14 at 22:15
  • Please see screencast I posted to youtube that illustrates the problem. – Rob Bowman Jan 03 '14 at 14:59

2 Answers2

6

Your video shows that after changing your module you did not unload it before calling import-module again. Calling Import-Module a second time for the same module does nothing since it is already loaded. That is why you see the result of your old function.

The solution is to call Remove-Module before calling Import-Module a second time. Or use Import-Module's -Force parameter to force reloading.

Import-Module -Name ".\BadPsm.psm1" -Force

The reason why exiting PowerShell ISE helps is because then you start fresh. It has nothing to do with PowerShell ISE. You would also see this effect in the PowerShell console.

The reason why others have tried and not able to reproduce what you are seeing is because they didn't use modules so each time they change the code, that changed code is run.

Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
  • That's it! When I added -Force at the end of Import-Module it works fine. Many thanks Lars, this has been bugging me for a long time – Rob Bowman Jan 03 '14 at 15:56
0

You can use the Remove-Variable cmdlet to clean it up, without having to exit the PowerShell ISE. You can also simply set the variable to $null, although it will still exist.

$myvar = $null;
Remove-Variable -Name myvar;