I have a script that works great. It is quite complex and does numerous “things”.. My problem started when I put the entire 1000-line script in a while-loop..
Some/many of my variables are doubled and messed up for each loop..
This is of course because I run the script in the same runspace.. So, what should I do? Delete all my variables with remove-variable at the end of my script? Or is there some kind of magic thing that lets me clear my runspace between each run?
I feel that I’ve cluttered things up and that I need some kind of scanner that tells me if I have any unneeded funcions/variables.. I am still in the early phases of learning and this script has been with me “all the way”, so it is by no means good coding..
So, what would be “best practice” in my situation?
You could add something like this to the initialization script for the job:
Then at the top of your while loop, dot source the remove-uservars function:
The should remove any variables your script created before each loop starts.