About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Monday, 25 August 2014

Powershell Script - Copy files from source location to destination - Xcopy deployment

The following script will backup files from destination location to some other folder and copy files from source location to destination. The script accepts following input parameters

1. Destination location
2. Backup location

Note: I have hard coded source location for convenience, however you can pass as a parameter

Steps involved

1. Save following script as psCopyFiles.ps1


#-----------------------------------------------------------------------------
# Name:             psCopyFiles.ps1 
# Description:      This script will files from source location to destination
#                   location and backup files to another directory
#
# Usage:            Run the script by passing the paramter SourceLocation,
#                   DestinationLocation & BackupLocation
# Created By:       Vamsi Mohan Mitta
# Date:             01/07/2014
#-----------------------------------------------------------------------------


Param([Parameter(Mandatory=$true)]
      [String]
      $DestinationLocation,
      [Parameter(Mandatory=$true)]
      [String]
      $BackupLocation
)
if ((gsnp Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null){
    asnp Microsoft.SharePoint.Powershell
}

function Get-ScriptDirectory
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

$currentPhysicalPath = Get-ScriptDirectory

$logfile=$currentPhysicalPath + "\log.log"

Start-Transcript $logfile

#$backupLocation = "\\WIN-K6KO9R0T2LC\C$\Backup"
#$backupFolder = $BackupLocation + "Backup"

if ((Test-Path $BackupLocation) -ne $True)
{
    New-Item $BackupLocation -type directory
    write-host “Successfully created folder!” -ForegroundColor Green
}

else
{
    write-host “Folder already exits” -ForegroundColor Blue
}

#Get current date
#$currentDate = (get-date).tostring("mm_dd_yyyy-hh_mm_s")
$currentDate = Get-Date -format MM-dd-yyyy
if($currentDate -ne $null)
{
    $newFolder = $BackupLocation + $currentDate
    if ((Test-Path $newFolder) -ne $True)
    {
        New-Item $newFolder -type directory
        write-host “Successfully created backup folder-” $newFolder -ForegroundColor Green
    }
    if($newFolder -ne $null)
    {
        $removeBackupFolder = $newFolder + "\*"
        Remove-Item $removeBackupFolder -Recurse

        Copy-Item $DestinationLocation -Destination $newFolder -Recurse
        write-host “Successfully copied files to backup folder-” $newFolder -ForegroundColor Green

        $removeFolder = $DestinationLocation + "*"
        Remove-Item $removeFolder -Recurse
        write-host “Successfully removed files from folder--” $DestinationLocation -ForegroundColor Green

        #copy files from source location to destination
        $sLocation = $currentPhysicalPath + "\webapp.com\*"
        Copy-Item $sLocation -Destination $DestinationLocation -Recurse
        write-host “Successfully copied files & folders to -” $DestinationLocation -ForegroundColor Green
    }   
}

Stop-Transcript
Echo Finish


Step2: Create batch file to run the script. Save file in .bat format.

cd /d %~dp0
powershell -file  ./psCopyFiles.ps1 -DestinationLocation "\\WIN-K6KO9R0T2LC\C$\ecwebapp.com\" -BackupLocation "\\WIN-K6KO9R0T2LC\C$\Backup\"
pause

Happy powershell coding... please feel free to comment...

Powershell Script - Create custom SharePoint 2013 search managed properties

The following script will create managed properties for search. The script will accept following input paramerers

1. Search Service application - Name of your search service application

Note: The script will refer .csv file which has input data. The column header format would be as follows, add your properties under this header and save file as .csv format.The file shoulb be available in the same directory.

Name;Properties;Type;Sort;Retrieve;Refine;Search;Multivalue;Query;Safe

Steps involved

1. Save following script as psCreateSearchManagedProperties.ps1


#-----------------------------------------------------------------------------
# Name:             psCreateSearchManagedProperties.ps1 
# Description:      This script will create a list of managed properties in
#                    the SharePoint 2013 Search Service Application
#                    
# Usage:            Run the script passing paramter SearchServiceApplication
# Created By:       Vamsi Mohan Mitta
# Date:             01/07/2014
#-----------------------------------------------------------------------------

Param([Parameter(Mandatory=$true)]
      [String]
      $SearchApplication
)

if ((gsnp Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null){
    asnp Microsoft.SharePoint.Powershell
}

function Get-ScriptDirectory
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

$currentPhysicalPath = Get-ScriptDirectory

$logfile=$currentPhysicalPath + "\log.log"

Start-Transcript $logfile

$objShell = New-Object -Com Shell.Application
$folder = $objShell.BrowseForFolder(0,"Please select the folder containing your CSV file:",0)
$pathfld = $folder.Self.Path
$fullpath = Join-Path $pathfld "managed-properties.csv"

$ssa = Get-SPEnterpriseSearchServiceApplication $SearchApplication

Import-Csv -Delimiter ";" -Path $fullpath | % {

    switch ($_.Type){
        "text" {$type = 1}
        "integer" {$type = 2}
        "decimal" {$type = 3}
        "DateTime" {$type = 4}
        "YesNo" {$type = 5}
        "Binary" {$type = 6}
        "Double" {$type = 7}
        default {$type = 1}
    }
   
    if ((Get-SPEnterpriseSearchMetadataManagedProperty $_.Name -SearchApplication $ssa -ea SilentlyContinue) -eq $null){
        try {
            New-SPEnterpriseSearchMetadataManagedProperty -Name $_.Name -SearchApplication $ssa -Type $type -Retrievable:([bool]::Parse($_.Retrieve)) -Queryable:([bool]::Parse($_.Query)) -SafeForAnonymous:([bool]::Parse($_.Safe)) -EA Stop
       
            $mp = Get-SPEnterpriseSearchMetadataManagedProperty $_.Name -SearchApplication $ssa
            $mp.Searchable = [bool]::Parse($_.Search)
            $mp.Refinable = [bool]::Parse($_.Refine)
            $mp.Sortable = [bool]::Parse($_.Sort)
            $mp.HasMultipleValues = [bool]::Parse($_.Multivalue)
            $mp.Update()

            $cps = ($_.Properties).split(",")

            Write-Host "Managed property $($_.Name) successfully created." -ForegroundColor Green

            foreach ($cpname in $cps){
                try {
                    $cp = Get-SPEnterpriseSearchMetadataCrawledProperty $cpname -SearchApplication $ssa -EA Stop

                    try {
                        New-SPEnterpriseSearchMetadataMapping -SearchApplication $ssa -ManagedProperty $mp -CrawledProperty $cp

                        Write-Host "Mapping between Managed property $($_.Name) and crawled property $($cp.Name) completed successfully." -ForegroundColor Green
                    } catch {
                        Write-Host "Unable to map managed property $($_.Name) and crawled property $($cp.Name). $($error[0].Exception.Message)." -ForegroundColor Magenta
                    }
                } catch {
                    Write-Host "Crawled property $($cp.Name) not found. $($error[0].Exception.Message)." -ForegroundColor Magenta
                }
            }
        } catch {
            Write-Host "Unable to create managed property $($_.Name). $($error[0].Exception.Message)." -ForegroundColor Magenta
        }
    } else {
        Write-Host "Managed property $($_.Name) already exists." -ForegroundColor DarkYellow
    }
}

Stop-Transcript
Echo Finish


Step2: Create batch file to run the script. Save file in .bat format.

cd /d %~dp0
powershell -file ./psCreateSearchManagedProperties.ps1 -SearchApplication "Search"
pause

Happy powershell coding... please feel free to comment...