Sunday, September 18, 2016

SharePoint PowerShell - BackUps and Restore

SharePoint: PowerShell - BackUps and Restore

Open Windows PowerShell using Run as admin
PC> add-pssnapin Microsoft.sharepoint.powershell

BackUp & Restore in SharePoint 2013

In SharePoint 2013, you can use Windows PowerShell functionality to perform a backup or restore process for the following items:
  • Farm
  • Site collection
  • Configuration database
  • Services
  • List or document

Farm BackUp
Backup-SPFarm -BackupMethod -Directory [-AssignmentCollection ] [-BackupThreads ] [-ConfigurationOnly ] [-Confirm []] [-Force ] [-Item ] [-Percentage ] [-WhatIf []]

Another Syntax(This doesn’t backups actually, it shows the items to be backed up in this command):

Backup-SPFarm -ShowTree [-AssignmentCollection ] [-ConfigurationOnly ] [-Confirm []] [-Item ] [-WhatIf []]

PS C:\Windows\system32> backup-spfarm -directory \\KSSHRPT01\FarmBackUp -backupmethod full
PS C:\Windows\system32> backup-spfarm -directory \\KSSHRPT01\FarmBackUp -backupmethod full -ConfigurationOnly

*The shared path shall be accessible from all the server. You can use local PC path in case if you have single server farm and have both SP and DB are installed on the same machine.

BackUp a Site collection
Backup-SPSite [-Identity] -Path [-AssignmentCollection ] [-Confirm []] [-Force ] [-NoSiteLock ] [-UseSqlSnapshot ] [-WhatIf []]

PS C:\Users\spadmin> $s = "http://ksshrpt01"
PS C:\Users\spadmin> $pth = "C:\KS\SiteBackUps\Primarysite.bak"
PS C:\Users\spadmin> backup-spsite $s $pth
PS C:\Users\spadmin> backup-spsite $s -path  $pth

Another way to do the same:

PS C:\Users\spadmin> get-spsiteadministration $s | Backup-spsite -path $pth

PS C:\Users\spadmin> backup-spsite $s -path  $pth -usesqlsnapshot -force
This example backs up a site collection using database snapshots to ensure backup integrity.Specifies a SQL Database Snapshot will be created when the backup begins, and all site collection data will be retrieved directly from the database snapshot. This snapshot will be deleted automatically when the backup completes
The UseSqlSnapshot parameter is recommended if the database server hosting your content database supports database snapshots such as SQL Server Enterprise Edition and SQL Server Developer Edition. This is because it will ensure a valid backup while allowing users to continue reading and writing to the site collection during the backup. It is not necessary to specify the NoSiteLock parameter when specifying the UseSqlSnapshot parameter.

PS C:\Windows\system32> backup-spfarm -showtree -item "Microsoft sharepoint foundation web application" -verbose
*Gives the tree path of back procedure, but doesn’t actually backs up the Farm.

PS C:\Windows\system32>Backup-SPFarm -Directory C:\Backup -BackupMethod full -BackupThreads 10 -Force
This example performs a backup of a farm using 10 threads and forces the backup to be saved to the C:\Backup directory even though SharePoint estimates that it does not have sufficient space available.

Performs a farm-level configuration-only backup.


Backup-SPConfigurationDatabase -Directory [-AssignmentCollection ] [-DatabaseCredentials ] [-DatabaseName ] [-DatabaseServer ] [-Item ]

Backup-SPConfigurationDatabase -ShowTree [-AssignmentCollection ] [-DatabaseCredentials ] [-DatabaseName ] [-DatabaseServer ] [-Item ]

The Backup-SPConfigurationDatabase cmdlet performs a configuration-only backup of the current farm or a configuration-only backup of a separate configuration database which is not attached to the current farm. If you wish to perform a configuration-only backup of the current farm, there is no need to specify the DatabaseServer and DatabaseName parameters. However, using the Backup-SPFarm cmdlet with the ConfigurationOnly parameter is recommended for such a scenario.
An example of a configuration backup is an administrator creates a farm configuration template which then can be applied to other SharePoint farms by performing a restore using the Restore-SPFarm cmdlet.

