Performing Management tasks using Cimcmdlets [2] – Computer Hardware

As part of this blog series we are sharing PowerShell snippets for a few Computer Management tasks. In this post we will be going over the samples for Computer Hardware management.

The Visual Basic samples for Computer Hardware Management are at: https://msdn.microsoft.com/en-us/library/aa394587(v=vs.85).aspx

The corresponding PowerShell samples are below:

1. Determine how much free memory a computer has:

PS:> # Get Instance of Win32_OperatingSystem

PS:> $os = Get-CimInstance –ClassName Win32_OperatingSystem –Namespace root/cimv2

PS:> # Look for FreePhysicalMemory property

PS:> $os | Select FreePhysicalMemory

2. Determine whether a computer has a DVD drive:

# Look for instances of Win32_CDROMDrive

$insts = Get-CimInstance –ClassName Win32_CDROMDrive –Namespace root/cimv2

# Loop through them and print DeviceID, Description and Name

foreach($inst in $insts)

{

$inst | Select Name, DeviceID, Description

}

3. Determine how much RAM is installed in a computer:

PS:> # Get instance of Win32_ComputerSystem

PS:> $inst = Get-CimInstance –ClassName Win32_ComputerSystem

PS:> # Print Name of the System and the Total Physical Memory

PS:> $inst | Select Name, TotalPhysicalMemory

4. Determine if a computer has more than one processor:

PS:> # Get instance of Win32_ComputerSystem

PS:> $inst = Get-CimInstance –ClassName Win32_ComputerSystem –Namespace root/cimv2

PS:> # Print Name of the system and Number of Processors

PS:> $inst | Select Name, NumberOfProcessors

5. Determine whether a computer has a PCMCIA slot:

# Use the Win32_PCMCIAController class and check the value of the Count property.

# If Count is 0, then the computer has no PCMCIA slots.

$insts = Get-CimInstance –ClassName Win32_PCMCIAController –Namespace root/cimv2

if($insts -ne $null)

{

$count = $insts.Count

if($count -eq $null)

{

$count = 1

}

Write-Host "Number of PCMCIA slots: $count"

}

else

{

Write-Host "Number of PCMCIA slots: 0"

}

6. Identify devices that are not working (those marked with an exclamation point icon in Device Manager)?

# Use the Win32_PnPEntity class

# Use the following clause in your WQL query. WHERE ConfigManagerErrorCode <> 0

$query = "Select * from Win32_PnPEntity WHERE ConfigManagerErrorCode <> 0"

$insts = Get-CimInstance -Query $query

# Loop through all such devices and print their information.

foreach($inst in $insts)

{

$inst | Format-Table -Property @("ClassGuid", "Description", "DeviceID", "Manufacturer", "Name", "PNPDeviceID", "Service")

}

7. Determine the properties of the mouse used on computer:

# Use the Win32_PointingDevice class.

# This returns the properties of all pointing devices, not just mouse devices.

$insts = Get-CimInstance –ClassName Win32_PointingDevice

# Loop though instances of Win32_PointingDevice and print the properties.

foreach($inst in $insts)

{

$inst | Format-Table –Property @(“Description", “DeviceID", “DeviceInterface", “DoubleSpeedThreshold", “Handedness", “HardwareType", “InfFileName", “InfSection", “Manufacturer", “Name", “NumberOfButtons", “PNPDeviceID", “PointingType", “QuadSpeedThreshold", “Resolution", “SampleRate", “Synch")

}

8. Determine the speed of a processor installed in a computer:

# Use the Win32_Processor class and check the value of the MaxClockSpeed property.

$insts = Get-CimInstance –ClassName Win32_Processor –Namespace root/cimv2

foreach($inst in $insts)

{

$inst | Select ProcessorId, MaxClockSpeed

}

9. Determine whether a computer is a tower, a mini-tower, a laptop, and so on:

# Use the Win32_SystemEnclosure class and check the value of the ChassisType property.

$colChassis = Get-CimInstance –ClassName Win32_SystemEnclosure –Namespace root/cimv2

# Loop though the collection of chassis

foreach($chassis in $colChassis)

{

# Look though the Chassis Types

foreach($item in $chassis.ChassisTypes)

{

Write-Host "Chassis Type: $item"

}

}

10. Get the serial number and asset tag of a computer:

# Use the Win32_SystemEnclosure class, and the properties SerialNumber and SMBIOSAssetTag.

$sytemEnclosures = Get-CimInstance –ClassName Win32_SystemEnclosure

$sytemEnclosureCollection = @()

$sytemEnclosureCollection += $sytemEnclosures

foreach($systemEnclosure in $sytemEnclosureCollection)

{

$systemEnclosure | Select PartNumber, SerialNumber, SMBIOSAssetTag

}

11. Determine what kind of device is plugged into a USB port:

# Use the Win32_USBHub class and check the Description property.

# This property may have a value such as "Mass Storage Device" or "Printing Support".

$items = Get-CimInstance –ClassName Win32_USBHub –Namespace root/cimv2

foreach($item in $items)

{

$item | Select DeviceID, PNPDeviceID, Description

}

12. Determine how many tape drives are installed on a computer:

# Use the class Win32_TapeDrive class and then count the number of instances received.

# If Count = 0, then no tape drives are installed on the computer.

$insts = Get-CimInstance –ClassName Win32_TapeDrive

$count = 0

if($insts -ne $null)

{

$count = $insts.Count

if($count -eq $null)

{

$count = 1

}

}

Write-Host "Number of tape drives = $count"

 

We will be covering other computer management scenarios in our future posts. If you have any questions please feel free to send them to us.

Important links:

            Processes

 

Thanks

Vaibhav Chugh [MSFT]

Standards Based Management