WMI question

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
ErnieB
Posts: 52
Joined: Thu Jan 19, 2017 2:14 pm

WMI question

Post by ErnieB » Wed Sep 19, 2018 12:40 am

Hello, can someone please assist with the following

I have the following script

Code: Select all

function Get-DiskInfo {
	[cmdletbinding()]
	param ()
	
	begin
	{ $Array = New-Object -TypeName System.Collections.ArrayList }
	process
	{
		try
		{
			$DiskInfo = [ordered]@{ }
			$diskdrive = Get-WmiObject win32_diskdrive -ErrorAction Stop
			
			foreach ($drive in $diskdrive)
			{
				$DiskInfo.DeviceID = $drive.deviceid.substring(4)
				$DiskInfo.Model = $drive.model
				$DiskInfo.Size = $drive.Size
				
				$partitions = Get-WmiObject -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($drive.DeviceID.replace('\', '\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition" -ErrorAction SilentlyContinue
				
				foreach ($part in $partitions)
				{
					
					$DiskInfo.Partition = $part.name
					$vols = Get-WmiObject -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($part.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition" -ErrorAction SilentlyContinue
					
					foreach ($vol in $vols)
					{
						$DiskInfo.Volume = $vol.Name
						$DiskInfo.DriveType = $vol.DriveType
						
						[void]$Array.Add([PSCustomObject]$DiskInfo)
					}
				}
				
			}
			
		}
		catch { return "Error collecting disk, partition and volume information the error was $($Error[0].Exception.Message)" }
	}
	
	end { return $Array }
	
}

which returns information about the disk drives on your computer (mostly from the internet with some bits added by me).
This works fine, however I need one more important piece of information added to the output and would be grateful for assistance with this

The above any returns disk drives where the disk type is 3 e.g. a fixed disk as opposed to a CD/DVD or USB drive.

I want the script to out put information on all drives on the system weather they are fixed disks, CD/DVD or USB and output the corresponding Drive Type

Thanks in advance

User avatar
jvierra
Posts: 12773
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: WMI question

Post by jvierra » Wed Sep 19, 2018 12:50 am

The function already returns all drive types.

User avatar
jvierra
Posts: 12773
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: WMI question

Post by jvierra » Wed Sep 19, 2018 12:53 am

To get CD/DVD drives you have to use a different class:

Get-WmiObject win32_cdromdrive

User avatar
ErnieB
Posts: 52
Joined: Thu Jan 19, 2017 2:14 pm

Re: WMI question

Post by ErnieB » Wed Sep 19, 2018 2:04 am

Hello Jim
Thanks very much for taking the time to reply.

if I run the script on a system with a CD/DVD it does not return it is the CD/DVD drive is empty (e.g. no disk in the drive)
I believe this is becuase the class the script uses is Win32_LogicalDisk and as there is not disk in the CD in the drive there is no logical disk, makes sense

However, the class Win32_Volume does return both the fixed disks and the CD/DVD drive (regardless if there is no disk in the drive or not)

I need to create one function that marries up the Disk, Partition, LogicalDisk (as in the above script) and now I need to add Volume in order to get the rest of the information I need (including if the volume is bootable)

the problem is I cannot figure out how to marry up the LogicalDisk with the Volume (as has already been done between Disk, Partition and LogicalDisk)

So I am looking to create one function to return objects (one object per associated disk, partition, logical disk, volume ) and get all the information I need


Thanks very much

User avatar
jvierra
Posts: 12773
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: WMI question

Post by jvierra » Wed Sep 19, 2018 2:08 am

You are going to have to get all physical devices of the types you need. Note that CDs do not have partitions.

The issue here I deciding how to design the script to return things that are the same and not try to use volume or partition.

Start small - get all of you hardware devices then add extra info as needed.

User avatar
jvierra
Posts: 12773
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: WMI question

Post by jvierra » Wed Sep 19, 2018 3:29 am

The issue is a lack of understanding of computer hardware. This is the same on Windows, Unix or Mac. Devices are not all volumes. USB devices are unknown until mounted. THe same is tru with CD/DVD devices.

To get all volumes and CDs you can do this:

Get-PnpDevice -Class volume,cdrom -PresentOnly

User avatar
ErnieB
Posts: 52
Joined: Thu Jan 19, 2017 2:14 pm

Re: WMI question

Post by ErnieB » Wed Sep 19, 2018 5:49 am

OK thanks again for your help

Locked