Warning: This is a pretty technical how-to post for those extending PowerGUI.
Now to the topic. PowerGUI is extremely honest. All it does to the system is just running PowerShell commands and giving you the outcome (and the code). This is great, but sometimes it can also be slow.
The upcoming 1.0.12 release is improving the performance quite a bit by re-using the collection of objects it already retrieved when you click on any of the headers to do a sort, or when you filter the data, or when you click any action or link. So for example, if you click Processes and then do something with the processes you retrieved, PowerGUI will not call Get-Process again, but will do sort, filter or some kind of pipelining on the collection you’ve already got.
The one situation in which this does not help is getting back to a node after you navigated away from it. Each time you click a node, PowerGUI is re-retrieving the data. There are situations in which you don’t need this (when the data is not that dynamic) so in this post I will show how you can make PowerGUI cache the data instead.
The example below is the code for a sample script node which I created to retrieve local system processes only the first time you click it and then on each subsequent click just use the previously cached collection:
# Cached Processes node to demo caching techniques in PowerGUI 1.0.11-12 # Try loading previously cached data $ProcessesCache=[System.AppDomain]::CurrentDomain.GetData("ProcessesCache"); # If no cache available query the servers if ($ProcessesCache -eq $null) { $ProcessesCache = Get-Process # Store the collection in cache [AppDomain]::CurrentDomain.SetData("ProcessesCache", $ProcessesCache); } # Display results $ProcessesCache
The code above will try to load a previously cached collection and only make the actual system code if the collection is not available in cache (-eq $null). This will make PowerGUI only make the call the first time you click the node after starting the tool.
Depending on how static your data actually is you might want to add some Refresh logic into your links, actions, etc.
In the future releases we will probably make caching even easier but this should let you start using it today without waiting for us (like folks like Antonio are apparently already doing)
Tags: KB, Knowledge Base, Known Issues, PowerGUI, PowerShell
Subscribe by email






0 Responses to “Caching data inside PowerGUI”