The seemingly simple problem

I could not believe what a total pain in the ass this was to resolve. I tried getting the CPU and Memory utilization via CMD but that produced incredibly unreliable results. The biggest problem being that division operations in CMD will only return integers so I was getting very weird results. Yes there is a way around this, but it was more of a hassle so I gave up and looked to PowerShell to save the day when CMD fails. However PowerShell has its own problems, there was a reason I was using CMD and that was because I needed to be able to call a batch file from a program that would only execute CMD commands.

The somewhat simple solution

The solution here, once all is said and done, is pretty simple – but getting to this point was NOT simple. There was a lot of frankensteining, google coding and asking coworkers questions. Anyhow below is the solution for those of you who just need to make things work, I am going to explain each part below the code for those interested in the break down.

Batch file component

You need this to call the PowerShell script from CMD in a single execution.

PowerShell component

If you just want to run the PowerShell script, then this is all you need.

How does this work?

The idea here is that you are going to call the batch file which is going to call the PowerShell script, but there is only one caveat with the way I have things configured. You have to name your batch file the same thing as your PowerShell script to make this seamless. This honestly just makes life easier. Do you have to do it this way? No, but again it’s just easier.

Example:

  1. Create a batch file named “GetCpuAndMemoryReadings.bat”
  2. Create a PowerShell script named “GetCpuAndMemoryReadings.ps1”
    1. Make sure to explicitly specify where you are saving your readings file. I used a flat file to save the results because propagating the output back to CMD was a total paint in the ass. Again not worth the time to figure it out (like a lot of things in PowerShell I find is the theme).
  3. When you run the batch file from CMD your reading will be outputted to the flat file.
  4. If you have another process that needs to consume the data, read the flat file you just produced

Credit where credit is due

  • The PowerShell script is largely adapted from this SO answer: https://stackoverflow.com/a/18091659/603807
  • My coworker gave me the batch file solution, I had no idea you could do that – it is very useful for calling PowerShell scripts

Leave a Reply

Your email address will not be published. Required fields are marked *