Reputation: 4247
I have a function that iterates through all HDD's on a computer, and returns information about those drives and their mapping to physical drives in an array.
I would like this function to return the information in a custom object.
Here is the function:
##--------------------------------------------------------------------------
## FUNCTION.......: Get-HDDInfo
## PURPOSE........:
## REQUIREMENTS...:
## NOTES..........:
##--------------------------------------------------------------------------
Function Get-HDDInfo {
[CmdletBinding()]
Param([Parameter(Mandatory = $True,
ValueFromPipeLine = $True,
Position = 0)]
[String[]]$ComputerName
)#END: Param
$W32_DD = @(gwmi Win32_DiskDrive -ComputerName $ComputerName)
$Array = @()
$W32_DD | foreach {
$query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" `
+ $_.DeviceID + "'} WHERE ResultClass=Win32_DiskPartition"
$Array += $_.Name
$Array += $_.Model
<#
$obj = New-Object PSObject
$obj.PSObject.typenames.insert(0,'JoeIT.Custom.SystemInfo')
$obj | Add-Member -MemberType NoteProperty -Name `
"PDCaption" -Value $_.Name
$obj | Add-Member -MemberType NoteProperty -Name `
"PDModel" -Value $_.Model
$Array += $obj
#>
Get-WmiObject -Query $query | foreach {
$Array += $_.Name
$Array += $_.Description
$Array += $_.PrimaryPartition
#$obj = New-Object PSObject
<#
$obj.PSObject.typenames.insert(0,'JoeIT.Custom.SystemInfo')
$obj | Add-Member -MemberType NoteProperty -Name `
"DPName" -Value $_.Name
$obj | Add-Member -MemberType NoteProperty -Name `
"DPDescription" -Value $_.Description
$obj | Add-Member -MemberType NoteProperty -Name `
"DPPrimary" -Value $_.PrimaryPartition
#>
$query2 = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" `
+ $_.DeviceID + "'} WHERE ResultClass=Win32_LogicalDisk"
Get-WmiObject -Query $query2 | ForEach {
$Array+= $_.Name
$Used = [math]::round($_.Size/1024/1024/1024,0)
$Free = [math]::round($_.FreeSpace/1024/1024/1024,0)
$Array += [String]$Used +"GB"
$Array += [String]$Free +"GB"
#Return $Array;
#$Array = $Null
}
<#
$Array += $obj
$obj = $Null
#>
}#END: Get-WmiObject -Query
}#END: $W32_DD | foreach
##----------------------------------------------------------------------
## Store results in custom Object
##----------------------------------------------------------------------
Return $Array
}#END: Function Get-HDDInfo
The stuff that is commented out is from my attempts to get the information into a custom object. Maybe I'm just a bit burnt out, but I just can't seem to make this work right. As you see it, the commented out code tries to overwrite named properties - I knew that when I wrote it, but for some reason I expected it to work anyway ;)
Maybe I shouldn't work three weeks without a day off, but my brain just isn't letting me solve this problem.
What I want is to be able to do something like this:
$test = (get-hddinfo $SVR01)
$test.PhysicalDrive1
$test.Partition1
$test.DriveLetter1
$test.TotalSize1
$test.FreeSpace1
This would query a computer named SVR01, and write out the first physical HDD, the first logical partition of that drive, the assigned drive letter, total size of the disk, and free space on the disk.
I could then do something like
$test.PhysicalDrive2
$(same code here for the second physical drive)
What the hell am I doing wrong?
Upvotes: 2
Views: 2588
Reputation: 60938
Try this:
[CmdletBinding()]
Param([Parameter(Mandatory = $True,
ValueFromPipeLine = $True,
Position = 0)]
[String[]]$ComputerName
)
$W32_DD = @(gwmi Win32_DiskDrive -ComputerName $ComputerName)
$a = new-object System.Object
$sc3 = 1
$sc2 = 1
$sc1 = 1
$W32_DD | foreach {
$query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" `
+ $_.DeviceID + "'} WHERE ResultClass=Win32_DiskPartition"
$a | Add-Member -type NoteProperty -name DiskDriveName$sc1 -value $_.Name
$a | Add-Member -type NoteProperty -name DiskDriveModel$sc1 -value $_.Model
Get-WmiObject -Query $query | foreach {
$a | Add-Member -type NoteProperty -name PartitionName$sc2 -value $_.Name
$a | Add-Member -type NoteProperty -name PartitionDescription$sc2 -value $_.Description
$a | Add-Member -type NoteProperty -name PrimaryPartition$sc2 -value $_.PrimaryPartition
$query2 = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" `
+ $_.DeviceID + "'} WHERE ResultClass=Win32_LogicalDisk"
Get-WmiObject -Query $query2 | ForEach {
$a | Add-Member -type NoteProperty -name LogicalDiskName$sc3 -value $_.Name
$Used = [math]::round($_.Size/1024/1024/1024,0)
$Free = [math]::round($_.FreeSpace/1024/1024/1024,0)
$a | Add-Member -type NoteProperty -name UsedSpace$sc3 -value $([String]$Used +"GB")
$a | Add-Member -type NoteProperty -name FreeSpace$sc3 -value $([String]$Free +"GB")
$sc3++
}
$sc2++
}
$sc1++
}
Return $a
Upvotes: 2
Reputation: 72660
Here is a way, it's not exactly what you want but it gives you a way to do it :
##--------------------------------------------------------------------------
## FUNCTION.......: Get-HDDInfo
## PURPOSE........:
## REQUIREMENTS...:
## NOTES..........:
##--------------------------------------------------------------------------
Function Get-HDDInfo
{
[CmdletBinding()]
Param([Parameter(Mandatory = $True, ValueFromPipeLine = $True, Position = 0)]
[String[]]$ComputerName)#END: Param
$W32_DD = @(gwmi Win32_DiskDrive -ComputerName $ComputerName)
$ArrayofPD = @()
foreach ($dd in $W32_DD)
{
$query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + $dd.DeviceID + "'} WHERE ResultClass=Win32_DiskPartition"
# create a new physical disc object
$PDobj = New-Object PSObject
$PDobj | Add-Member -MemberType NoteProperty -Name "PDCaption" -Value $dd.Name
$PDobj | Add-Member -MemberType NoteProperty -Name "PDModel" -Value $dd.Model
$ArrayofLD = @()
$diskParts = Get-WmiObject -Query $query
foreach ($diskPart in $diskParts)
{
# create a new logical disc object
$LDobj = New-Object PSObject
$LDobj | Add-Member -MemberType NoteProperty -Name "DPName" -Value $diskPart.Name
$LDobj | Add-Member -MemberType NoteProperty -Name "DPDescription" -Value $diskPart.Description
$LDobj | Add-Member -MemberType NoteProperty -Name "DPPrimary" -Value $diskPart.PrimaryPartition
$query2 = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + $diskPart.DeviceID + "'} WHERE ResultClass=Win32_LogicalDisk"
$LogicalDisk = Get-WmiObject -Query $query2
if ($LogicalDisk -ne $null)
{
$LDobj | Add-Member -MemberType NoteProperty -Name "LGName" -Value $LogicalDisk.Name
$Used = [math]::round($LogicalDisk.Size/1024/1024/1024,0)
$Free = [math]::round($LogicalDisk.FreeSpace/1024/1024/1024,0)
$LDobj | Add-Member -MemberType NoteProperty -Name "UsedSpace" -Value $([String]$Used +"GB")
$LDobj | Add-Member -MemberType NoteProperty -Name "FreeSpace" -Value $([String]$Free +"GB")
}
$ArrayofLD += $LDobj
}
$PDobj | Add-Member -MemberType NoteProperty -Name "LogicalDisks" -Value $ArrayofLD
$ArrayofPD += $PDobj
}
##----------------------------------------------------------------------
## Store results in custom Object
##----------------------------------------------------------------------
Return $ArrayofPD
}#END: Function Get-HDDInfo
Clear-Host
$a = Get-HDDInfo localhost
$a
Dot source the function for me it gives :
PS C:\Users\JPB\Documents> $a = Get-HDDInfo localhost
PS C:\Users\JPB\Documents> $a
PDCaption PDModel LogicalDisks
--------- ------- ------------
\\.\PHYSICALDRIVE0 ST9500420AS {@{DPName=Disque n° 0, partition n° 0; DPD...
\\.\PHYSICALDRIVE1 ST932042 3AS USB Device {@{DPName=Disque n° 1, partition n° 0; DPD...
And :
PS C:\Users\JPB\Documents> $a[0].LogicalDisks
DPName DPDescription DPPrimary
------ ------------- ---------
Disque n° 0, partition n° 0 Système de fichiers installable True
Disque n° 0, partition n° 1 Système de fichiers installable True
Upvotes: 1