Wednesday, June 3, 2015

Enable NetBIOS & Update Feed Cache for User Profile Service Application

Enable NetBIOS for User Profile Service Application


$ServiceApps = Get-SPServiceApplication
$UserProfileServiceApp = ""
foreach ($sa in $ServiceApps)
{
if ($sa.DisplayName -eq "User Profile Service Application")
{$UserProfileServiceApp = $sa}
}
$UserProfileServiceApp.NetBIOSDomainNamesEnabled = 1
$UserProfileServiceApp.Update()

Gracefully remove distributed cache instance
Stop-SPDistributedCacheServiceInstance -Graceful
Remove-SPDistributedCacheServiceInstance


-------------------------------------------------------------------------------------------------------------------------------------------------------------------Update Feed Cache


$upa = Get-SPServiceApplication | where {$_.TypeName -Like "User Profile Service Application"}
$upa.FeedCacheTTLHours = 96
$upa.Update()

Stop Distributed cache host gracefully

## Settings you may want to change for your scenario ##
$startTime = Get-Date
$currentTime = $startTime
$elapsedTime = $currentTime - $startTime
$timeOut = 900

try
{
    Write-Host "Shutting down distributed cache host."
$hostInfo = Stop-CacheHost -Graceful -CachePort 22233 -ComputerName sp2013App.contoso.com

while($elapsedTime.TotalSeconds -le $timeOut-and $hostInfo.Status -ne 'Down')
{
     Write-Host "Host Status : [$($hostInfo.Status)]"
     Start-Sleep(5)
     $currentTime = Get-Date
     $elapsedTime = $currentTime - $startTime
     $hostInfo = Get-CacheHost -HostName SP2013app.contoso.com -CachePort 22233
}

Write-Host "Stopping distributed cache host was successful. Updating Service status in SharePoint."
Stop-SPDistributedCacheServiceInstance
Write-Host "To start service, please use Central Administration site."
}
catch [System.Exception]
{
Write-Host "Unable to stop cache host within 15 minutes."
}

Source:Microsoft Technet
It is always advised to stop DC service on SharePoint 2013 before a server is stopped/restarted as per one of the Premium case we worked with Microsoft.

Tuesday, June 2, 2015

Run SharePoint Cumulative Update using powershell

Run SharePoint Cumulative Update

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
###########################
##Ensure Patch is Present##
###########################
$patchfile = Get-ChildItem | where{$_.Extension -eq ".exe"}
if($patchfile -eq $null)
{
Write-Host "Unable to retrieve the file. Exiting Script" -ForegroundColor Red
Return
}


########################
##Stop Search Services##
########################
##Checking Search services##
$srchctr = 1
$srch4srvctr = 1
$srch5srvctr = 1

$srv4 = get-service "OSearch15"
$srv5 = get-service "SPSearchHostController"

If(($srv4.status -eq "Running") -or ($srv5.status-eq "Running"))
{
Write-Host "Choose 1 to Pause Search Service Application" -ForegroundColor Cyan
Write-Host "Choose 2 to leave Search Service Application running" -ForegroundColor Cyan
$searchappresult = Read-Host "Press 1 or 2 and hit enter"
Write-Host


if($searchappresult -eq 1)
{
$srchctr = 2
Write-Host "Pausing the Search Service Application" -foregroundcolor yellow
Write-Host "This could take a few minutes" -ForegroundColor Yellow
$ssa = get-spenterprisesearchserviceapplication
$ssa.pause()
}


elseif($searchappresult -eq 2)
{
Write-Host "Continuing without pausing the Search Service Application"
}
else
{
Write-Host "Run the script again and choose option 1 or 2" -ForegroundColor Red
Write-Host "Exiting Script" -ForegroundColor Red
Return
}
}

Write-Host "Stopping Search Services if they are running" -foregroundcolor yellow
if($srv4.status -eq "Running")
{
$srch4srvctr = 2
set-service -Name "OSearch15" -startuptype Disabled
$srv4.stop()
}

