I’ll start by saying i’m pretty new to Powershell and am really hoping to learn and eventually become proficient, but for now a I have what I think to be a simple question…
I’m building a script to query a single host which is entered by the user and return a pile of information. In my case I have set up each piece of information to query independently so it can be stored in a variable (Username, RAM, OS, ect.), but in doing so I now have 19 queries so far that as you can imagine can a bit to run being that I still have 4 more queries to add I was wondering what I can do to circle back and make this process more efficient. My current run time so far is roughly 50, obviously id like to see that closer to 5-7, I was wondering if there is a way to have the script fire say 5 separate WMI queries to a single machine simultaneously rather than one at a time.
Maybe i’m looking at this all wrong? Would it be better to pull down an entire namespace at once and somehow extract data from it? So far i’m querying 3 name spaces on the target machine if that makes a difference.
Without any line of your code it will quite hard to recommend something for you. But some general advices might help either.
If you fire some wmi queries to one machine it makes sense to combine these single queries in one cim session. That usually speeds up those tasks noticeably … here is something I set up time ago for a customer:
So, if you’re using Get-WmiObject, switch to Get-CimInstance. Faster. Establish a CimSession first, query against it, and then close it. Much faster. Make the connection over Dcom if needed (CimSessionOption), but don’t use the old, deprecated Wmi commands.
It’s not possible to query an entire namespace, and if it was, it would NOT be faster. Not the way the tech works.
I was able to clean it up quite a bit after I posted this by combining all of my “like” queries into one request, i’m evolving quickly. My second attempt ran in 7 seconds. I will deffinately look into switching to Get-CimInstance, im going to add some more info into this first. I have attached my remote query block below, this is just pulling basic system info. How can I segment a script into blocks of code? I would like to be able to control when, where, and why parts of my script execute, later adding call options as part of a GUI. I’m looking to have a call such as “Summary” that would just run this block and stop eliminating the need to have multiple script files.
Unfortunately my first reply has been blocked - I don’t know why. So I try again.
Here something I put together a while ago for a customer. You could use an inspiration or blue print and adapt it to your needs