Hi,
Using the following command
Add-SPShellAdmin -username DOMAIN\YourUser I have added user as admin to powersehll script. But while executing my script it is throwing cannot perform opeartion on null valued exression. Can any one help on this.Below is my script.
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source=ESESSMW1762;Initial Catalog=Ericsson1_MSCRM;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "select eri_spcclmsiteurl from Account where eri_regionid is null"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet= $null
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
try
{
IF( $DataSet.Tables[0].Rows.Count -gt 0)
{
# For Each Contract Folder
$sharepointAppUrl = $null
FOREACH ($ContractFolder IN $DataSet.Tables[0].Rows)
{
$sharepointAppUrl =$ContractFolder[0] ;
#Adding Sharepoiont dll
if ( -not $(Get-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue" ))
{Add-PSSnapin Microsoft.SharePoint.PowerShell}
$Runsite = $null
[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
$Runsite = New-Object Microsoft.SharePoint.SPSite ($ContractFolder[0])
});
If($Runsite -ne $null)
{
$oSite = new-object Microsoft.SharePoint.SPSite([Guid]$Runsite.ID,$Runsite.SystemAccount.UserToken)
}
Add-Content $LogFile "Processing the Contract :: $oSite"
$ListName =$docLibraryName;
$web =$oSite.OpenWeb(); #this will open the correct web based on the url above
$doclib=[Microsoft.SharePoint.SPDocumentLibrary]$web.Lists["contract"]
$foldercoll=$doclib.Folders;
foreach($folder in $foldercoll) {
$folder.ResetRoleInheritance();
$folder.BreakRoleInheritance($false,$true);
$folder.update();
ForEach ($SiteGroup in $web.SiteGroups) {
$FolderGroup=$folder.name+"_group"
if ($SiteGroup.Name -match "CustomerDocuments" -or $SiteGroup.Name -like $FolderGroup)
{
#write-host $SiteGroup.Name
$GroupName = $SiteGroup.Name
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($SiteGroup)
$roleDefinition = $web.RoleDefinitions[$PermissionLevel];
$roleAssignment.RoleDefinitionBindings.Add($roleDefinition);
$folder.RoleAssignments.Add($roleAssignment)
$folder.update();
Add-Content $LogFile "DOne for the Folder $folder.name :: $oSite"
}
}
}
}
}
}