Clearing PowerShell cache

Clearing PowerShell cache

I am using PowerShell version 2, VMware vSphere PowerCLI (64bit) and VMware vSphere Client version 5.5

I have difficulty to clear PowerShell cache while running scripts.

Here is my scenario:
I have a script to query multiple vCenters and displaying clusters discovered in each vCenter

Here is a sample run of my script:

  vCenters provisioned
1-      vCenter01
2-      vCenter02
3-      vCenter03
Select a vCenter and press enter: 1

Clusters discovered in vCenter01:
1-      Cluster01-VC01
2-      Cluster02-VC01
3-      Cluster03-VC01
4-      Cluster04-VC01

If I run the script for the second time in the current PowerCLI window; I get the following output

      vCenters provisioned
1-      vCenter01
2-      vCenter02
3-      vCenter03
Select a vCenter and press enter: 2

Clusters discovered:
1-      Cluster01-VC01
2-      Cluster02-VC01
3-      Cluster03-VC01
4-      Cluster01-VC02
5-      Cluster02-VC02
6-      Cluster03-VC02
7-      Cluster04-VC02
8-      Cluster05-VC02

As you can see above the new discovery includes clusters from vCenter01 and vCenter02. It should show only cluster from vCenter02. I have made sure I disconnect from previous vCenter before connecting to another vCenter.

I have included the following statement in my script to clear and or remove variables each time I run the script, but it is not working:

Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host

I am using a variable called $AllClusters in my script as follows:

$AllClusters = Get-Cluster
 
I use the following statement to connect to different vCenters based on the user’ choice:

Connect-VIServer –server <vCenterName>
And at the end of the script I use the following statement to disconnect from selected vCenter:

Disconnect-VIServer -server <vCenterName> -confirm:$false


Thanks for your help
tech2000-usaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Try
Remove-Variable -Include * -ErrorAction SilentlyContinue

Generally your variables are cleared when the script (there's a difference between running a script vs. just the commands)stops, but where you're running the script (and how) can have an effect as well.  For example, are you using PS ISE, PS console, or something else?  It's pretty much all a question about scope.
1
tech2000-usaAuthor Commented:
Your suggestion didn't solve the issue. Including your statement in the script causes the following:

cmdlet Remove-Variable at command pipeline position 1
Supply values for the following parameters:
Name[0}:
0
footechCommented:
I missed that the -name parameter is required.  Your best course of action is to specify which variables you want removed.  You likely don't have many, so just make note of their names and supply those to the Remove-Variable command.

You didn't answer my question about how/where you're running your script.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tech2000-usaAuthor Commented:
Thank you, your suggestion resolve the issue!

Remove-Variable -Name AllCluster -ErrorAction SilentlyContinue


-Massoud
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.