locked
PowerShell 2.0 vs 4.0... What am i doing wrong (accessing objects properties) RRS feed

  • Question

  • Hi Guys,
    This is a newbee question,

    I started scripting in powershell around 4 months ago. 
    I created a script in 4.0. when i try to port it to 2.0 I notice that i can't seem to access any data using the ".propertyname" 

    somehow in certain cases it works...
    in some of my code, the $memspec variable is an Array, in other circumstances it as pscustomobject. When it is a pscustomobject i can access the ".properties" but i'm not sure why or how it decides what to use? (maybe this has nothing to do with it)

    Is this normal?  what am I doing wrong??

    Here are some examples..

    $memspec=get-wmiobject -class win32_physicalmemory | select-object -property speed,capacity
    $memspec.speed # does not work in powershell 2.0??
    $memspec | foreach {$_.speed} # Works in Powershell 2.0
    
    # does not work in 2.0
    [array]$arr= $null
    $pso= new-object psobject
    $pso | Add-Member NoteProperty fname "John"
    $pso | Add-Member NoteProperty lname "Snow"
    $arr+=$pso
    $arr.fname
    
    #does work in 2.0
    function get-name
    {
     [array]$arr= $null
     $pso= new-object psobject
     $pso | Add-Member NoteProperty fname "John"
     $pso | Add-Member NoteProperty lname "Snow"
     $arr+=$pso
     $arr
    }
    
    $name=get-name
    $name.fname 
    
    # does not work in 2.0
    function get-names 
    {
     [array]$arr= $null
     $pso= new-object psobject
     $pso | Add-Member NoteProperty fname "John"
     $pso | Add-Member NoteProperty lname "Snow"
     $arr+=$pso
     $pso= new-object psobject
     $pso | Add-Member NoteProperty fname "Arya"
     $pso | Add-Member NoteProperty lname "Stark"
     $arr+=$pso
     $arr
    }
    
    $names=get-names
    $names.fname 
    
    
    


    Anthony Cartier-Info

    Monday, June 22, 2015 1:27 PM

Answers

  • That feature is available in V3 and later.  In V2 and later do this:
    $names=get-names
    $names | %{$_.
    fname}


    \_(ツ)_/

    • Marked as answer by Anthony A Friday, June 26, 2015 1:23 PM
    Monday, June 22, 2015 1:55 PM

All replies

  • That feature is available in V3 and later.  In V2 and later do this:
    $names=get-names
    $names | %{$_.
    fname}


    \_(ツ)_/

    • Marked as answer by Anthony A Friday, June 26, 2015 1:23 PM
    Monday, June 22, 2015 1:55 PM
  • In V2 and later use this:

     $names=@(new-object psobject -property @{fname="John";lname="Snow"})
     $names+=new-object psobject -property @{fname="Arya";lname="Stark"}
     $names | %{$_.fname}
    

    Much easier to type and much more flexible.


    \_(ツ)_/

    Monday, June 22, 2015 1:58 PM
  • Good To know!

    I quickly overlooked microsoft site on "whats new with powershell 4" and didnt see anything on this.

    maybe i should have checked whats new on powershell 3?

    Anyhow... now i know to be carrefull with syntax when working on a multi platform scripts!


    Anthony Cartier-Info

    Friday, June 26, 2015 1:23 PM
  • Alternatively (single line):


    Get-Names | select-object -expandproperty fname
    


    -- Bill Stewart [Bill_Stewart]

    Friday, June 26, 2015 1:56 PM