IT tutorials
 
Technology
 

Sharepoint 2013 : Working with PowerShell (part 3) - PowerShell and SharePoint - Web Applications, Site Collections, Memory and Disposal

10/5/2013 8:01:05 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Web Applications

PowerShell includes a selection of Cmdlets to manipulate web applications. A web application is the hosting application that resides in Internet Information Server and hosts one or many site collections. Execute the following PowerShell command to obtain a list of web application Cmdlets:

Get-Command –Noun SPWebApplication

Table 3 provides a description of each of the web application–related Cmdlets.

Table 3. List of Web Application Cmdlets

Cmdlet Description
Convert-SPWebApplication Converts the authentication mode of an existing web application from Classic to Claims-Based Authentication (CBA). SharePoint 2010 used to allow you to create Classic Mode web applications via Central Administration, and this Cmdlet provided conversion to CBA after creation. Since the default authentication mode in SharePoint 2013 is CBA, this Cmdlet comes into play only if you create a Classic Mode web application via PowerShell and then decide to convert to CBA later.
Get-SPWebApplication Gets an instance of a SPWebApplication object. Pipe the result of this command to the Format-List Cmdlet to see the properties belonging to the object. Use this Cmdlet with other Cmdlets to make changes to or operate on an existing web application in your farm.
New-SPWebApplication Creates a new web application in the farm. This Cmdlet takes a number of parameters, including the content database, application pool managed account, URL, port, etc.
Remove-SPWebApplication Deletes an existing web application for all zones or a particular zone. Using this command preserves content databases and, depending on the –DeleteIISSite parameter, will delete the IIS site entry.
Set-SPWebApplication Takes in a SPWebApplication object and allows you to set properties on the web application. This Cmdlet provides behavior in PowerShell similar to how you can edit an existing web application’s settings via Central Administration.

Now that I have covered web applications in PowerShell, I shall examine site collections next, followed by sites and subsites.

Site Collections (SPSite)

A site collection represents the taxonomy of a web site, and hosts a hierarchy of sites. In Central Administration, you create, edit, and delete site collections under the Application Management section. Table 4 lists the available Cmdlets; you can see a list of these Cmdlets by executing the following command:

Get-Command –Noun SPSite

Table 4. List of Site Collection Cmdlets

Cmdlet Description
Backup-SPSite Performs a backup of a site collection to a file on disk. This Cmdlet locks the site collection while the backup process completes. The –UseSqlSnapshot parameter ensures integrity when backing up the site collection while allowing users to read and write to the active site collection.
Copy-SPSite Copies a site collection to a new URL and content database. This Cmdlet assumes the ability to create SQL snapshots and effectively provides the same operation as using the Backup and Restore Cmdlets with SQL snapshot and content database parameters. When copying the site collection, the new site collection has a new unique identifier.
Get-SPSite Gets a new instance of a SPSite object from a provided URL.
Move-SPSite Moves a site collection to a new URL and content database. During the move, SharePoint applies a no-access lock on the site collection so users cannot read or write to the site collection. Similar to the Copy Cmdlet, the new site collection has a new unique ID and resides in a new content database.
New-SPSite Creates a new site collection. This Cmdlet accepts parameters for site owners, target URL, and template for the new site collection.
Remove-SPSite Removes an existing site collection, including all contained sites and subsites.
Repair-SPSite Performs a health check on the site collection and automatically repairs any encountered issues (when possible). Run the Test-SPSite Cmdlet to get a report on repairs required.
Restore-SPSite Restores a site collection from a backup file on disk. This Cmdlet will not allow you to overwrite an existing site collection at the destination URL, unless you provide the –Force parameter. A content database may not host multiple site collections with the same unique identifier, which the backup operation preserves. Therefore, to restore a site collection to a different URL in the same farm as the backup, create a new content database and pass an object instance of this database to the Restore Cmdlet.
Set-SPSite Provides ability to set property values on a provided SPSite object and the site collection. To obtain a list of available properties, use the Format-List * Cmdlet with the SPSite object.
Test-SPSite Runs the same set of rules as the Repair-SPSite Cmdlet without repairs, and produces a report of issues.
Upgrade-SPSite Performs an upgrade on site collection as either build-to-build (default) or version-to-version. Execute this Cmdlet after patching for build-to-build. When executing in version-to-version mode the Cmdlet performs a health check before performing an upgrade.

