Avatar of rookie_b
rookie_b
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Powershell Custom Object Properties Basics Question

I often come across a situation where I need an extra property in the object, and I understand one of the ways to create a note property is to use select-object, rather than add-member, or pscustomobject. My problem is I keep getting an error when I try to set that property, that the property doesn't exist or cannot be set, so clearly I am getting  something basic wrong.

For example, I would like to include the path to an acl object access export, but keep getting an error:

$paths = "\\server\share\folder1","\\server\share\folder2","\\server\share\folder3"

$report = @()
$paths |

foreach-object {
$access =  (get-acl $_).access |select-object *,path
$access.path = $_
$report+=$access
}

$report | export-csv -path report.csv -NoTypeInformation

Open in new window


I can use custom object  to achieve that, but I was hoping to save myself some time writing out hash tables by instead using select-object to define the custom property .
Do I need to use new-object?

PowershellWindows OS

Avatar of undefined
Last Comment
Robert

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Zvonko

as easy as this:
$paths = "\\server\share\folder1","\\server\share\folder2","\\server\share\folder3"

$report = @()
$paths |

foreach-object {
   write-host $_
$access =  (get-acl $_).access |select-object *,path=$_
write-host $access
#$access.path = $_
#$access | Add-Member -NotePropertyName refPath -NotePropertyValue $_
$report+=$access
}

$report | export-csv -path report.csv -NoTypeInformation

Open in new window



Robert

You would do it something like the following.
By adding a member to the array it will allow you to include that data in the export.

$paths = "\\server\share\folder1","\\server\share\folder2","\\server\share\folder3"

$report = @()
$paths |

foreach-object {

$access =  (get-acl $_).access
$access | Add-Member -NotePropertyName "Path" -NotePropertyValue $_ 
$report+=$access
}

$report | export-csv -path report.csv -NoTypeInformation

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck