Showing posts with label Sharepoint2010. Show all posts
Showing posts with label Sharepoint2010. Show all posts

Delete Document Library using PowerShell

Delete Site Collection by using PowerShell

Delete Subsite using PowerShell

Enable Site Features using Powershell

Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue
Enable-SPFeature "SharePoint Server Standard Site features" -Url http://globas:1050

Save Site Template using Powershell

Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue
$Web=Get-SPWeb http://globas/
$Web.SaveAsTemplate("test4","test4","test4",0)

Write-Host "Finished! Press enter key to exit." -ForegroundColor Green
Read-Host

Restore Site BackUp Using Powershell

Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue
      Write-Host   
      Write-Host   
      Write-Host "Backup starting at $backupStart for $Site "   
      Write-Host "******************************************"
     Restore-SPSite -Identity http://globas:1040/ -Path "E:\Sharepoint\FirstPrincipal\SiteBackUp\FPBakup\Latest\fp_04oct2012-latest.bak" -force    
    
      Write-Host   
      Write-Host   
      Write-Host "Backup Completed at $backupComplete for $Site "   
      Write-Host "******************************************"
Write-Host "Finished! Press enter key to exit." -ForegroundColor Green
Read-Host

Delete List using Powershell

Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue
$web = Get-SPWeb http://globas:1010
$custom = $web.lists["Articles"]
$custom.Delete()
$custom1 = $web.lists["BasicWebparts - Gallery"]
$custom1.Delete()
$custom2 = $web.lists["ContentSlides"]
$custom2.Delete()
$custom3 = $web.lists["domphoto"]
$custom3.Delete()
$custom4 = $web.lists["dp"]
$custom4.Delete()
$custom5 = $web.lists["gallery"]
$custom5.Delete()
$custom6 = $web.lists["photo1"]
$custom6.Delete()

Write-Host "Finished! Press enter key to exit." -ForegroundColor Green
Read-Host

Automated Daily Site Collection Back up

Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue
try
 {
    $today = (Get-Date -Format dd-MM-yyyy)
    $backupDirectory = "D:\Backup\DailySiteCollectionBackUp\$today"
  # Backup file Location
    $backupFile = "D:\FPBackup\DailySiteCollectionBackUp\$today\Backup.dat"
  # Log file location
    $logFile = "$backupDirectory\BackupLog.log"   
  # Address of the Site Collection to backup
    $Site = "http://moss01/"
   
 # Location of the Backup Folder
    if (-not (Test-Path $backupDirectory))
    {
      [IO.Directory]::CreateDirectory($backupDirectory)
      #New-Item $logPath -type $backupDirectory
    }
  
 # Get backup start date and time
    $backupStart = Get-Date -format "MM-dd-yyyy HH.mm.ss"
   
  # creates a log file  Start-Transcript -Path
    Start-Transcript -Path $logFile
     
 # This will actually initiate the backup process.
      Write-Host   
      Write-Host   
      Write-Host "Backup starting at $backupStart for $Site "   
      Write-Host "******************************************"
     Backup-SPSite -Identity $Site -Path $backupFile -Force
     $backupComplete = Get-Date -format "MM-dd-yyyy HH.mm.ss"
      Write-Host   
      Write-Host   
      Write-Host "Backup Completed at $backupComplete for $Site "   
      Write-Host "******************************************"
  
 Stop-Transcript
 }
Catch
 {
  $ErrorMessage = $_.Exception.Message
  write "$today BackUp Failed   $ErrorMessage  ">>$logFile

 }

Automated wsp installer using Powershell

######################################
######## Set Variables ###############
######################################
$InstallDIR = "C:\install"
 
######################################
#### CODE, No Changes Necessary ######
######################################
Write-Host "Working, Please wait...."
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
 