The more common of the SPSite Cmdlets are the Backup and Restore Cmdlets. As administrators, we are familiar with the STSADM backup and restore commands, which provide granular backup of complete site collections (often used when deploying a version of a web solution from development to staging and production). The new PowerShell Backup and Restore Cmdlets support the use of SQL snapshots, which ensures integrity of the backup, while allowing users to continue reading and writing to the active site collection. STSADM backup places a read-only lock on the site collection, prohibiting backup of a busy SharePoint site collection during business hours.

Sites (SPWeb)

Consider a modern intranet or public web site, which consists of multiple levels, accessible via navigation elements. A site represents a single level in a site hierarchy. In SharePoint, a single site contains document libraries, lists, and subsites. It does not matter if the site is a publishing site, team site, meeting site, etc. The site represents one node in the overall site taxonomy.

The SharePoint object model, and hence PowerShell, represents a site as a SPWeb object. Similar to other objects in PowerShell, you can get an idea of the number of properties associated with the SPWeb object by using the following command:

Get-SPWeb http://web-url | Format-List *

Table 5 lists the available Cmdlets, using the following command:

Get-Command –Noun SPWeb

Table 5. List of Site Cmdlets

Cmdlet Description
Export-SPWeb Exports a subsite hierarchy, starting at a particular site node in the site collection, to a file on disk (if using compression, or files in a directory otherwise). Unlike backing up a site collection, exporting a subsite hierarchy allows you to attach the sub-tree to another site collection or another subsite in a different location. Export assigns new IDs to objects. This Cmdlet provides export of the entire site (and subsites), or a single document library, or single list in the site.
Get-SPWeb Returns a series of SPWeb objects representing sites that match the scope in the identity parameter –URL or site collection SPSite object.
Import-SPWeb Imports a previously exported site hierarchy, list, or library to an existing site, whether the root site in the site collection or another subsite.
New-SPWeb Creates a new subsite under an existing site, whether the parent is the root site in the collection or an existing subsite in the hierarchy. This Cmdlet expects the URL of the new site, which resides under an existing site in the site collection. You may provide a template for the new site or assume the default template associated with the site collection.
Remove-SPWeb Removes a site from the site collection and any subsites belonging to the site specified. Deleting the root (top-level) site in a site collection causes deletion of the site collection, since every site collection must include at least one site.
Set-SPWeb Provides ability to set the value of a property on an existing SPWeb object. Use the Format-List * Cmdlet to get a list of available properties.

Typically, the most common of the preceding SPWeb Cmdlets is the Get-SPWeb Cmdlet, with which you might perform multiple operations via different SP Cmdlets. The SPSite site collection maintains a list of all SPWeb objects available in the site collection, which you can iterate as follows:

