Search code examples
powershelloverloadingpsobjectget-wmiobject

Issues Overloading ToString on Nested PSObject Property


I've created a custom PSObject with a variety of properties and I'm trying to overload ToString for a few of them. I've had success with the one of the TimeSpan properties, but not with one that is a WMI object.

# Get WMI info
$wmiOS = Get-WmiObject -Class Win32_OperatingSystem
$wmiSystem = Get-WmiObject -Class Win32_ComputerSystem
$wmiDrives = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType='3'"
$ipv4Address = (Test-Connection -ComputerName $env:COMPUTERNAME -Count 1).IPV4Address.IPAddressToString
$uptime = (Get-Date) - ($wmiOS.ConvertToDateTime($wmiOS.LastBootUpTime))
# Setup object
$result = New-Object -TypeName psobject -Property @{
    Hostname = $($wmiSystem.Name)
    Domain = $($wmiSystem.Domain)
    Username = $($wmiSystem.Username)
    OperatingSystem = $($wmiOS.Caption)
    OSArchitecture = $($wmiOS.OSArchitecture)
    PSVersion = "$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)"
    IPv4Address = $ipv4Address
    Uptime = $uptime
    DriveInfo = $wmiDrives
}

# Overload DriveInfo ToString
$result.DriveInfo = $result.DriveInfo | Add-Member -MemberType ScriptMethod -Name ToString -Value {
    "$($this.DeviceID) $($this.FreeSpace) GB;"
} -Force -PassThru

# Overload Uptime ToString 
$result.Uptime = $result.Uptime | Add-Member -MemberType ScriptMethod -Name ToString -Value {
    "$($this.Days) days, $($this.Hours) hours, $($this.Minutes) minutes, $($this.Seconds) seconds"
} -Force -PassThru

# Overload result ToString
$result = $result | Add-Member -MemberType ScriptMethod -Name ToString -Value {
    $temp = @"
Hostname: $($this.Hostname)
Domain: $($this.Domain)
Console User: $($this.Username)
Operating System: $($this.OperatingSystem)
OS Architecture: $($this.OSArchitecture)
PS Version: $($this.PSVersion)
IPv4 Address: $($this.IPv4Address)
Uptime: $($this.Uptime.ToString())
Free Space: $($this.DriveInfo)
"@
        $temp
    } -Force -PassThru

Everything seems to work fine but the DriveInfo overload. I just get "System.Object[]" as a return from it. What's strange to me is when I just do $result.ToString(), I get the following (see "Free Space"):

Domain: domain.local
Console User: domain\username
Operating System: Microsoft Windows 10 Pro
OS Architecture: 64-bit
PS Version: 5.1
IPv4 Address: 1.1.1.1
Uptime: 2 days, 1 hours, 31 minutes, 49 seconds
Free Space: C: 58721181696; E: 631496687616;

$result.Uptime.ToString():

2 days, 1 hours, 19 minutes, 34 seconds

$result.DriveInfo.ToString():

System.Object[]

I'm probably missing something obvious, but I'm lost at this point. Thanks for any help you can provide in advance!


Solution

  • Can you just try to replace :

    $result.DriveInfo = $result.DriveInfo | Add-Member -MemberType ScriptMethod -Name ToString -Value {"$($this.DeviceID) $($this.FreeSpace) GB;"} -Force -PassThru
    

    By

    $result.DriveInfo | Add-Member -MemberType ScriptMethod -Name ToString -Value {"$($this.DeviceID) $($this.FreeSpace) GB;"} -Force -PassThru
    

    In my understanding the Add-Member apply to each drive, you do not need to affect the result, because you change each objects in the list. After that you can test using :

    $n = 0
    $result.DriveInfo[$n].ToString()