whorsfall
asked on
Powershell Enumerate memebers of a PSObject
Hi,
I am trying to work out how to write a powershell function that can enumerate all members of a PSObject and print the Member name and its value.
So if an object has 3 properties then I might see an output like the following:
Employee_Name : Fred
Employee_Age : 18
Employee_Sex : Male
So I am trying to write really a generic function that can handle any PSOBject (or other powershell object) and work it out.
In this simple example I have provided some code.
I know C# Reflection and was wondering how powershell would do it.
So in the example below in fnTest I would like to have something like:
function fnTest($data)
{
foreach ($item in $data)
{
write-host $item.Label: $item.Value
}
}
Thanks,
Ward.
I am trying to work out how to write a powershell function that can enumerate all members of a PSObject and print the Member name and its value.
So if an object has 3 properties then I might see an output like the following:
Employee_Name : Fred
Employee_Age : 18
Employee_Sex : Male
So I am trying to write really a generic function that can handle any PSOBject (or other powershell object) and work it out.
In this simple example I have provided some code.
I know C# Reflection and was wondering how powershell would do it.
So in the example below in fnTest I would like to have something like:
function fnTest($data)
{
foreach ($item in $data)
{
write-host $item.Label: $item.Value
}
}
Thanks,
Ward.
function fnTest($data)
{
# Here is want to seperate the Item Name and its value
}
$employee_list = @();
$employee1 = New-Object PSObject
$employee1 | Add-Member Noteproperty Employee_Name -Value "Fred"
$employee1 | Add-Member Noteproperty Employee_Age -Value 18
$employee1 | Add-Member Noteproperty Employee_Sex -Value "Male"
$employee_list += $employee1
$employee2 = New-Object PSObject
$employee2 | Add-Member Noteproperty Employee_Name -Value "John"
$employee2 | Add-Member Noteproperty Employee_Age -Value 22
$employee2 | Add-Member Noteproperty Employee_Sex -Value "Male"
$employee_list += $employee2
$employee3 = New-Object PSObject
$employee3 | Add-Member Noteproperty Employee_Name -Value "Jane"
$employee3 | Add-Member Noteproperty Employee_Age -Value 32
$employee3 | Add-Member Noteproperty Employee_Sex -Value "Female"
$employee_list += $employee3
foreach ($employee in $employee_list)
{
fnTest $employee # <-- Function to enumerate object properties.
}
ASKER
Hi,
Thanks for your response . I think u misunderstood my question a bit. I probably should of phrased it better. I wanted the fnTest function to be able to handle any type of PSObject passed to it and be able to enumerate the members.
So if I passed a different type of object with different members eg say "car_type" and "car_color" it can enumerate the $data variable within the fnTest function.
So fnTest can handle any type of object passed to it.
Sorry for the confusion
Ward
Thanks for your response . I think u misunderstood my question a bit. I probably should of phrased it better. I wanted the fnTest function to be able to handle any type of PSObject passed to it and be able to enumerate the members.
So if I passed a different type of object with different members eg say "car_type" and "car_color" it can enumerate the $data variable within the fnTest function.
So fnTest can handle any type of object passed to it.
Sorry for the confusion
Ward
There may better ways
but this work for your example
use it like this
detailobject $employee_list
or
detailobject (get-process)
but this work for your example
use it like this
detailobject $employee_list
or
detailobject (get-process)
function detailObject($object)
{
$Properties = $object | gm -type Property,noteproperty
$Pcount = $properties.count
$counter = 0
While ( $counter -lt $pcount)
{ $name = ($properties[$counter].name)
$value = $object[$counter].$name
Write-host "name : $name Value: $Value"
$counter++
}
}
Any reason you do not just use format-list * ?
Yeah I don't know why you wouldn't just use Select-Object * or Format-List *. These will both achieve what you want. If you did want a function to do it though, I guess you would have a reason...
Function fnTest($data)
{
$arrProperties = $data | GM -MemberType Properties | Select -ExpandProperty Name
Foreach ($property in $arrProperties)
{
Write-Host "$($property): $($data.$property)"
}
}
But pretty much the output will be the same as just doing a format-list.
ASKER
Hi,
Thanks for your answers. The reason why I am not using the standard methods is I posted this a more simple example.
What I wanted to do in the fnTest() function later exapand it so it can automate excel using COM.
So the first row would output the columns heading then below that I would put the members each data item into to each cell.
So the first row would be headings, the next row would be employee1 and the next row
employee2 and so on.
So in the modified version I might pass the variable $employee_list.
The excel com objects need me to fill the values cell by cell.
So I could not work out how to break up each item one by one.
However if there is a better way please let me know.
Thanks,
Ward.
e.g:
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$workbook1 = $excel.Workbooks.Add()
# write into cell B5 (column 2, line 5):
$workbook1.ActiveSheet.Cel ls.Item(5, 2)= "I can write directly to cells!"
# read cell content
$content = $workbook1.ActiveSheet.Cel ls.Item(5, 2).Text
"Cell B5 content: $content"
</code>
Thanks for your answers. The reason why I am not using the standard methods is I posted this a more simple example.
What I wanted to do in the fnTest() function later exapand it so it can automate excel using COM.
So the first row would output the columns heading then below that I would put the members each data item into to each cell.
So the first row would be headings, the next row would be employee1 and the next row
employee2 and so on.
So in the modified version I might pass the variable $employee_list.
The excel com objects need me to fill the values cell by cell.
So I could not work out how to break up each item one by one.
However if there is a better way please let me know.
Thanks,
Ward.
e.g:
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$workbook1 = $excel.Workbooks.Add()
# write into cell B5 (column 2, line 5):
$workbook1.ActiveSheet.Cel
# read cell content
$content = $workbook1.ActiveSheet.Cel
"Cell B5 content: $content"
</code>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awsome - thanks :)
you example is almost correct
Open in new window
I hope this helps
joe