WMIX – PowerShell WMI extensions Part 2

In Part –1 we learnt how to obtain and install the ‘WMIX’ module.

In this part we will spend some time using some of the functions from the module.

 

Get-ComputerSystem

PS C:\> Get-ComputerSystem

 

 

Domain              : WORKGROUP

Manufacturer        : VMware, Inc.

Model               : VMware Virtual Platform

Name                : VM1

PrimaryOwnerName    : Kiran

TotalPhysicalMemory : 3220688896

Note: by default the function shows a limited set of properties, to see the complete set, type :

Since we don’t want to see all the properties let’s select just the properties that are of interest to us :

PS C:\> Get-ComputerSystem | select Name,Domain,Manufacturer,Model,SystemType

 

 

Name         : VM1

Domain       : WORKGROUP

Manufacturer : VMware, Inc.

Model        : VMware Virtual Platform

SystemType   : X86-based PC

 

the ‘SystemType’ property above denotes the operating system type:

  • 32-Bit –> X86-based PC
  • 64-Bit –> X64-based PC

Useful property to be aware of. If you would like to find out whether you can install a 64-bit operating system on your computer type the following:

if the above command returns ‘64’ your processor is able to support 64-bit OS, If on the other hand it returns ‘32’ then you are limited to 32-bit operating systems.

let’s get back to ‘Get-ComputerSystem’. So far we have been getting information from the local system but what if I want to run these commands remotely. Say I have a bunch of computers on my network and I would like to be able to determine their model, manufacturer,systemtype etc., how would I do it?

turns out this is as simple as providing a “ComputerName” parameter to the functions from WMIX.

I’ve got 2 domain controllers on my lab network so lets run the command against the first server – KKS-DC1.

PS C:\> Get-ComputerSystem -ComputerName KKS-DC1

Get-WmiObject : Could not get objects from namespace ROOT\cimv2. Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

At C:\Users\kiran\Documents\WindowsPowerShell\Modules\wmifx\root\cimv2\Get-ComputerSystem.ps1:201 char:16

 

if you are seeing this error then that means your logged on account does not have admin privileges on the remote computer which is required to run these commands. Fortunately the function has a “Credential” parameter which we can use to provide alternate credentials.

Note :  I have to specify alternate credentials because I am logged onto a system that is in a workgroup and attempting to run code against a domain joined computer.

PS C:\> Get-ComputerSystem -ComputerName KKS-DC1 –Credential VMLAB\Administrator

 

 

Domain              : VMLAB.COM

Manufacturer        : VMware, Inc.

Model               : VMware Virtual Platform

Name                : KKS-DC1

PrimaryOwnerName    : Labuser

TotalPhysicalMemory : 535924736

 

So that worked. lets run this on the second server – KKS-DC2 this time we will custom select our properties.

PS C:\> Get-ComputerSystem -ComputerName KKS-DC2 –Credential VMLAB\Administrator |

  Select Name,Domain,Manufacturer,Model,SystemType

 

 

Name         : KKS-DC2

Domain       : VMLAB.COM

Manufacturer : VMware, Inc.

Model        : VMware Virtual Platform

SystemType   : x64-based PC

 

 

Since the “ComputerName” parameter accepts an array of strings we can specify multiple computers on the same line.

PS C:\> Get-ComputerSystem -ComputerName KKS-DC1,KKS-DC2 -Credential VMLAB\Administrator | Select Name,Domain,Manufacturer,Model,SystemType

 

 

Name         : KKS-DC1

Domain       : VMLAB.COM

Manufacturer : VMware, Inc.

Model        : VMware Virtual Platform

SystemType   : X86-based PC

 

Name         : KKS-DC2

Domain       : VMLAB.COM

Manufacturer : VMware, Inc.

Model        : VMware Virtual Platform

SystemType   : x64-based PC

 

okay so this works for multiple computers but what if I have a 1000 computers, do I have to specify them one by one? nope, you don’t have to. Just use the following syntax:

How about some filtering; Can we list only those computers that have a “SystemType” of x64? No problem, use the “Filter” parameter.

Note :  The filter parameter is very picky about the syntax so make sure that you are typing in the property names and values exactly as shown below( the single quotes inside the double quotes ). Also do notice that I am only selecting 2 properties to display.

PS C:\> Get-ComputerSystem -ComputerName KKS-DC1,KKS-DC2  -Credential VMLAB\Administrator -Filter “SystemType=’x64-based PC'”  | select Name,SystemType

 

Name    SystemType 

—-    ———- 

KKS-DC2 x64-based PC

 

