Page 1 of 1

WMI question

Posted: Wed Sep 19, 2018 12:40 am
by ErnieB
Hello, can someone please assist with the following

I have the following script

Code: Select all

function Get-DiskInfo {
	param ()
	{ $Array = New-Object -TypeName System.Collections.ArrayList }
			$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 = $
					$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
		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

Re: WMI question

Posted: Wed Sep 19, 2018 12:50 am
by jvierra
The function already returns all drive types.

Re: WMI question

Posted: Wed Sep 19, 2018 12:53 am
by jvierra
To get CD/DVD drives you have to use a different class:

Get-WmiObject win32_cdromdrive

Re: WMI question

Posted: Wed Sep 19, 2018 2:04 am
by ErnieB
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

Re: WMI question

Posted: Wed Sep 19, 2018 2:08 am
by jvierra
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.

Re: WMI question

Posted: Wed Sep 19, 2018 3:29 am
by jvierra
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

Re: WMI question

Posted: Wed Sep 19, 2018 5:49 am
by ErnieB
OK thanks again for your help