if($srv5.status -eq "Running")
{
$srch5srvctr = 2
Set-service "SPSearchHostController" -startuptype Disabled
$srv5.stop()
}

do
{
$srv6 = get-service "SPSearchHostController"
if($srv6.status -eq "Stopped")
{
$yes = 1
}
Start-Sleep -seconds 10
}
until ($yes -eq 1)

Write-Host "Search Services are stopped" -foregroundcolor Green
Write-Host



#######################
##Stop Other Services##
#######################
Set-Service -Name "IISADMIN" -startuptype Disabled
Set-Service -Name "SPTimerV4" -startuptype Disabled
Write-Host "Gracefully stopping IIS W3WP Processes" -foregroundcolor yellow
Write-Host
iisreset -stop -noforce
Write-Host "Stopping Services" -foregroundcolor yellow
Write-Host

$srv2 = get-service "SPTimerV4"
if($srv2.status -eq "Running")
{$srv2.stop()}

Write-Host "Services are Stopped" -ForegroundColor Green
Write-Host
Write-Host



##################
##Start patching##
##################
Write-Host "Patching now keep this PowerShell window open" -ForegroundColor Magenta
Write-Host
$starttime = Get-Date

$filename = $patchfile.basename
$arg = "/passive"

Start-Process $filename $arg

Start-Sleep -seconds 20
$proc = get-process $filename
$proc.WaitForExit()

$finishtime = get-date
Write-Host
Write-Host "Patch installation complete" -foregroundcolor green
Write-Host



##################
##Start Services##
##################
Write-Host "Starting Services Backup" -foregroundcolor yellow
Set-Service -Name "SPTimerV4" -startuptype Automatic
Set-Service -Name "IISADMIN" -startuptype Automatic

##Grabbing local server and starting services##
$servername = hostname
$server = get-spserver $servername

$srv2 = get-service "SPTimerV4"
$srv2.start()
$srv3 = get-service "IISADMIN"
$srv3.start()
$srv4 = get-service "OSearch15"
$srv5 = get-service "SPSearchHostController"

###Ensuring Search Services were stopped by script before Starting"
if($srch4srvctr -eq 2)
{
set-service -Name "OSearch15" -startuptype Automatic
$srv4.start()
}
if($srch5srvctr -eq 2)
{
Set-service "SPSearchHostController" -startuptype Automatic
$srv5.start()
}

###Resuming Search Service Application if paused###
if($srchctr -eq 2)
{
Write-Host "Resuming the Search Service Application" -foregroundcolor yellow
$ssa = get-spenterprisesearchserviceapplication
$ssa.resume()
}

Write-Host "Services are Started" -foregroundcolor green
Write-Host
Write-Host
Write-Host "Script Duration" -foregroundcolor yellow
Write-Host "Started: " $starttime -foregroundcolor yellow
Write-Host "Finished: " $finishtime -foregroundcolor yellow
Write-Host "Script Complete"

Write-Host "REMINDER: Run SharePoint Products Configuration Wizard on each server" -foregroundcolor green

Clear SharePoint Config Cache with PowerShell

Clear SharePoint Config Cache with PowerShell
Add-PSSnapin -Name Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue

Stop-Service SPTimerV4
$folders = Get-ChildItem C:\ProgramData\Microsoft\SharePoint\Config
foreach ($folder in $folders)
{
$items = Get-ChildItem $folder.FullName -Recurse
foreach ($item in $items)
{
if ($item.Name.ToLower() -eq "cache.ini")
{
$cachefolder = $folder.FullName
}

}
}
$cachefolderitems = Get-ChildItem $cachefolder -Recurse
foreach ($cachefolderitem in $cachefolderitems)
{
if($cachefolderitem -like "*.xml")
{
$cachefolderitem.Delete()
}

}

$a = Get-Content $cachefolder\cache.ini
$a = 1
Set-Content $a -Path $cachefolder\cache.ini

read-host "Do this on all your SharePoint Servers - and THEN press ENTER"
start-Service SPTimerV4

