简体   繁体   中英

Returning string of a function as an Add-Member value parameter in PowerShell

I'm trying to create a Powershell script that shows the folder permissions and the members of the permission groups. I have a function called "Get-Members" that returns (as a comma separated string) the members of the group that has sent to the function as an argument.

Now I'd like to know how i can use the returning string with the Add-Member's value parameter. How can i use the function with that? I tried

Add-Member -MemberType NoteProperty -Name "Members" -Value Get-Members($_.IdentityReference) -PassThru

but it doesn't seem to be working.

Here's the whole thing:

($root | get-acl).Access | Add-Member -MemberType NoteProperty -Name "Members" -Value Get-Members($_.IdentityReference) -PassThru  | Add-Member -MemberType NoteProperty -Name "Folder" -Value $($root.fullname).ToString()  -PassThru | select -Property Path, IdentityReference, FileSystemRights

And here's the function:

Function Get-Members {
    param( [string]$group )

    $xyz=$group
    if ($group -match '\\')
    {
        $xyz=$group -creplace '^[^\\]*\\', ''
    }

    $Group = [ADSI]"LDAP://cn=$xyz,ou=SecurityGroups,ou=Accounting,ou=Services,dc=CONTOSO,dc=ny,dc=local"

$Members = $Group.Member | ForEach-Object {[ADSI]"LDAP://$_"}

$combined = $Members | select -ExpandProperty name

$result= $combined -join ","

return $result

}

How can I get this to work?

Your brackets are wrong. Try this:

Add-Member -MemberType NoteProperty -Name "Members" -Value (Get-Members $_.IdentityReference) -PassThru

Which would make the Get-Members part execute first and return its values to the -Value property due to the brackets.

As an aside, i'd recommend you choose a different name over Get-Members because its too close to the very well known Get-Member cmdlet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM