Home > OS >  Powershell script throwing System.Management.Automation.PSMethod error on SetAccessRule
Powershell script throwing System.Management.Automation.PSMethod error on SetAccessRule

Time:10-29

I'm getting this error in my script when I use AddAccessRule:

Exception setting "AddAccessRule": "Cannot set the Value property for PSMemberInfo object of type "System.Management.Automation.PSMethod"."

I have researched lots of similar scripts, but I'm having no luck searching on this error. Any help would be greatly appreciated! Thanks!

Script below:

'''

$folders = get-childitem -path "C:\Users\spewingadmin\Documents\reports_bak2\" -Recurse

Foreach($folder in $folders){
$foldacl = get-acl -Path $folder.FullName
$InheritanceFlag = $foldacl.Access.InheritanceFlags
$PropagationFlag = $foldacl.Access.PropagationFlags
$objType = $foldacl.Access.AccessControlType
$Permissions = $foldacl.Access.FileSystemRights

For ($x=0;$x -lt $foldacl.Access.Count;$x  ){
    $user = $foldacl.Access.IdentityReference[$x]
    $domainresult = $user.Value.substring(0,($user.Value.indexof("\")))

    If(($domainresult -eq "WORKGROUP")) {
        $username = $user.Value.substring((($user.Value.indexof("\"))   1),($user.Value.length - $user.Value.indexof("\") - 1))
        $DomainUser = "NEWWORKGROUP\"   $username
        $NewPerms = ($domainuser,$Permissions,$InheritanceFlag,$PropagationFlag,$objType)
        $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule -ArgumentList $NewPerms
        $Ar
        $foldacl.SetAccessRule=($Ar)
        
    }
}
##Set-acl -Path $folder.FullName -AclObject $foldacl
##$foldacl
} 

'''

CodePudding user response:

Yes you can return a string no problem, but it is quite finicky setting it up. It is hard to tell what’s happening without seeing the parameters, script, variables, etc.

Is the powershell script itself setup to output a string? Double check that the names are correct in the PowerShellVariables pop-up window and that the direction is set to out and that the type is string and you are setting the value to the correct UiPath variable

CodePudding user response:

diopside has provided the crucial pointer in a comment on the question:

You have an extraneous = in the following attempt at a method call, and removing it fixed your problem, as you've confirmed:

# Note the extraneous "="
$foldacl.SetAccessRule=($Ar)  # !! WRONG

should be:

# OK - syntactically correct method call.
$foldacl.SetAccessRule($Ar)

Therefore, your question qualifies for closure based on the standard reason "Not reproducible or was caused by a typo".

However, given the somewhat obscure nature of the error message - Cannot set the Value property for PSMemberInfo object of type "System.Management.Automation.PSMethod". - perhaps an explanation is helpful:

PS> 'foo'.ToString # Note: *No* () to actually *call* the .ToString() method.

OverloadDefinitions
-------------------
string ToString()
string ToString(System.IFormatProvider provider)
string IConvertible.ToString(System.IFormatProvider provider)
  • Trying to assign to such a method-signature object (group) - which doesn't make any sense - then results in the aforementioned error message:
PS> 'foo'.ToString = 'whatever' # !! Predictably fails.

[...] Exception setting "ToString": "Cannot set the Value property for PSMemberInfo object [...]"
  • Related