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
The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.
To display the value of a variable, type the variable name, preceded by a dollar sign ( $ ). To change the value of a variable, assign a new value to the variable. The following examples display the value of the $MyVariable variable, changes the value of the variable, and then displays the new value.
The Clear-Variable cmdlet deletes the data stored in a variable, but it does not delete the variable. As a result, the value of the variable is NULL (empty). If the variable has a specified data or object type, this cmdlet preserves the type of the object stored in the variable.
We can also access environment variables using the built-in variable called $env followed by a colon and the name of the environment variable. For example, instead of using Get-Item or Get-ChildItem and using the Env drive, I can save some keystrokes and instead simply specify $env:COMPUTERNAME.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With