Inconsistent results from select-object cmdlet


i've been tearing hair out on issue , wondering if provide insight occurring.

i trying select subset of properties using select-object command. subset of properties stored in array. when create array directly within powershell console works fine, when import array of properties csv file error

select-object : cannot convert system.management.automation.psobject 1 of following types {system.string, system.management.automation.scriptblock}.

despite both arays being same (according compare-object).

example code , output (simplified original code same error)

# create objects

#create objects  $prop = @{c='ca';cn='fred'}  $f = new-object psobject -property   $prop$mb = @($f)  $prop = @{c='us';cn='tom'}  $f = new-object psobject -property $prop$mb += $f
 


#check output of array of objects  $mb

cn                                                          c
--                                                          -
fred                                                        ca
tom                                                         us

# create property arrays  $propfromarray = @('c';'cn')  $propfromcsv = import-csv .\phoneexportfields1.csv  $propfromcsv =$propfromcsv | select -expandproperty fieldname

# check property arrays

$propfromarray

c
cn

$propfromcsv

c
cn


#compare 2 objects  compare-object $propfromarray $propfromcsv -includeequal

inputobject                                                 sideindicator
-----------                                                 -------------
c                                                           ==
cn                                                          ==

# try selecting data need  $mb | select -property $propfromarray

c                                                           cn
-                                                           --
ca                                                          fred
                                                         tom

!works fine

using other method

$mb | select -property $propfromcsv


select-object : cannot convert system.management.automation.psobject 1 of following types {system.string, syst
em.management.automation.scriptblock}.
@ line:1 char:14
+  $mb | select <<<<  -property $propfromcsv
    + categoryinfo          : invalidargument: (:) [select-object], notsupportedexception
    + fullyqualifiederrorid : dictionarykeyunknowntype,microsoft.powershell.commands.selectobjectcommand

any appreciated.

i don't know why,but occur system.management.automation.pipelinestoppedexception: pipeline has been stopped. -only powershell v2

but can use foreach instead of select: import-csv .\phoneexportfields1.csv| %{$_.fieldname}

the exception thrown when cmdlet or windows powershell provider attempts write pipeline or perform number of other windows powershell operations after pipeline has been terminated. pipeline have been terminated before call made or during call.

in the powershell v3 works needed.




Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

some help on Event 540

WMI Repository 4GB limit - Win 2003 Ent Question

Event ID 1302 (error 1307) DFS replication service encountered an error while writing to the debug log file