$Dir = get-childitem $InstallDIR -Recurse
$WSPList = $Dir | where {$_.Name -like "*.wsp*"}
Foreach ($wsp in $WSPList )
{
    $WSPFullFileName = $wsp.FullName
    $WSPFileName = $wsp.Name
    clear
    Write-Host -ForegroundColor White -BackgroundColor Blue "Working on $WSPFileName" 
 
    try
    {
        Write-Host -ForegroundColor Green "Checking Status of Solution"
        $output = Get-SPSolution -Identity $WSPFileName -ErrorAction Stop
    }
    Catch
    {
        $DoesSolutionExists = $_
    }
    If (($DoesSolutionExists -like "*Cannot find an SPSolution*") -and ($output.Name -notlike  "*$WSPFileName*"))
    {
        Try
        {
            Write-Host -ForegroundColor Green "Adding solution to farm"
            Add-SPSolution "$WSPFullFileName" -Confirm:$false -ErrorAction Stop | Out-Null
 
            Write-Host -ForegroundColor Green "Checking Status of Solution"
            $output = Get-SPSolution -Identity $WSPFileName -ErrorAction Stop
            $gobal = $null
            if ($output.Deployed -eq $false)
            {
                try
                {
                    Write-Host -ForegroundColor Green "Deploy solution to all Web Apps, will skip if this solution is globally deployed"
                    Install-SPSolution -Identity "$WSPFileName" -GACDeployment -AllWebApplications -Force -Confirm:$false -ErrorAction Stop | Out-Null
                }
                Catch
                {
                    $gobal = $_
                }
                If ($gobal -like "*This solution contains*")
                {
                    Write-Host -ForegroundColor Green "Solution requires global deployment, Deploying now"
                    Install-SPSolution -Identity "$WSPFileName" -GACDeployment -Force -Confirm:$false -ErrorAction Stop | Out-Null
                }
            }
 
            Sleep 1
            $dpjobs = Get-SPTimerJob | Where { $_.Name -like "*$WSPFileName*" }
            If ($dpjobs -eq $null)
            {
                Write-Host -ForegroundColor Green "No solution deployment jobs found"
            }
            Else
            {
                If ($dpjobs -is [Array])
                {
                    Foreach ($job in $dpjobs)
                    {
                        $jobName = $job.Name
                        While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)
                        {
                            Write-Host -ForegroundColor Yellow -NoNewLine "."
                            Start-Sleep -Seconds 5
                        }
                        Write-Host
                    }
                }
                Else
                {
                    $jobName = $dpjobs.Name
                    While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)
                    {
                        Write-Host -ForegroundColor Yellow -NoNewLine "."
                        Start-Sleep -Seconds 5
                    }
                    Write-Host
                }
            }
        }
        Catch
        {
            Write-Error $_
            Write-Host -ForegroundColor Red "Skipping $WSPFileName, Due to an error"
            Read-Host
        }
    }
    Else
    {
        $skip = $null
        $tryagain = $null
        Try
        {
            if ($output.Deployed -eq $true)
            {
            Write-Host -ForegroundColor Green "Retracting Solution"
            Uninstall-SPSolution -AllWebApplications -Identity $WSPFileName -Confirm:$false -ErrorAction Stop
            }
        }
        Catch
        {
            $tryagain = $_
        }
        Try
        {
            if ($tryagain -ne $null)
            {
                Uninstall-SPSolution -Identity $WSPFileName -Confirm:$false -ErrorAction Stop
            }
        }
        Catch
        {
            Write-Host -ForegroundColor Red "Could not Retract Solution"
        }
 
        Sleep 1
        $dpjobs = Get-SPTimerJob | Where { $_.Name -like "*$WSPFileName*" }
        If ($dpjobs -eq $null)
        {
            Write-Host -ForegroundColor Green "No solution deployment jobs found"
        }
        Else
        {
            If ($dpjobs -is [Array])
            {
                Foreach ($job in $dpjobs)
                {
                    $jobName = $job.Name
                    While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)
                    {
                        Write-Host -ForegroundColor Yellow -NoNewLine "."
                        Start-Sleep -Seconds 5
                    }
                    Write-Host
                }
            }
            Else
            {
                $jobName = $dpjobs.Name
                While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)
                {
                    Write-Host -ForegroundColor Yellow -NoNewLine "."
                    Start-Sleep -Seconds 5
                }
                Write-Host
            }
        }        
 
        Try
        {
            Write-Host -ForegroundColor Green "Removing Solution from farm"
            Remove-SPSolution -Identity $WSPFileName -Confirm:$false -ErrorAction Stop
        }
        Catch
        {
            $skip = $_
            Write-Host -ForegroundColor Red "Could not Remove Solution"
            Read-Host
        }
        if ($skip -eq $null)
        {
            Try
            {
                Write-Host -ForegroundColor Green "Adding solution to farm"
                Add-SPSolution "$WSPFullFileName" -Confirm:$false -ErrorAction Stop | Out-Null
                $gobal = $null
                try
                {
                    Write-Host -ForegroundColor Green "Deploy solution to all Web Apps, will skip if this solution is globally deployed"
                    Install-SPSolution -Identity "$WSPFileName" -GACDeployment -AllWebApplications -Force -Confirm:$false -ErrorAction Stop | Out-Null
                }
                Catch
                {
                    $gobal = $_
                }
                If ($gobal -like "*This solution contains*")
                {
                    Write-Host -ForegroundColor Green "Solution requires global deployment, Deploying now"
                    Install-SPSolution -Identity "$WSPFileName" -GACDeployment -Force -Confirm:$false -ErrorAction Stop | Out-Null
                }
            }
            Catch
            {
                Write-Error $_
                Write-Host -ForegroundColor Red "Skipping $WSPFileName, Due to an error"
                Read-Host
            }
 
            Sleep 1
            $dpjobs = Get-SPTimerJob | Where { $_.Name -like "*$WSPFileName*" }
            If ($dpjobs -eq $null)
            {
                Write-Host -ForegroundColor Green "No solution deployment jobs found"
            }
            Else
            {
                If ($dpjobs -is [Array])
                {
                    Foreach ($job in $dpjobs)
                    {
                        $jobName = $job.Name
                        While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)
                        {
                            Write-Host -ForegroundColor Yellow -NoNewLine "."
                            Start-Sleep -Seconds 5
                        }
                        Write-Host
                    }
                }
                Else
                {
                    $jobName = $dpjobs.Name
                    While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)
                    {
                        Write-Host -ForegroundColor Yellow -NoNewLine "."
                        Start-Sleep -Seconds 5
                    }
                    Write-Host
                }
            }
    }
    Else
    {
        Write-Host -ForegroundColor Red "Cannot Install $WSPFileName, Please try manually"
        Read-Host
    }
}
}