I was about to share this little performance tip with a friend and figured it would be good to show some actual data to back it up.
Querying the same WMI class on a remote host multiple times creates multiple connections and multiple queries. Reuse objects to increase performance.
Here’s an example of two WMI calls originating from a server called DEMOSF1 to another server called DEMO2K8 concerning the same Win32_ComputerSystem property.
WMI Trace activity on the target server shows that indeed, there are multiple connections and multiple queries, even though we are asking for the same data twice. See for yourself how the timestamps in each event match up with the source queries above…
Should I Really Care?
Authenticating twice to the same machine and pulling all 50+ properties to only use one each time might not seem like a big deal, but imagine doing that on 100 machines or 1,000 or 20,000. Then imagine needing many properties from many different WMI classes on a regular basis using this technique.
A simple change can save some time and provide better performance while making your network admins happy.
Get a reference to the WMI class once, then use any of the properties as you please – locally.
On the remote host, now we only see one WMI connection and one WMI query.
So, even though we accessed multiple properties for the returned object, subsequent WMI connections and queries were not taking place (or needed).
Whether you’re new to WMI and PowerShell or even an experienced scripter, it’s always good to find more efficient ways to use your available resources while automating processes. Don’t forget to add error checking and look for even more ways to make your WMI queries more efficient!