Thursday, November 20, 2014

SharePoint Powershell script to get permission for all subsites.

SharePoint Powershell script to get permission for all subsites.

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$webApps = Get-SPWebApplication "https://myportal.mysite.dev.mycompany.com"
foreach ($webApp in $webApps)
{
  foreach ($site in $webApp.Sites)
   {
       #WRITE-HOST SITE COLLECTION URL
       write-host “SITE: ” $site.URL
       #FOREACH LOOP, LOOPING THROUGH ALL SITE COLLECTION ADMINISTRATORS
       foreach ($siteAdmin in $site.RootWeb.SiteAdministrators)
              {
                 #WRITE-HOST SITE COLLECTION ADMINS
                 write-host -foregroundcolor green “SITE COLLECTION ADMINS – $($siteAdmin.DisplayName)”
 }
        #FOREACH LOOP, LOOPING THROUGH ALL SUB-SITES IN SITE COLLECTION
        foreach ($webs in $site.AllWebs)
 {
 #WRITE-HOST SUB-SITE NAME
 write-host “SUB-SITE NAME:” $webs.Name
 #WRITE-HOST SUB-SITES URL
 write-host “SUB-SITES URL:” $webs.URL
 #FOREACH LOOP, LOOPING THROUGH ALL ROLE ASSIGNMENTS IN THE WEB ROLE ASSIGNMENTS
 foreach ($roleAssignment in $webs.RoleAssignments)
  {
   #FOREACH LOOP, LOOPING THROUGH ALL ROLE DEFINITIONS IN ROLE ASSIGMENT ROLE DEFINITIONS
   foreach ($roleDefinition in $roleAssignment.RoleDefinitionBindings)
    {
     #IF STATEMENT (IF ROLE DEFINITION EQUALS FULL CONTROL)
     #if ($roleDefinition.Name -eq “Full Control”)
     #{
     #WRITE-HOST PERMISSION LEVEL AND PERMISSION NAME
     write-host -foregroundcolor red “Permission Level: ” $roleDefinition.Name “| Permission Name: “$roleAssignment.Member.Name
     #}
    }
   }
  }
  }
}
write-host -foregroundcolor red “Script Over"

Monday, December 16, 2013

Configure Synchronization Connections An error has occurred while accessing the SQL Server database

Configure Synchronization Connections An error has occurred while accessing the SQL Server database

I was continiously getting this error in User profile service application under configure sync connection.

Googled some of the blog and saw some suggestion to do a IIS reset across the farm. I did the same but couldnt find the solution to my problem.

Further digging into Services.msc (windows services), I noticed my Forefront Identity Manager Service is stopped and Forefront Identity Manager Sync Service was started.

Just started the Forefront Identity Manager Service and my problem was resolved.

Tuesday, December 3, 2013

User profile Synchronization service stuck on starting

Technology:SP2013

In one of my project I had faced an issue where I was working with User Profile Synchronization connection and the service abruptly stuck on starting phase.

We did some troubleshooting like specified below


Troubleshooting Steps:

# User Profile sync service was stuck on starting on "Application Server".

# Stopped UPA sync on "Application Server"  running below commands:

Get-SPServiceInstance   (copy UPA sync guid)

Stop-SPServiceInstance –id

 

# Verified that User profile sync service is using farm account.

# Farm account has Replicate Directory Changes permission on domain.

# Added farm account to local admin group on server "App Server/Index Server".

# Added farm account to all FIM & WSS groups in Local users and computers.

# Added farm account to "Log on as a batch job" and "Log on as a service".

 

# Followed below steps to start UPA sync service on server "App Server/Index Server".

1 Stopped SP timer and SP Administration service under services.msc

2 Clear the SharePoint Config cache and set the counter to 1

3 stsadm -o execadmsvcjobs

4 Started SharePoint timer and SharePoint admin service from services.msc

5 Delete all the certificated related to ForefrontIdentityManager from MMC - Certificates.

6 Gave full permission to SharePoint groups "WSS_WPG" & WSS_ADMIN_WPG" on below folder.

