Using PowerShell Script Activate Publishing Feature

  1. #********** Active Site publishing Collection Feature  *********************************************  
  2. $siteCollectionFile = 'C:\doc\Get_SiteCollections_url.csv'  
  3.   
  4.   
  5. $host.Runspace.ThreadOptions = "ReuseThread"   
  6.    
  7. function Enable-SPOFeature   
  8. {   
  9.     param ($sSiteColUrl,$sUserName,$sPassword,$sFeatureGuid)   
  10.     try   
  11.     {       
  12.         #Adding the Client OM Assemblies           
  13.         Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"   
  14.         Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"   
  15.   
  16.         #SPO Client Object Model Context   
  17.         $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl)    
  18.         $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)     
  19.         $spoCtx.Credentials = $spoCredentials         
  20.    
  21.         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green   
  22.         Write-Host "Enabling the Feature with GUID $sFeatureGuid !!" -ForegroundColor Green   
  23.         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green   
  24.    
  25.         $guiFeatureGuid = [System.Guid] $sFeatureGuid   
  26.         $spoSite=$spoCtx.Site   
  27.         $spoSite.Features.Add($sFeatureGuid, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None)   
  28.         $spoCtx.ExecuteQuery()   
  29.         $spoCtx.Dispose()   
  30.     }   
  31.     catch [System.Exception]   
  32.     {   
  33.         write-host -f red $_.Exception.ToString()      
  34.     }       
  35. }   
  36.    
  37. #Required Parameters    
  38. $sUserName = "[email protected]"   
  39. $sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString  
  40. #SharePoint Server Publishing Infrastructure   
  41. $sFeatureGuid = "f6924d36-2fa8-4f0b-b16d-06b7250180fa"   
  42.   
  43. Import-Csv $siteCollectionFile |   
  44.     where {   
  45.         Write-Host $_.URL " Publishing feature activating"  
  46.         $error.Clear()  
  47.         try  
  48.         {  
  49.             Enable-SPOFeature -sSiteColUrl $_.URL -sUserName $sUserName -sPassword $sPassword -sFeatureGuid $sFeatureGuid   
  50.             if($error[0])  
  51.             {  
  52.                 Write-Host $_.URL " feature activation failed"  
  53.                 $_.URL + " feature activation failed for feature: Publishing"  >> ".\FeatureActivationLog.txt"  
  54.             }  
  55.             else  
  56.             {  
  57.                 Write-Host $_.URL " feature activation successfull"  
  58.                 $_.URL + " feature activation successfull for feature: Publishing"  >> ".\FeatureActivationLog.txt"    
  59.             }  
  60.         }  
  61.         catch  
  62.         {  
  63.             $ErrorMessage = $_.Exception.Message  
  64.             $FailedItem = $_.Exception.ItemName  
  65.             Write-Host $_.URL " feature activation failed"  
  66.             $_.URL + " feature activation failed for feature: Publishing" >> ".\FeatureActivationLog.txt"   
  67.         }  
  68.         finally  
  69.         {  
  70.         }  
  71.     }