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.
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
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
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.
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.