Pipe group to Set-PnPListItemPermission powershell cmdlet
-
08-01-2021 - |
Domanda
Following one-liner failed
Get-PnPGroup | where-Object {$_.title -eq 'test account'} | Set-PnPListItemPermission -Identity 1 -List "Accounts" -AddRole "Read" -Group $_.id
yielding error
Set-PnPListItemPermission : Cannot bind argument to parameter 'Group' because it is null.
At line:1 char:139
+ ... "Read" -Group $_.id
+ ~~~~~
+ CategoryInfo : InvalidData: (:) [Set-PnPListItemPermission], ParameterBindingV
alidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,SharePointPnP.Po
werShell.Commands.Lists.SetListItemPermission
However, by breaking the pipe into 2 commands it works
$group = Get-PnPGroup | where-Object {$_.title -eq 'test account'}
Set-PnPListItemPermission -Identity 1 -List "Accounts" -AddRole "Read" -Group $group.id
How to run the command in one pipeline?
Soluzione
You're missing a foreach-object around Set-PnPListItemPermission
Get-PnPGroup | where-Object {$_.title -eq 'test account'} | Foreach-Object {Set-PnPListItemPermission -Identity 1 -List "Accounts" -AddRole "Read" -Group $_.id}
But also be aware that the only thing you gain by putting it on one line is:
- Harder to debug
- Slower execution
- Not potentially overwriting variable
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange