1

I set up SharePoint 2013 with FBA users (SQL). I'd like to know if it's possible to create new users using PowerShell script ?

What I tried but did not work :

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "C:\inetpub\wwwroot\wss\VirtualDirectories\mysite\web.config")
[System.Reflection.Assembly]::LoadWithPartialName("System.Web")
[System.Web.Security.Membership]::Providers

But the last command only shows me the default AspnetSqlMembershipProvider ... and not my custom one (web.config is configured with a custom Membership provider, and FBA SQL Authentication is working).

  • did my answer help you at all? – jpollar Jun 25 '15 at 14:17
4
+50

Nico,

I found a source that may help you immensely. The following link walks you through how to use a PowerShell script to import a list of users from Excel and create them for FBA.

Importing and Exporting Data in Extranet Collaboration Manager (ExCM) 2010

You can also download the PowerShell source code from this link.

It looks like the code creating the user account is as follows.

# Import user
$status = [System.Web.Security.MembershipCreateStatus]::Success
$extranetUser = $membershipProvider.CreateUser($username, $password, $email, $passwordQuestion, $passwordAnswer, $true, $null, [ref]$status)

You may need to modify the code to fit your needs, but it should get you pointed in the right direction. Let me know if you need further assistance. I'm eager to help.

Jim

  • Thanks for your help. I apologize for the delay, I've been busy a lot. This must work and I'm gonna give a try soon ! Thanks @jpollar – Nico Jun 28 '15 at 16:22
  • Let me know if you have trouble implementing it. – jpollar Jun 28 '15 at 20:02
  • Ok, I have problems while implementing it. When I execute this code $membershipProviderName = "SQLMembership" $membershipProvider = [System.Web.Security.Membership]::Providers[$membershipProviderName] When I want to use $membershipProvider nothing happens. I tried a full execution, but user was not created. I have doubts on LoadingAssembly at the beginning, maybe it comes from that ? – Nico Jun 29 '15 at 12:10
  • I only get a "Cannot invoke null parameter" on the line $membershipProvider.CreateUser(...), any ideas ? – Nico Jun 29 '15 at 12:17
  • I guess this is because [System.Web.Security.Membership]::Providers only returns the default provider ... And not the one I newly created whereas I can see it with Get-SPAuthenticationProvider and from IIS – Nico Jun 29 '15 at 12:58
0

I've just start looking into creating FBA users through PowerShell, I found that if I used the SharePoint Management Shell the only membership provider that was returned was the default provider, if I ran the same commands in an elevated PowerShell window then I got all of the membership providers including my custom one.

So do not use the SharePoint Management Shell just a regular PowerShell window.

0

I solved the issue of not being able to set the CurrentDomain APP_CONFIG_FILE. Which was why his original commands did not work. When PowerShell loads the default sql database is already loaded in the APP_CONFIG_FILE. When you use SetData it doesn't overwrite, but adds it as a second connectionString. When you look for providers only the first connectionString for default gets used. So we have to remove the first connectionString. Here is all the code that will add a user to FBA in 2013.

*You will need to change $configurationFile to reflect your directory.

*You will need to change $membershipProvider to reflect your membershipprovider name that you set in central admin->application management->Mange Web Applications -> Authentication Providers. You already know this if you set up FBA...

*You need to run as admin, and it "MAY" only work in regular 64-bit Windows PowerShell. I run it in both regular and SharePoint PowerShell though.

echo "Creating SP Users"
#Load Assemblies for SharePoint and PowerShell
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.PowerShell")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Configuration")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Web")

#Remove the default APP_CONFIG_FILE value this allows you to get the correct Membership provider
Add-Type -AssemblyName System.Configuration
[Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
[Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"})[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null)
[Configuration.ConfigurationManager]::ConnectionStrings[0].Name

$configurationFile = "C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config"
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configurationFile)

$membershipProviderName = "FBAMembership"
$membershipProvider = [System.Web.Security.Membership]::Providers[$membershipProviderName]


if($membershipProvider -eq $null)
{
    throw "The membership provider '$membershipProviderName' could not be found."
}

$status = [System.Web.Security.MembershipCreateStatus]::Success
$newMember = $membershipProvider.CreateUser("dtest.user", "!Q@W#E$R%T1q2w3e4r5t", "dtest.user@mail.mil", "test", "test", $true, $null, [ref]$status)

#echo $newMember

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.