Question

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

Était-ce utile?

La solution

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.

Autres conseils

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;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top