WMI question

Ask your 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.
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ErnieB
Posts: 56
Last visit: Mon Dec 19, 2022 2:09 am

WMI question

Post by ErnieB »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: WMI question

Post by jvierra »

The function already returns all drive types.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: WMI question

Post by jvierra »

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

Get-WmiObject win32_cdromdrive
User avatar
ErnieB
Posts: 56
Last visit: Mon Dec 19, 2022 2:09 am

Re: WMI question

Post 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: WMI question

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: WMI question

Post 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
User avatar
ErnieB
Posts: 56
Last visit: Mon Dec 19, 2022 2:09 am

Re: WMI question

Post by ErnieB »

OK thanks again for your help
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked