SharePoint + PowerShell: Batch Import Models, Create ECT’s, Set Permissions

Here is a script that will batch import Models, create associated ECT’s, and set permissions for users and admins. Please modify the variables as needed.

# script from netnerds.net
# Make sure the namespace is the same as the namespace in your exported bdcm files
# My $siteURL happens to be the same as my $namespace. That may not be the case for you.
# Do this only on dev machines
# I’m running this on the SharePoint 2010 server so I made my servicecontext localhost
#
# What this does:
# Scans current directory for BDC Export(.bdcm) files
# Creates the External System/Lob and the External Content Types
# Creates External Lists from those ECT’s
# Sets the proper permissions

$adminGroup = “DOMAIN\SharePoint Admins”
$userGroup = “DOMAIN\SharePoint Users”
$serviceContext = “http://localhost/”
$nameSpace = “http://sharepoint/”
$SiteUrl = $nameSpace

$bdc = Get-SPBusinessDataCatalogMetadataObject -BdcObjectType Catalog -ServiceContext $serviceContext
$pathtobdcmfiles = Get-Location
$importFiles = Get-Childitem -path $pathtobdcmfiles | Where {$_.extension -eq “.bdcm” -and $_.basename -ne “catalog”}

foreach ($file in $importFiles) {
Import-SPBusinessDataCatalogModel -Path $file.FullName -Identity $bdc -force -ModelsIncluded -PropertiesIncluded -PermissionsIncluded -Verbose

# THIS WHOLE CHUNK COMES FROM: http://gallery.technet.microsoft.com/scriptcenter/82437789-f294-4bcd-8210-a1ba0e081f82/
# IT MAKES THE EXTERNAL LISTS FROM THE MODELS. AWESOME.
$Model = Get-SPBusinessDataCatalogMetadataObject -BdcObjectType Model -Name $file.baseName -ServiceContext $ServiceContext

$ListUrl = “Lists/{0}”

ForEach ($Entity in $Model.AllEntities)
{
$ns = $Entity.Namespace
Write-Host “Entity Namespace : $ns”
$name = $entity.Name
Write-Host “Entity Name : $name”

Write-Host “Looking for MethodInstance specific finder…”
$sf = $Entity.MethodInstances | Where-Object{ $_.MethodInstanceType -eq [Microsoft.BusinessData.MetadataModel.MethodInstanceType]::SpecificFinder }
If($sf -eq $null)
{
Write-Host “Skipping external list creation as Method Instance of Type SpecificFinder was not found in Entity : $name”
break
}
else
{
Write-Host “Creating and Configuring SPListDataSource”
$ds = New-Object -TypeName Microsoft.SharePoint.SPListDataSource
$instanceName = [String]::Empty
ForEach($instance in $entity.LobSystem.LobSystemInstances){$instanceName = $Instance.Name}

$ds.SetProperty(“LobSystemInstance”, $instanceName)
$ds.SetProperty(“EntityNamespace”, $ns)
$ds.SetProperty(“Entity”, $name)
$ds.SetProperty(“SpecificFinder”, $sf.Name)

$site = $null
Try
{
$site = Get-SPSite -Identity $SiteUrl
$web = $site.OpenWeb()
$web.Lists.Add($Entity.Name, “”, [String]::Format($ListUrl, $Entity.Name), $ds);
}
Finally
{
$web.Dispose()
$site.Dispose()
}
}
}
#END CHUNK
}

$claimAdmin = New-SPClaimsPrincipal -Identity $adminGroup -IdentityType WindowsSamAccountName
$claimUsers = New-SPClaimsPrincipal -Identity $userGroup -IdentityType WindowsSamAccountName

Grant-SPBusinessDataCatalogMetadataObject -Identity $bdc -Principal $claimAdmin -Right “Execute,SetPermissions,Edit,SelectableInClients”
Grant-SPBusinessDataCatalogMetadataObject -Identity $bdc -Principal $claimUsers -Right “Execute,Edit,SelectableInClients”

Copy-SPBusinessDataCatalogAclToChildren -MetadataObject $bdc

#Ignore this, its what I wish my namespace was…
#$nameSpace = “http://$env:USERDNSDOMAIN/”.toLower()
 
As they say in the SuSE Linux motd: Have a lot of fun!

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, holds a master's degree in Systems Engineering and is coauthor of Learn dbatools in a Month of Lunches. Chrissy is certified in SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Posted in PowerShell, SharePoint

Leave a Reply