PS>Backup-SPConfigurationDatabase -DatabaseName SharePoint_Config -DatabaseServer SqlServer1 -Directory \\server\share\Backup -ShowTree

This example displays components that are available for inclusion in a configuration-only backup.

PS>Backup-SPConfigurationDatabase -DatabaseName SharePoint_Config -DatabaseServer SqlServer1 -Directory \\server\share\Backup -Verbose
This example performs a configuration-only backup with verbose output.


Returns a history of backup and restore operations.
The Get-SPBackupHistory cmdlet reads a history of backup and restore operations that have been run. Specifies whether you want to display only the backup history, only the restore history, or all of the history.

Get-SPBackupHistory -Directory [-AssignmentCollection ] [-ShowBackup ] [-ShowRestore ]

PS C:\Windows\system32> Get-SPBackupHistory -Directory \\KSSHRPT01\FarmBackUp

T S Operation ID                         Method    Start Time
- - ------------                         ------    ----------
B Y 3682c4c6-7eb8-4f61-9714-9870e67d9948 Full

PS C:\Windows\system32>

PS C:\Windows\system32>Get-SPBackupHistory -Directory C:\Backup -ShowBackup
This example returns all of the farm backup operations that have been run for the C:\Backup directory.

PS C:\Windows\system32>(Get-SPBackupHistory -Directory C:\Backup -ShowBackup)[0].SelfId | Restore-SPFarm -Directory C:\Backup -RestoreMethod overwrite

This example gets all of the farm backup operations that have been run for the "C:\Backup" directory, finds the most recent backup, and then passes its backup GUID to the Restore-SPFarm cmdlet. The Restore-SPFarm cmdlet will then perform an overwrite restore from that backup package.

Restores one or more items from a backup.

This cmdlet contains more than one parameter set. You may only use parameters from one parameter set, and you may not combine parameters from different parameter sets. For more information about how to use parameter sets, see Cmdlet Parameter Sets.
The Restore-SPFarm cmdlet restores one or more items from a backup such as an individual database, Web application, or the entire farm. This cmdlet can also be used to apply a farm template to the entire farm.

Restore-SPFarm -Directory -RestoreMethod [-AssignmentCollection ] [-BackupId ] [-ConfigurationOnly ] [-Confirm []] [-FarmCredentials ] [-Force ] [-Item ] [-NewDatabaseServer ] [-Percentage ] [-RestoreThreads ] [-WhatIf []]

Restore-SPFarm -Directory -ShowTree [-AssignmentCollection ] [-BackupId ] [-ConfigurationOnly ] [-Confirm []] [-Item ] [-WhatIf []]


PS>Restore-SPFarm -Directory \\file_server\share\Backup -BackupId 12345678-90ab-cdef-1234-567890abcdef -RestoreMethod new -ConfigurationOnly

This example restores the configuration settings from the backup package in the \\file_server\share\Backup directory to the farm.

PS>Restore-SPFarm -ShowTree -Directory \\file_server\share\Backup -BackupId 12345678-90ab-cdef-1234-567890abcdef -Item "Microsoft SharePoint Foundation Web Application" -Verbose

This example show which components of the farm would be restored under the Microsoft SharePoint Foundation Web Application node, but does not actually restore them.

PS>Restore-SPFarm -Directory C:\Backup -BackupId 12345678-90ab-cdef-1234-567890abcdef  -RestoreMethod overwrite -RestoreThreads 10 -Force

This example restores a farm by using 10 threads and suppresses the overwrite warning.

Restores a site collection.
The Restore-SPSite cmdlet performs a restoration of the site collection to a location specified by the Identity parameter. A content database may only contain one copy of a site collection. If a site collection is backed up and restored to a different URL location within the same Web application, an additional content database must be available to hold the restored copy of the site collection.


PS>Restore-SPSite http://server_name/sites/site_name -Path C:\Backup\site_name.bak

This example restores a site collection from the backup file C:\Backup\site_name.bak to the site collection URL http://server_name/sites/site_name.

PS>Restore-SPSite http://server_name/sites/site_name -Path C:\Backup\site_name.bak -Force -DatabaseServer SQLBE1 -DatabaseName SQLDB1

This example restores a site collection backup from the backup file
C:\Backup\site_name.bak, but overwrites the existing site collection at http://server_name/sites/site_name while specifying that the site collection must be stored in a specific content database.

PS>Restore-SPSite -Path \\file_server\share\site_name.bak -HostHeaderWebApplication http://server_name

This example restores a site collection backup from the backup file \\file_server\share\site_name.bak to the host-named site collection on the Web application http://server_name.

Reference: MSDN

SharePoint PowerShell to Manage Term Store / Add Terms using PowerShell

SharePoint PowerShell to Manage Term Store / Add Terms

//get the term store
$mysite = get-spsite “http://yoursite:portnumber
$taxonomySession = get-taxonomySession -site $mysite
$termStore = $taxonomySession.TermStores[“Managed Metadata Service”]

//now we will create the Term Store groups. Before we create any group we will check if the group already exists
Example 1:

if($termstore.groups["News Keys"] -eq $null) { $termstoregroup = $termstore.creategroup("News Keys");  $termstore.description = "News Group";  $termstore.addgroupmanager ("KS\spadmin"); $termstore.addcontributor ("ks\k.singh");$termStore.CommitAll();}

This is not working as “description” is not an identified property.

Example 2:

if($termstore.groups["Main News Keys"] -eq $null) { echo "group is not there";  echo "group is there"; $termstoregroup = $termstore.creategroup("Main News Keys"); $termstore.commitall();   }

Example 3:
$session = Get-SPTaxonomySession -Site "http://ksshrpt01/";
$termStore = $session.TermStores[“Managed Metadata Service”];
$group = $termstore.CreateGroup(“News Group”);
$group.Description = "My News Group";
$termStore.addgroupmanager ("KS\spadmin");
$termSet = $group.CreateTermSet(“Sports”,1033);
$termSet.Description = “Sports”;
$termSet.IsAvailableForTagging = $true;
$termSet.IsOpenForTermCreation = $true;

I encountered many issues/errors and finally went through. I will advise the following troubleshooting tips.

  1. termstore.description("") - Invalid command. This never worked for me. I shall update this once I come across any solution to this. Remove this cmdlet if you too face the same issue.
  2. I faced issue related to Permission (UnAuthorized User). I started the PowerShell console as Administrator and still I had the same issue.
  3. Central Admin -> Manage Service applications -> Managed Metadata Service -> Select and go to permission. Assign permission to the user running PowerShell.
  4. Central Admin -> Service Applications->Managed Metadata Service -> Properties -> Set term store administrator as the user you want. Save it and reset IIS as a precautionary measure.

The Create TermStore Group command shall now work for you.

Both of the following commands worked well.

  1. PS> if($termstore.groups["Main News Keys"] -eq $null) { echo "group is not there";  $termstoregroup = $termstore.creategroup("Main News Keys");  $termstore.commitall();   }
  2. PS>$session = Get-SPTaxonomySession -Site "http://ksshrpt01/";
PS>$termStore = $session.TermStores[“Managed Metadata Service”];
PS>$group = $termstore.CreateGroup(“News Group”);
PS>$group.Description = "My News Group";

*We will use the second command for all further examples

Please check the following screen.

Use following the command to assign permission to the group.

PS> $termstoregroup.addGroupManager("ks\spadmin")
PS> $termstoregroup.AddContributor("ks\k.singh")

You need to commit the changes to the Term Store.


Let’s create Term Sets in the Term Store

PS> $newTermSet = $termstoregroup.CreateTermSet("Sports",1033)
PS> $newTermSet = $termstoregroup.CreateTermSet("Politics",1033)
PS C:\Users\spadmin> $newTermSet = $termstoregroup.CreateTermSet("Nation",1033)
PS C:\Users\spadmin> $newTermSet = $termstoregroup.CreateTermSet("Business",1033)
PS C:\Users\spadmin> $termstore.commitall()

Hope this helps…..

Visual Studio Build Error - The "GenerateResource" task failed unexpectedly

Error: "The GenerateResource task failed unexpectedly" This error is quite common while building the project in Visual Studio....