Wednesday, June 26, 2013

Portal Site Connection Url Updation using Powershell

Hi,
Generally in our site collection, we could have updated portal site name, portal site url to get the site hierarchy. We follow the below navigation to update the portal site name,url. But if there are twenty site collections where we need to update portal site name,url. At that time PowerShell script can ease the task for the same as below
//PortalSiteConnection.bat
cd /d %~dp0
@echo off
powershell.exe -noexit .\PortalSiteConnection.ps1 .\PortalSiteConnection.csv
//PortalSiteConnection.csv
MainWebAppurl
//PortalSiteConnection.ps1
#-----Input parameters to the script
param($inputFile)

#------To check if Sharepoint cmdlets are registered are registered
if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null )
{
            Add-PSSnapin Microsoft.SharePoint.Powershell 
}
#---------------------------------------------------------------------------------------------------
#-----------------------------------------Start Logging---------------------------------------------
$filepath = $MyInvocation.MyCommand.Definition                                       
$directorypath = [System.IO.Path]::GetDirectoryName($filepath)
$LogTime = Get-Date -Format yyyy-MM-dd_h-mm
$LogFile = $directorypath + "\PortalSiteConnectionSettings_Log_$LogTime.txt"
Start-Transcript -Path $LogFile -Force
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
function PortalSiteConnectionSettings()
{
param([string] $mwebAppUrl)

$MainWebApp = Get-SPWebApplication -Identity $mainwebAppUrl

if($MainWebApp -ne $null)
{


get-spsite $($MainWebApp.Url + "*" ) -Limit ALL | ForEach-Object{if($_.PortalUrl.Contains(“https://abc”) –and ($_.PortalUrl -ne $null)){

$_.PortalUrl = $_.PortalUrl.Replace("https:// abc.", " https://xyz");

}

}
}
}


#---------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------
#loop through a csv file
Import-Csv $inputFile | ForEach{                                
    $mainwebAppUrl = $_.MainWebAppurl  
    try
    {
  write-host "Updating Provisioning Url in portal site connection...."
        PortalSiteConnectionSettings $mainwebAppUrl
  write-host "Portal Site Connection Url has been updated" -ForegroundColor Green
    }
    catch
    {
  write-host "Completed with errors..."   
        Write-Output $_
    }
}
#---------------------------------------------------------------------------------------------------

#-----------------------------------------End Logging-----------------------------------------------
Stop-Transcript
#---------------------------------------------------------------------------------------------------

Note:
Before running this script,make sure that the account with which this script is executing is holding Farm admin privilege (or)
Before running this script,make sure that the account with which this script is  executing is member of admin group under every site collection of mentioned Web Application  

No comments:

Post a Comment