header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
How to access PSobject properties within the PSObject?
Last Post 02 May 2011 12:55 AM by Ginolard. 5 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
GinolardUser is Offline
New Member
New Member
Posts:46
Avatar

--
26 Apr 2011 11:42 PM
    Maybe I haven't explained it very well in the title, so here's an example

    foreach ( $csv in $csvs )
    {
    Echo "Processing Cluster Shared Volume $n"
    $Signature = ( $csv | Get-ClusterParameter DiskSignature ).Value.substring(2)

    $obj = New-Object PSObject -Property @{
    Name = $csv.Name
    CSVPath = ( $csv | select -Property Name -ExpandProperty SharedVolumeInfo).FriendlyVolumeName
    LUN = ( Get-WmiObject Win32_DiskDrive | Where { "{0:x}" -f $_.Signature -eq $Signature } ).SCSILogicalUnit
    PhysicalDisk = ( Get-WmiObject Win32_DiskDrive | Where { "{0:x}" -f $_.Signature -eq $Signature } ).DeviceID.substring(4)
    }
    ($obj).PhysicalDisk = ($obj).PhysicalDisk -Replace "PHYSICALDRIVE", "Physical Disk "
    $objs += $obj
    $n++
    }

    What I want to do is add another property called LUNPath with the following code


    LUNPath = (get-nalun | where {(get-nalunmap $_).lunid -eq LUN} |select path)

    However, my experience with the PSObject is limited so I'm not sure if you can create a new property that references a previous one
    JonathanUser is Offline
    Basic Member
    Basic Member
    Posts:109
    Avatar

    --
    27 Apr 2011 01:03 PM
    If I am reading what you want to do correctly, you would access it by the following by "this.LUN". The "this" object refers back to the current PSObject, and then the .LUN property of "this". Hope that helps.
    Jonathan Tyler
    http://powershellreflections.wordpress.com
    Follow Me On Twitter
    GinolardUser is Offline
    New Member
    New Member
    Posts:46
    Avatar

    --
    27 Apr 2011 10:04 PM
    I just tried trhe following variations, none of which worked. I guess I've misunderstood ;)

    LUNPath = (get-nalun | where {(get-nalunmap $_).lunid -eq "this.lun"} |select path)
    LUNPath = (get-nalun | where {(get-nalunmap $_).lunid -eq this.lun} |select path)
    LUNPath = (get-nalun | where {(get-nalunmap $_).lunid -eq "$this.lun"} |select path)
    LUNPath = (get-nalun | where {(get-nalunmap $_).lunid -eq $this.lun} |select path)
    GinolardUser is Offline
    New Member
    New Member
    Posts:46
    Avatar

    --
    29 Apr 2011 06:23 AM
    Quick bump in the hope someone can help
    PoshoholicUser is Offline
    PowerShell MVP, Community Director
    Basic Member
    Basic Member
    Posts:111
    Avatar

    --
    29 Apr 2011 07:48 AM
    To add a property that references another property, you need to use Add-Member to add a member of type ScriptProperty.  ScriptProperty members are evaluated when they are referenced, so they can use information in other members or methods.  For example, assuming that the LUN property had a child property called Path, you could expose it like this:

    $obj = New-Object PSObject -Property @{
        Name = $csv.Name
        CSVPath = ( $csv | select -Property Name -ExpandProperty SharedVolumeInfo).FriendlyVolumeName
        LUN = ( Get-WmiObject Win32_DiskDrive | Where { "{0:x}" -f $_.Signature -eq $Signature } ).SCSILogicalUnit PhysicalDisk = ( Get-WmiObject Win32_DiskDrive | Where { "{0:x}" -f $_.Signature -eq $Signature } ).DeviceID.substring(4)
    }
    $obj | Add-Member -MemberType ScriptProperty -Name LUNPath -Value {$this.LUN.Path}

    Just make sure you use $this inside the script block that defines the ScriptProperty member when you want to refer to the object itself.

    Does that help?
    Kirk Munro [MVP]
    Poshoholic

    My blog: http://poshoholic.com
    Follow me on Twitter: http://twitter.com/poshoholic
    GinolardUser is Offline
    New Member
    New Member
    Posts:46
    Avatar

    --
    02 May 2011 12:55 AM
    Kirk,

    OK. I can see how using the Add-Member cmdlet is the right approach. However, that line gives me a "Missing '=' operator after key in hash literal." if I put it inside the PSObject scriptblock
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer