A few weeks in the past, I acquired an e-mail message from somebody who requested a reasonably thought-provoking query. He wished to know if there’s a simple approach to calculate a complete for a column of data displayed in PowerShell. Extra particularly, the one that wrote me the e-mail wished to have the ability to use PowerShell to find out the quantity of assets that had been being collectively utilized by a bunch of Hyper-V digital machines.
Whereas the above query appears easy sufficient, there’s a lot to unpack. The query implies the necessity for making a VM group, including digital machines to that group, performing some kind of useful resource question in opposition to the VMs within the group, after which totaling the question outcomes. For this text, nonetheless, I’m going to maintain issues a little bit bit easier.
Somewhat than going by means of all the steps that I described above, I’m going to make use of the Get-VM cmdlet to amass a listing of all the digital machines that reside on a selected Hyper-V host, after which I’m going to derive the entire quantity of reminiscence that’s in use by these digital machines. Certain, there are simpler methods to determine how a lot reminiscence is in use, however determining how a lot reminiscence is in use isn’t the purpose. The approach that I’m about to indicate you can be utilized to create a sum whole for any column of data displayed in PowerShell. I’m utilizing reminiscence for example, however the approach might simply as simply be utilized to CPU assets, disk house, storage IO, or every other metric.
In the event you take a look at the screenshot under, you possibly can see that I’ve run the Get-VM cmdlet on a Hyper-V server. As a result of I didn’t specify any further command parameters, PowerShell exhibits me each VM on the host, in addition to the state, CPU utilization, reminiscence utilization, uptime, standing, and model of every digital machine. Since there are solely six VMs current, it’s simple to see that the 5 working VMs are consuming 1GB of reminiscence every, for a collective whole of 5GB.
On this case, it was simple to inform how a lot reminiscence was getting used simply by trying, however in the actual world, issues aren’t at all times fairly really easy. That being the case, let me present you methods to make PowerShell give you the entire for you.
The very first thing that we have now to do is to determine the identify of the parameter that holds the digital machine reminiscence worth. Within the screenshot above, the column header is known as MemoryAssigned(M). Nevertheless, in PowerShell column headers don’t at all times match parameter names. If I had been to append the Choose-Object M* cmdlet to the top of the Get-VM cmdlet, PowerShell exhibits me all the VM parameters that begin with the letter M. As you possibly can see within the subsequent determine, the parameter that displays the quantity of reminiscence getting used is MemoryAssigned.
Now that we all know the column identify, we will sum the column. Regardless that there are methods of getting a column sum instantly, I favor to make use of a variable. The explanation for that is that relying on what sort of information you might be totaling, PowerShell won’t at all times current the information in the very best format. I’ll present you what I imply in only a second. For proper now although, the command that’s used to find out the entire quantity of reminiscence utilized by the Hyper-V digital machines is:
$Complete = (Get-VM | Measure-Object ‘MemoryAssigned’ -sum).sum
As you possibly can see, this command is comparatively easy. I’m creating a variable named $Complete. This variable is assigned the output of the Get-VM cmdlet after it has been filtered by the Measure-Object cmdlet. On this case, the Measure-Object cmdlet is being directed towards the MemoryAssigned column, which is then being summed. The screenshot under exhibits the command being utilized in PowerShell, in addition to the worth that’s in the end assigned to the $Complete variable.
Earlier, I mentioned that I wish to map the outcomes to a variable as a result of PowerShell doesn’t at all times output the worth in the most effective format. This can be a good instance of that assertion. Whereas it’s true that PowerShell is telling us that the VMs are collectively consuming 5GB of reminiscence, the 5GB worth is being expressed as 5,368,709,120. In different phrases, PowerShell is reporting digital machine reminiscence utilization in kilobytes. Regardless that there are particular use instances for this, it’s often going to be extra handy to see the worth expressed when it comes to gigabytes. Fortunately, that is very easy to do.
Since we mapped the worth to a variable, all we have now to do is to divide the variable by 1GB. We don’t even should know the variety of kilobytes in a gigabyte, as a result of PowerShell is natively in a position to divide a worth by gigabytes. Right here is the command:
$Complete / 1GB
As you possibly can see within the subsequent screenshot, this command provides a results of 5, indicating that the VMs are collectively consuming 5GB of reminiscence. By the way, you possibly can simply as simply show the worth in megabytes by dividing by 1MB reasonably than 1GB.
Digital machine assets: Leverage PowerShell for extra data
Regardless that I labored by means of the instance of displaying the quantity of reminiscence that my digital machines are collectively consuming, the approach that I confirmed you shouldn’t be restricted to tallying reminiscence consumption. PowerShell can report on all kinds of digital machine attributes and you should use this method to sum any numerical attribute. You can even filter the listing of VMs after which create sums for attributes inside the filtered listing.
Featured picture: Freepik / Science vector created by Slidesgo
Extra PowerShell Fundamentals articles