Sunday, July 19, 2015

Run userprofile synchronization service through powershell




Below is a way to run userprofile service through powershell. This script wait for the completion of user profile service execution and let user know when it completes.


param(
 [string]$siteUrl
    )

function RunUserProfileSynchronization()
{     
   try 
   {
        $site= New-Object  Microsoft.SharePoint.SPSite($siteUrl)
        $serviceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site)
        $configManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($serviceContext)

        if($configManager.IsSynchronizationRunning() -eq $false)
        {
            [DateTime] $lastoverallendtime= $configManager.GetSynchronizationStatus().LastOverallEndTime;
    
            Write-Host "Starting user profile synchronization"
            $configManager.StartSynchronization($false);
            Write-Host "User Profile synchronization is started"
            Write-Host "########################################################################";

            while($true)
            {
                Write-Host "Please wait it is still running............. "
                if($configManager.GetSynchronizationStatus().LastOverallEndTime -ne $null)
                {
                    # Below wait period is important to avoid SQL query threshhold
                    Start-Sleep -s 10   
                     if ($configManager.GetSynchronizationStatus().LastOverallEndTime -gt $lastoverallendtime)
                    {
                        Write-Host "Synchronization service completed below is overall status:- "
                        Write-Host "Lastoverallstarttime" $configManager.GetSynchronizationStatus().LastOverallStartTime;
                        Write-Host "Lastoverallendtime " $configManager.GetSynchronizationStatus().LastOverallEndTime;
                        Write-Host "Overallfailure" $configManager.GetSynchronizationStatus().OverallFailures;
                        Write-Host "Overallsuccess" $configManager.GetSynchronizationStatus().OverallSuccesses;
                
                        break;
                    }
             }
                else
                {   # Below wait period is important to avoid SQL query threshhold    
                    Start-Sleep -s 10
                }
             }
         }
        else
        {
        Write-Host "Already Synchronizing"
        }
        }
   catch
   {
         $ErrorMessage = $_.Exception.Message
         Write-Host  ($ErrorMessage) -ForegroundColor Red
   }
}

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null) 
{
    Write-Output "Loading SharePoint Powershell Snapin" -ForegroundColor Green
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
}

RunUserProfileSynchronization


Thursday, July 9, 2015

Skeleton of basic powershell sharepoint file

Below is basic skeleton of powershell file for sharepoint:-


param(

)
function Ensure-PSSnapin
{
    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
    {
        Add-PSSnapin "Microsoft.SharePoint.PowerShell" -Verbose:$false
        Write-Verbose "SharePoint Powershell Snapin loaded."
    } 
}

function Release-PSSnapin
{
    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -ne $null) 
    {
        Remove-PSSnapin "Microsoft.SharePoint.PowerShell" -Verbose:$false
        Write-Verbose "SharePoint Powershell Snapin removed."
    } 
}

Ensure-PSSnapin
$gc = Start-SPAssignment -Verbose:$false

try
{
        
# do your stuff        

} 
catch
{
    write-output -ForegroundColor Red $_.ToString()
 write-output -ForegroundColor Red $_.Exception.ToString()
}
finally 
{
 Stop-SPAssignment $gc -Verbose:$false
 Release-PSSnapin
}

Wednesday, July 8, 2015

App catalog site collection creation and association with web application using powershell SharePoint 2013


The App catalog site collection to hold our Apps. There can be one App Catlog site collection in a webapplication. There can be multiple App catalog site collection in a web application.

The App Catalog site collection can be created from central admin however following are the powershell command which can be used and modified according to need to create and associate app site collection for webapplication.


# Following command to create new app catalog sitecollection

New-SPSite -Url http://XYZ:1440/sites/AppCatalog -OwnerAlias "XYZ\administrator" -Name “App Catalog site" -Template "APPCATALOG#0"

Write-Output "Start associating app catalog sitecollection" $AppCatalogSitecollectionUrl "to" $WebApplicationUrl;

Update-SPAppCatalogConfiguration -site $AppCatalogSitecollectionUrl -Force:$true -SkipWebTemplateChecking:$true;

Write-Output "Association completed";