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?
Try this: