Thursday, January 11, 2018

Get All Timer Jobs Status from a Farm in GridView using PowerShell


clear
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell

function Get-SPTimerJobStatus
{
    Get-SPTimerJob | sort Name | ForEach-Object {
        $lastRun = $_.HistoryEntries | Select-Object -first 1
        if ($_.WebApplication -eq $null) { $level = "Farm" }
        else { $level = $_.WebApplication.Url }
        
        $values = @{
            "Name" = $_.Name
            "Level" = $level
            "StartTime" = $lastRun.StartTime
            "EndTime" = $lastRun.EndTime
            "Status" = $lastRun.Status
        }
        New-Object PSObject -Property $values | Select @("Name","Level","StartTime","EndTime","Status")
    }
}
Get-SPTimerJobStatus | Out-GridView

Find out all Site Collection Administrators from a Web Application using PowerShell


clear
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell

############### VARIABLES ################################

$rootsiteUrl = "https:/webappurl"

################################################################

$rootSite = New-Object Microsoft.SharePoint.SPSite($rootsiteUrl)
$spWebApp = $rootSite.WebApplication
 
foreach($site in $spWebApp.Sites)
{
    foreach($siteAdmin in $site.RootWeb.SiteAdministrators)
    {
        Write-Host "$($siteAdmin.ParentWeb.Url) - $($siteAdmin.DisplayName)"
    }
    $site.Dispose()
}
$rootSite.Dispose()

Remove Trusted Identity Token Issuer from SharePoint using PowerShell



clear 
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue 
Add-PSSnapin Microsoft.SharePoint.Powershell

Remove-SPTrustedIdentityTokenIssuer STSWebSite

Add Certificate SharePoint Trusted Root using PowerSHell



clear  
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue  
Add-PSSnapin Microsoft.SharePoint.Powershell 

$invocation = (Get-Variable MyInvocation).Value
$currentfolder = Split-Path $invocation.MyCommand.Path
$SolutionPath=$currentfolder + "\STSPublicCert.cer"
New-SPTrustedRootAuthority -Name "STSTrustRenewed" -Certificate $SolutionPath 

Renew FAST Search Certificate using PowerShell

This script is more useful for the environments that uses self signed certificates for FAST Search


###################################
# Apply Certificate to FAST
###################################

Clear
Remove-PSSnapin AdminSnapIn -ErrorAction SilentlyContinue
Remove-PSSnapin Microsoft.FASTSearch.PowerShell -ErrorAction SilentlyContinue
Remove-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$userName = "Domain\ServiceAccountName"
$FastContentServiceName = "FAST Content"

write-host "Applying Certificate to FAST" -ForegroundColor Green
#Release worker processess with IISRESET
iisreset

stop-service FAST*

$installerdir = $env:FASTSEARCH + "installer\scripts"
echo $installerdir
cd $installerdir
$pw = ConvertTo-SecureString -AsPlainText -force test
.\ReplaceDefaultCertificate.ps1 -generateNewCertificate $true -certificatePassword $pw
$cert = @(dir cert:\LocalMachine\My -recurse | ? { $_.Subject -eq 'CN=FASTSearchCert' })[0]
$thumb = $cert.Thumbprint

Start-service FAST*

.\SecureFASTSearchConnector.ps1 -certThumbprint $thumb -ssaName $FastContentServiceName -username $userName

Generate DOCX to PDF file using Word Automation Service (WAS) in SharePoint using PowerShell


Note: This script will be very useful to test whether the WAS service is working properly in the environment. In addition, it will help to find out the source and destination libraries have proper permissions for the WAS to generate the PDF


clear  
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue  
Add-PSSnapin Microsoft.SharePoint.Powershell 

$webAppUrl = "http://webappurl"


[void][System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.Office.Word.Server" ) 

$jobSettings = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJobSettings 

$jobSettings.OutputFormat = "PDF" 

$job = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJob( "Word Automation Services", $jobSettings ) 

$job.UserToken = (Get-SPWeb $webAppUrl).CurrentUser.UserToken 

#Provide the full document url with docx and pdf location

$job.AddFile( "http://webappurl/Shared%20Documents/test1.docx", "http://webappurl/Shared%20Documents/ConvertedDoc.pdf" ) 

$job.Start( )

write-host "Complete"

Disable Sandbox Model for Word Automation Services using PowerShell


clear
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell

$sp = Get-SPServiceApplication | where {$_.TypeName.Equals("Word Automation Services")}  
$sp.DisableSandbox = $true
$sp.Update()

Perform IISReset in all the servers of a SharePoint Farm using PowerShell


clear
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell

If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )  
{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell } 
 
$host.Runspace.ThreadOptions = "ReuseThread" 
 
#Definition of the funcion that performs the IIS RESET in all the servers 
function PerformIISReset 
{     
    try 
    {         
        #Getting the servers where the IISReset is going to be done 
        $spServers= Get-SPServer | ? {$_.Role -eq "Application"} 
        foreach ($spServer in $spServers) 
        {             
            Write-Host "Perform IIS Reset in server $spServer" -f blue 
            iisreset $spServer /noforce "\\"$_.Address 
            iisreset $spServer /status "\\"$_.Address 
        }         
        Write-Host "IIS Reset completed successfully!!" -f blue   
    } 
    catch [System.Exception] 
    { 
        write-host -f red $_.Exception.ToString() 
    } 
} 
 
