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 - Remove/Delete App to AppCatalog store in SharePoint 2013

The following powershell script will remove app from 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 psRemoveAppFromAppCatalog.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
{
    $site = new-object Microsoft.SharePoint.SPSite($WebUrl)

    $web = $site.openweb()

    $list=$web.Lists[$AppCatalogName]

    $listItems = $list.Items

    $listItemsTotal = $listItems.Count

    Write-Host $listItemsTotal

    if($list -ne $null)
    {
        for ($x=$listItemsTotal-1;$x -ge 0; $x--)
        {
          if($listItems[$x].name -eq $AppName) # file refers to the    name of the document
          {
            $listItems[$x].Delete()
            write-host -f Green "Successssfully unistalled the app -" $AppName " from app catalog" -ForegroundColor Green
          }
        }
    }
}

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 ./psRemoveAppFromAppCatalog.ps1 -WebUrl "https://catalog.ec.com" -AppCatalogName "Apps for SharePoint" -AppName "Oakton.Solutions.EC.WebApp.app"
pause


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

No comments:

Post a Comment