"C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys"

# Started sync service with farm account.

#  However the sync service stopped again.

 # Verified that farm account was not having connect permission on sync db.

# Gave said permissions to the farm account.

# Reset User profile's sync database as per below article:


 # Tried starting synch service, still same behavior.

# Created a new User Profile Service Application with new databases, still same behavior.
 
# Went to Start->Administrative tools->Local Security policy->Local Policies->user right assignments  on App server and removed Admin account from "Deny log on localy" group

 

 

After lot of Troubleshooting and inconstant touch with microsoft we got the solution to our problem.

 

SharePoint is supposed to create a folder “Madata” at" C:\Program Files\Microsoft Office Servers\15.0\Synchronization Service" which was missing. We created the folder manually and created a txt file inside Madata folder with name "UpdateFile.txt" and restarted the sync service.

 

Our sync service was up and active and we were able to import profile from active directory.

 
 
Worth reading:
 
 
 
 

Tuesday, February 1, 2011

Powershell scripts for Sharepoint 2010/ 2013

To stop a Sharepoint service on basis of GUID

Stop-SPServiceInstance {GUID}

 -------------------------------------------------------------------------------------------------------------------------------------------------------------------
To Start a Sharepoint service on basis of GUID

Start-SPServiceInstance {GUID}

 -------------------------------------------------------------------------------------------------------------------------------------------------------------------
To Start a Sharepoint service on basis of name

Get-SPServiceInstance | Where-Object {$_.TypeName –eq ‘Central Administration/User profile Synchronization service’} | Start-SPServiceInstance

 -------------------------------------------------------------------------------------------------------------------------------------------------------------------
To Stop a SharePoint Service

Get-SPServiceInstance | Where-Object {$_.TypeName –eq ‘Central Administration/User profile Synchronization service’} | Stop-SPServiceInstance

 -------------------------------------------------------------------------------------------------------------------------------------------------------------------
To Stop service on particular server

Get-SPServiceInstance -Server "ServerName"  | where-object {$_.TypeName -eq "User Profile Synchronization Service " } | Stop-SPServiceInstance -Confirm:$false

 -------------------------------------------------------------------------------------------------------------------------------------------------------------------
To Start service on particular server

Get-SPServiceInstance -Server "ServerName"  | where-object {$_.TypeName -eq "User Profile Synchronization Service " } | Start-SPServiceInstance -Confirm:$false

 -------------------------------------------------------------------------------------------------------------------------------------------------------------------
Update Distributed cache Size

Update-SPDistributedCacheSize -CacheSizeInMB CacheSize

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------
Export/Import of subsite

Export-SPWeb "http://SiteCollection/sites/Site1/Subsite1" -Path "C:\Export.cmp"
Import-SPWeb "http://SiteCollection/sites/Site1/Subsite2" -Path "C:\Export.cmp"

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------
Export/Import of list

Export-SPWeb “http://abc.com/site/” –ItemUrl “Lists/employee” –Path “c:\export\employee.cmp”
Import-SPWeb “http://abcxyz.com/site/” -Path "c:\export\employee.cmp"

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Activate Publishing features through powershell


Enable-SPFeature -Identity PublishingSite -URL https://abc.com -Force
Enable-SPFeature -Identity PublishingWeb -URL https://abc.com -Force


-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Session state for SharePoint 2013
New-SPStateServiceDatabase -Name "SP2013_Env_StateService" | New-SPStateServiceApplication -Name "State Service Application" | New-SPStateServiceApplicationProxy -DefaultProxyGroup


Enable-SPSessionStateService -DatabaseName SP2013_Env_ASP_NET_SPSessionStateService -DatabaseServer SP2013_Env_ALIAS_Servername


More reading at
http://nikpatel.net/2012/02/12/enable-asp-net-session-state-on-sharepoint-2010-application/
http://blogs.msdn.com/b/markarend/archive/2010/05/27/using-session-state-in-sharepoint-2010.aspx