function global:Add-FBAMember([parameter(mandatory=$true)][string]$siteCollUrl, [parameter(mandatory=$true)][string]$fbaUser, [parameter(mandatory=$true)][string]$spGroup)
{
# Created by Russell Wright 2015-12-21
# Updated by Russell Wright 2015-12-31 Added AllowUnsafeUpdates
# Must have machine.config <membership>, <roleManager> and <connectionStrings> sections
Add-PSSnapin microsoft.sharepoint.powershell
$web = Get-SPWeb -identity $siteCollUrl
# Save the value of AllowUnsafeUpdates
$allowUnsafeUpdates = $web.AllowUnsafeUpdates
$web.AllowUnsafeUpdates = $true
# EnsureUser initializes the user in SharePoint
$user = $web.EnsureUser($fbaUser)
$getuser = Get-SPUser -Identity $user -Web $siteCollUrl
# Set-SPUser adds the user to the SharePoint group
Set-SPUser -Identity $user -Web $siteCollUrl -Group $spGroup
$web.AllowUnsafeUpdates=$allowUnsafeUpdate
$retMsg = "Added user: " + $fbaUser + " to group " + $spGroup + " in " + $siteCollUrl
$retMsg
}
We needed a PowerShell Function to call from Winshuttle Workflow that would ensure the user was in SharePoint after a SQL load to the aspnetdb. The trick appears to be using "AllowUnsafeUpdates," similar to what many others have posted with their C# code. There were also some items noted in the config files where the machine.config required the FBA connection string in order for the PowerShell to work.
0 Responses to “PowerShell to Add FBA User to SharePoint Group and EnsureUser”