Start-SPAssignment –Global 
PerformIISReset 
Stop-SPAssignment –Global 
 

Extract WSP (SharePoint Solution) file from SharePoint Farm using PowerShell

clear
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell

$solName = "Sample.wsp"
$farm = Get-SPFarm

$file = $farm.Solutions.Item($solName).SolutionFile
$file.SaveAs("c:\Sample.wsp")

write-host "Completed"

Set Master Page Url using PowerShell - SharePoint

clear
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell

################ ENVIRONMENT VARIABLES #########################

$webAppUrl = "https://webappurl"

################################################################


$mainSite = $webAppUrl 
$web = Get-SPWeb $mainSite
$web.MasterUrl = "/_catalogs/masterpage/v4.master"
$web.Update()
$web.Dispose()

Write-host "Completed"

Enable Crawl for DropOffLibrary Documents using PowerShell - SharePoint

clear  
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue  
Add-PSSnapin Microsoft.SharePoint.Powershell 

################## ENVIRONMENT VARIABLES ##################

$siteUrl = "https://record-site-coll-url"


###########################################################

$dropOffLibrary ="Drop Off Library"
try
{
    $site=Get-SPSite $siteUrl
    $web=$site.RootWeb
    $docLibrary =$web.Lists[$dropOffLibrary]
    # Change the advanced settings
    $docLibrary.NoCrawl = $false;
    $docLibrary.Update()
    $site.Dispose()
    write-host "Completed"
}
catch
{
    write-host "Error"
}


Create Send To Connector using PowerShell - SharePoint

clear
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell

#################################### ENVIRONMENT VARIABLES #########################################

 $WebApplicationURL = "https://webappUrl"  ## eg: "http://spddwebapp"
    $RecordCenterSiteColls = @("SiteColl1","SiteColl2")
  
 
####################################################################################################
    
    $ServiceURL = "_vti_bin/officialfile.asmx"
    write-host "Opening web app object."
    
    $WebApp = Get-SPWebApplication $WebApplicationURL


function AddSendToConnection($sendServiceURL, $siteName )
{

    #Get the Web Application
   
     
    $SPOfficialFileHost = New-Object Microsoft.SharePoint.SPOfficialFileHost

    #Set Send to Options#

    #Send to Connection Name
    $oFName = $siteName + " Send To Conenctor"
    $SPOfficialFileHost.OfficialFileName = $oFName

    #Send to Target location
    
    #write-host "Office File Url - $offFileUrl"
    $SPOfficialFileHost.OfficialFileUrl = $sendServiceURL

    $SPOfficialFileHost.ShowOnSendToMenu = $true

    #Send to Operation

    $SPOfficialFileHost.Action=[Microsoft.SharePoint.SPOfficialFileAction]::Move

    $SPOfficialFileHost.Explanation = "Send to connection for documents move to other record site collections."

    $WebApp.OfficialFileHosts.Add($SPOfficialFileHost); 

    $WebApp.Update()

}



write-host "Loop through all receiver site collections."
foreach ($rSite in $RecordCenterSiteColls)
{
    $fullSiteURL = $WebApplicationURL + "/sites/" + $rSite
    $sendServiceURL =   $fullSiteURL + "/" + $ServiceURL
    write-host "Send service URL - $sendServiceURL"
    AddSendToConnection $sendServiceURL $rSite
    write-host "Succesfully added Send To Connector for $rSite"
}

write-host "Completed"

Export Content Types using PowerShell - SharePoint


clear
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell
####################### ENVIRONMENT VARIABLES #######################
$contentTypeSourceWeb = "http://webapp/sites/contenttypehub" #Site Collecton
# The exported content type is stored in the XML file
$xmlFilePathForContentType = "C:\Script-SiteContentTypes.xml" 
#####################################################################

#Content Type group you are looking to export
$contentTypeGroupName = "Custom Content Types"
$sourceWeb = Get-SPWeb $contentTypeSourceWeb

#Create Export File
New-Item $xmlFilePathForContentType -type file -force
#Export Content Types to XML file
Add-Content $xmlFilePathForContentType "<?xml version=`"1.0`" encoding=`"utf-8`"?>"
Add-Content $xmlFilePathForContentType "`n<ContentTypes>"
$sourceWeb.ContentTypes | ForEach-Object {
    if ($_.Group -eq $contentTypeGroupName) {
        write-host "Adding content type to file."
        Add-Content $xmlFilePathForContentType $_.SchemaXml
    }
}
Add-Content $xmlFilePathForContentType "</ContentTypes>"
$sourceWeb.Dispose()
write-host "Completed"

Access to XMLHttpRequest at 'from origin has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. .net core angular

Issue: The angular application was getting error from API that the origin has been blocked by CORS policy. Solution: Make sure that the...