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 - Upload App to AppCatalog store in SharePoint 2013

The following powershell script will upload .App file to App catalog and accept following input parameters

1. WebUrl - App Catalog site url
2.AppCatalogName - 'Apps for Sharepoint'
3.AppName - Your .app name

Note: The .app should be in the same directory where you are running the script.

Steps involved
1. Save following script as psUploadAppToCatalog.ps1


Param(           
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]           
[string]$WebUrl,           
[Parameter(Mandatory=$true)]           
[string]$AppCatalogName,           
[Parameter(Mandatory=$true)]           
[string]$AppName          


if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell";
}
    
function Get-ScriptDirectory
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

#Get Current Physical Path
$currentPhysicalPath = Get-ScriptDirectory
$logfile=$currentPhysicalPath + "\log.log"
Start-Transcript $logfile        

try
{
    Start-SPAssignment -Global             
    $spWeb = Get-SPWeb -Identity $WebUrl            
    $spWeb.AllowUnsafeUpdates = $true;           
    $List = $spWeb.Lists[$AppCatalogName]           
    $folder = $List.RootFolder
    $FilePath = $currentPhysicalPath + "\" + $AppName    
    #$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)
           
    $File= Get-ChildItem $FilePath           
    [Microsoft.SharePoint.SPFile]$spFile = $spWeb.GetFile("/" + $folder.Url + "/" + $File.Name)           
    $flagConfirm = 'y'           

    #if($spFile.Exists -eq $true)           
    #{           
     #   $flagConfirm = Read-Host "File $AppName already exists in library $DocLibName, do you want to upload a new version(y/n)?"            
    #}           

    #if ($flagConfirm -eq 'y' -or $flagConfirm -eq 'Y')  
    if ($flagConfirm -eq 'y')       
    {           
        $fileStream = ([System.IO.FileInfo] (Get-Item $File.FullName)).OpenRead()           
        #Add file           
        write-host -NoNewLine -f yellow "Copying file " $File.Name " to " $folder.ServerRelativeUrl "..."           
        [Microsoft.SharePoint.SPFile]$spFile = $folder.Files.Add($folder.Url + "/" + $File.Name, [System.IO.Stream]$fileStream, $true)           
        write-host -f Green "...Success!"           
        #Close file stream           
        $fileStream.Close()
        write-host -f Green "Successssfully uploaded the app -" $AppName " to app catalog" -ForegroundColor Green     
    }              
$spWeb.AllowUnsafeUpdates = $false;
}

catch
{
        $Host.UI.RawUI.WindowTitle = " -- Error --"
        Write-Host -ForegroundColor Red $_.ToString()
        Write-Host -ForegroundColor Red $_.Exception.ToString()
}
Stop-Transcript         

Stop-SPAssignment -Global
            
Step2: Create batch file to run the script. Save file in .bat format.

cd /d %~dp0
powershell -file ./psUploadAppToCatalog.ps1 -WebUrl "https://catalog.ec.com" -AppCatalogName "Apps for SharePoint" -AppName "xxxxx.app"
pause

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

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...