Thursday, August 29, 2013

Create New Secure Store Service Application ID in SharePoint 2010 using PowerShell


Use CreateSecureStoreApplicationID function to new SharePoint Secure Store Application ID using Powershell.

Parameters : 
  1. $sssName - Mandatory - SharePoint Web Url - e.g. http://server:port/ 
  2. $sssFriendlyName - Mandatory - SharePoint Library Name 
  3. $sssContactEmail - Mandatory - File Path on hard drive , e.g. .\abc.xlsx
  4. $sssAdminIdentity
  5. $sssAdminPassword
  6. $defaultServiceContextUrl


function CreateSecureStoreApplicationID
{

[CmdletBinding()]
    Param(
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
    [string]
$sssName,
    [Parameter(Mandatory=$true)]
    [string]
$sssFriendlyName,
    [Parameter(Mandatory=$true)]
    [string]
$sssContactEmail,
    [Parameter(Mandatory=$true)]
     [string]$sssAdminIdentity,
    [Parameter(Mandatory=$true)]
    [string]
$sssAdminPassword,
    [Parameter(Mandatory=$true)]
    [string]
$defaultServiceContextUrl
    )
    


    Start-SPAssignment -Global  
    $SecureStoreServiceInstances = Get-SPServiceInstance | ? {$_.GetType().Equals([Microsoft.Office.SecureStoreService.Server.SecureStoreServiceInstance])}
    $SecureStoreServiceInstance = $SecureStoreServiceInstances | ? {$_.Server.Address -eq $env:COMPUTERNAME}
    If (-not $?)
    {
    Throw " - Failed to find Secure Store service instance"
    }  

    if ($SecureStoreServiceInstance.Status -eq "Disabled")
    {
        Throw "-Secure Store Service Application is disabled"
    }
       
    $defaultServiceContext = Get-SPServiceContext $defaultServiceContextUrl
    try
    {
    $ssApp = Get-SPSecureStoreApplication -ServiceContext $defaultServiceContext -Name $sssName -ea "SilentlyContinue"
    if ($?)
    {
      Write-Host "."
    }
    else
    {
       Write-Host "."
       throw $error[0].Exception
    }
    }
    catch
    {
        $ssApp = $null
    }
    if ($ssApp -ne $null)
    {
    Write-Host -f yellow "Secure Store Service Application $sssName already exists"
    return
    }
    Write-Host -f yellow "Creating new Secure Store Application Id"
    $UserNameField = new-spsecurestoreapplicationfield -name "Windows UserName" -type WindowsUserName -masked:$false
    $PasswordField = new-spsecurestoreapplicationfield -name "Windows Password" -type WindowsPassword -masked:$true
    $fields = $UserNameField, $PasswordField   
   
    $targetApp = new-spsecurestoretargetapplication -Name $sssName -FriendlyName $sssFriendlyName -ContactEmail $sssContactEmail -ApplicationType Group

    $targetAppAdminAccount = New-SPClaimsPrincipal -Identity $sssAdminIdentity -IdentityType WindowsSamAccountName
   
   
    $ownerClaims = New-SPClaimsPrincipal -EncodedClaim "c:0(.s|true"  
   
    $ssApp = new-spsecurestoreapplication -ServiceContext $defaultServiceContext -TargetApplication $targetApp -Administrator $targetAppAdminAccount -Fields $fields -CredentialsOwnerGroup $ownerClaims
      
    # Set credential (username, password) for SSS
    $ssApp = Get-SPSecureStoreApplication -ServiceContext $defaultServiceContext -Name $sssName
    $credSecValue1 = ConvertTo-SecureString $sssAdminIdentity -AsPlainText -Force
    $credSecValue2 = ConvertTo-SecureString $sssAdminPassword -AsPlainText -Force
    $credentialValues = $credSecValue1,$credSecValue2
    write-host -f Green "Success"   
    # Fill in the values for the fields in the target application 
    Write-Host -f yellow "Updating Secure Store Application $sssName Identity Credential Values"
    Update-SPSecureStoreGroupCredentialMapping -Identity $ssApp -Values $credentialValues 
    write-host -f Green "...Success"


    Stop-SPAssignment -Global 

}


Function  Calling -


$sssName = "SecureStoreName"$sssFriendlyName = "Secure Store Friendly Name"
$sssContactEmail = "Email@abc.com"
$sssAdminIdentity = "domain\user"
$sssAdminPassword = "Password"
$defaultServiceContextUrl = "http://server:port/" 
   
CreateSecureStoreApplicationID $sssName $sssFriendlyName $sssContactEmail $sssAdminIdentity $sssAdminPassword $defaultServiceContextUrl


No comments: