Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

How is this function works? Powershell

$
0
0

Hello,I need help because it seems i have a basic missunderstanding about functions.

I have this function:

Function Rights ($user, $ShortListName, $permission){
$site = new-object Microsoft.SharePoint.SPSite("http://spteszt")
$spweb = $site.Openweb()
$userName = $spweb.EnsureUser($User)
$spFolder = $spweb.lists[$ShortListname]
if($spweb.SiteUsers[$userName])
	{
        $assignment=new-object Microsoft.SharePoint.SPRoleAssignment($spweb.SiteUsers[$userName])
        $assignment.RoleDefinitionBindings.Add($spweb.RoleDefinitions[$permission])
        $spfolder.BreakRoleInheritance($false, $false)
        $spfolder.RoleAssignments.add($assignment);
        Write-Host "Permission provided for user ", $userName
	$spfolder.update()
	}
 else{
      Write-Host "User ""$userName"" was not found in this web!"
    }
}

And in the srcipt i call this as:

$ShortListName = "shortnm"

Rights("Domain\sptest_group" ,$ShortListName, "Reader")

When the script reach this part it fail.

Exception calling "EnsureUser" with "1" argument(s): "The specified user Domain\sptest_group shortnm Reader could not be f
ound."
At line:4 char:30+ $userName = $spweb.EnsureUser <<<< ($User)+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException+ FullyQualifiedErrorId : DotNetMethodException

Index operation failed; the array index evaluated to null.
At line:5 char:26
+ $spFolder = $spweb.lists[ <<<< $ShortListname]+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException+ FullyQualifiedErrorId : NullArrayIndex

Index operation failed; the array index evaluated to null.
At line:6 char:21
+ if($spweb.SiteUsers[ <<<< $userName])+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException+ FullyQualifiedErrorId : NullArrayIndex

If i give the parameters manually inside the function, then it works. So this is why i think i invoke this function in a wrong way.


I Hate Mondays




Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>