(Get-SPSite http://sp2013).AllWebs | Foreach-Object { $_.Title }

The preceding command gets SPWeb objects via the AllWebs collection on the SPSite object returned by the Get-SPSite Cmdlet. In my example, I am simply displaying the title of each site in the site collection, but I could just as easily pass the SPWeb object to a Cmdlet that performs an operation on the SPWeb object.

Memory and Disposal

Developers of SharePoint applications understand the need for memory disposal practice. SharePoint objects, such as SPSite, SPWeb, etc., consume resources, and it is the developer’s responsibility to release these resources by calling the Dispose method on these types of objects when done using them. Do you have to concern yourself with memory disposal when using these objects in PowerShell? Fortunately, PowerShell, in conjunction with the SharePoint object model, assists in the disposal of expensive SharePoint objects.

Using the SharePoint Management Shell, rather than the standard Windows PowerShell, because each command or single-line pipeline runs in a single thread. This is important when using objects like SPSite and SPWeb, because we often like to pipe these objects and Cmdlets that return these objects together, which would ordinarily leak memory in a multi-threaded Windows PowerShell window.

So, how does PowerShell manage memory disposal? I shall provide an example.

Get-SPWebApplication | Get-SPSite –limit all | Foreach-Object { $._Url }

The preceding command iterates over every site collection for every web application in the farm. For each site collection, the command displays the URL. The issue with this command is that the call to Get-SPSite for each web application uses space in memory for the new SPSite object. In a standard Windows PowerShell, not disposing of the SPSite object after each iteration would cause a memory leak. The SharePoint Management Shell caters to these scenarios and ensures disposal of all objects allocated in the preceding pipeline sequence.

Now, consider the following scenario, where SPSite and SPWeb objects are used over several lines of a PowerShell script:

$site = Get-SPSite " http://sp2013 "
Foreach ($web in $site.AllWebs) {
      Write-Host $web.Title
}

The preceding script gets a new instance of a SPSite object for a site collection URL, then iterates every site in the site collection (represented by SPWeb objects) displaying the title. In this example, PowerShell cannot help you because it has no way of knowing whether to dispose of the SPSite object nor each SPWeb object in the iteration, since the operations span several lines in the script. In this case, disposal is the responsibility of the script developer.

As a rule, I like to dispose of all SharePoint objects that implement the IDisposable interface. The following modified script ensures disposal:

$site = Get-SPSite " http://sp2013 "
try {
  Foreach ($web in $site.AllWebs) {
    try {
      Write-Host $web.Title
    } finally {
      $web.Dispose();
    }
  }
} finally {
  $site.Dispose();
}

Although the second version of the script ensures disposal of memory, the script is messy. Developers writing code in C# have a shortcut to clean up disposable objects with the using keyword. In PowerShell, we also have a shortcut, shown as follows:

Start-SPAssignment –Global
$site = Get-SPSite " http://sp2013 "
Foreach ($web in $site.AllWebs) {
      Write-Host $web.Title
}
Stop-SPAssignment –Global

Wrapping our script between the Start-SPAssignment –Global and Stop-SPAssignment –Global statements ensures that PowerShell disposes of any object allocated and assigned to the global space. Objects in the global space are those that the script has not scoped to a particular named space. The following example shows the script using two scoped name spaces:

$siteScope = Start-SPAssignment
$site = Get-SPSite " http://sp2013 "
for ($i=0;$i -lt $site.AllWebs.Count; $i++) {
  $webScope = Start-SPAssignment
  Write-Host $site.AllWebs[$i].Title
  Stop-SPAssignment $webScope
}
Stop-SPAssignment $siteScope

As you have discovered, memory management of SharePoint objects in PowerShell is important. If you are writing small scripts that have a short execution time—as in they complete within a few seconds—then memory-management is probably not one of your major concerns. However, if you have a script that iterates a large site collection and performs significant processing, then you can easily find yourself eating memory until your PowerShell instance crashes.

Before I conclude this section on memory and disposal, there is one more important point to be aware of—PowerShell does not clean up leaked objects at the end of scripts. In other words, if you have a leaky script, which you execute several times (perhaps because you are debugging), PowerShell is unable to claim the leaked memory until you terminate the PowerShell process. Again, this is not a big consideration if you are executing your scripts in a short window of time, but if you have a PowerShell script running for a lengthy duration, pay close attention to the amount of memory the PowerShell process consumes.

 
Others
 
- Sharepoint 2013 : Working with PowerShell (part 2) - PowerShell and SharePoint - Administration Permissions, Content Databases
- Sharepoint 2013 : Working with PowerShell (part 1) - PowerShell Basics
- Exchange Server 2010 Quick Start Guide : Installing Exchange Server 2010
- Exchange Server 2010 Quick Start Guide : Configuring Windows Server 2008 R2
- Exchange Server 2010 Quick Start Guide : Server Sizing Quick Reference
- Microsoft Lync Server 2010 : UCMA (part 3) - Walkthrough of the UCMA 3.0 Components - Toolbox Components, Error Condition Components
- Microsoft Lync Server 2010 : UCMA (part 2) - Walkthrough of the UCMA 3.0 Components
- Microsoft Lync Server 2010 : UCMA (part 1) - Server APIs ,Client APIs , PowerShell
- Sharepoint 2010 : Querying SharePoint Data (part 2) - Querying Using LINQ
- Sharepoint 2010 : Querying SharePoint Data (part 1) - Querying Using CAML
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us