As you can see, although we specified 2 computers we got results from just one computer because that was the only one which matched our condition.We could have obtained the same result  using “Wildcards” like so:

 

I had mentioned earlier that you can determine your processor’s capability using “Get-Processor”. Let’s use that on the 2 lab servers.

Note :  We cannot use the Credential parameter when running the query against the localsystem, hence the 2 separate queries below. If you are wondering how I found out about the AddressWidth and DataWidth properties I got them from the following MSDN link

 

PS C:\> Get-Processor -ComputerName KKS-DC1,KKS-DC2  -Credential VMLAB\Administrator   | select SystemName,AddressWidth,DataWidth

 

SystemName AddressWidth DataWidth

———- ———— ———

KKS-DC1              32        64

KKS-DC2              64        64

 

 

PS C:\> Get-Processor -ComputerName Localhost     | select SystemName,AddressWidth,DataWidth

 

SystemName AddressWidth DataWidth

———- ———— ———

VM1               32        64

 

 

 

From the results above we can determine the following:

  • KKS-DC1              –> Has a 64-bit processor and a 32-bit operating system running on it.
  • KKS-DC2              –> Has a 64-bit processor and a 64-bit operating system running on it.
  • VM1(localsystem) –> Has a 64-bit processor and a 32-bit operating system running on it.

So if I wanted I could format KKS-DC1 and my localsystem to install a 64-bit operating system, but I think we will hold off on that, at least for the time being Smile

Lets do one last example. We will try to find out how much memory is installed on my Localsystem using PowerShell.

 

Get-PhysicalMemory

Note :  The MemoryType property value of 2 means “DRAM” –> MSDN link.

 

PS C:\> Get-PhysicalMemory | select PSComputerName,BankLabel,MemoryType,Capacity

 

PSComputerName BankLabel   MemoryType   Capacity

————– ———   ———-   ——–

VM1         RAM slot #0          2 2147483648

VM1         RAM slot #1          2 1073741824

VM1         RAM slot #2          2  536870912

VM1         RAM slot #3          2  268435456

VM1         RAM slot #4          2  134217728

VM1         RAM slot #5          2   33554432

VM1         RAM slot #6          2   16777216

 

The result shows that my localsystem has 7 memory banks with varying amounts of memory installed on each. This goes against the recommended config of installing the same capacity into each bank but since this is a virtual system the above is quite all right.

The “Capacity” property displays values in bytes which makes it hard to read, so let’s convert those byte values into Megabyte values.

PS C:\> $CapacityMB = @{Name=’CapacityMB';Expression={$_.capacity/1mb}}

 

PS C:\> Get-PhysicalMemory | select PSComputerName,BankLabel,MemoryType,$CapacityMB

 

PSComputerName BankLabel   MemoryType CapacityMB

————– ———   ———- ———-

VM1         RAM slot #0          2       2048

VM1         RAM slot #1          2       1024

VM1         RAM slot #2          2        512

VM1         RAM slot #3          2        256

VM1         RAM slot #4          2        128

VM1         RAM slot #5          2         32

VM1         RAM slot #6          2         16

 

 

looking much better.Just for kicks let’s add a custom column displaying the total ram installed on the system.

Note:  This can be considered as an advanced trick so if you are new to PowerShell don’t worry about it, as you gain experience you will begin to understand all the advanced concepts.

PS C:\> Get-PhysicalMemory | Foreach-Object {  $_ | Add-Member NoteProperty TotalRam ($Ram += $_.capacity/1mb) -PassThru} | select PSComputerName,BankLabel,MemoryType,$CapacityMB,TotalRAM

 

PSComputerName BankLabel   MemoryType CapacityMB TotalRam

————– ———   ———- ———- ——–

VM1         RAM slot #0          2       2048     2048

VM1         RAM slot #1          2       1024     3072

VM1         RAM slot #2          2        512     3584

VM1         RAM slot #3          2        256     3840

VM1         RAM slot #4          2        128     3968

VM1         RAM slot #5          2         32     4000

VM1         RAM slot #6          2         16     4016

 

 

The TotalRAM property increments with each instance of a memory bank. So the last row depicts the total amount of ram installed on my system which is 4016 MB or roughly 4 GB.

Here is another way of calculating the total ram installed on a computer:

PS C:\> $TotalRam = (Get-PhysicalMemory | Measure-Object -Property Capacity -Sum).Sum/1MB

 

PS C:\> $TotalRam

4016

 

And here’s the proof Smile

81 memory

So think about what you want to get and just prefix that with a “Get-“ , more often than not you will get what you intended.

Leave a Comment Yourself

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">