SPHOL322: Introduction to Windows PowerShell With SharePoint Server 2013

2013 SPHOL322: Introduction to Windows PowerShell With SharePoint Server 2013 Hands-On Lab Lab Manual  This document is provided “as-is”. Informati...
Author: Derek Long
49 downloads 0 Views 3MB Size
2013 SPHOL322: Introduction to Windows PowerShell With SharePoint Server 2013 Hands-On Lab Lab Manual 

This document is provided “as-is”. Information and views expressed in this document, including URL and other Internet Web site references, may change without notice. You bear the risk of using it. This document does not provide you with any legal rights to any intellectual property in any Microsoft product. You may copy and use this document for your internal, reference purposes. © 2012 Microsoft. All rights reserved.



Contents Contents ................................................................................................................................................................. 1 INTRODUCTION TO WINDOWS POWERSHELL WITH SHAREPOINT SERVER 2013 ........................................ 2 Lab Objectives........................................................................................................................................................ 2 Technologies .......................................................................................................................................................... 2 Audience ................................................................................................................................................................ 2 Scenario ................................................................................................................................................................. 2 Getting Started and Setting up your SharePoint 2013 Management Shell Environment ...................................... 2 Connect to the Lab Environment ....................................................................................................................... 2 Exercise 1: Explore the SharePoint 2013 Management Shell ............................................................................... 4 Task 1 – Log In and Configure the SharePoint 2013 Management Shell Environment .................................... 4 Task 2 – Retrieving Information from the Server Farm ...................................................................................... 6 Commonly Used Get Cmdlets ........................................................................................................................... 8 Task 3 – Feature Management .......................................................................................................................... 9 Task 4 – Create Site Settings and Properties .................................................................................................. 11 Exercise 2: Using Wildcards, Filters, and Combining cmdlets in Windows PowerShell ...................................... 15 Task 1 – Combining Cmdlets in Windows PowerShell .................................................................................... 15 Task 2 – Using Filters and Wildcards with Windows PowerShell Cmdlets ...................................................... 18 Task 3 – Enumerating Collection Commands and Administrative Tasks with PowerShell ............................. 20 Exercise 3: Perform Advanced Tasks with PowerShell ....................................................................................... 23 Task 1 – Using Local Variables with PowerShell cmdlets ............................................................................... 23 Task 2 – Using Granular Memory Control Cmdlets ........................................................................................ 24 Task 3 – Using the Object Model in PowerShell to Change Site Settings....................................................... 27 Summary .............................................................................................................................................................. 31

Page 1

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

Introduction to Windows PowerShell With SharePoint Server 2013 Estimated time to complete this lab: 90 minutes

Lab Objectives The objective of this lab is to introduce you to using Windows PowerShell to administer a SharePoint Server 2013 environment. The lab begins with a beginner’s introduction to the SharePoint 2013 Management Shell. You’ll move onto exploring how you can use PowerShell scripting to administer your SharePoint Server 2013 farm, learn how to assign and create variables, and use the SharePoint object model from Windows PowerShell, and learn Windows PowerShell scripting techniques that simplify administration tasks in SharePoint Server 2013. After completing this lab, you will be better able to:   

Find your way around the SharePoint 2013 Management Shell and interact with SharePoint Web applications, site collections, and sites. Use Windows PowerShell scripting techniques, such as pipes, filters, wildcards, and enumerations, for SharePoint Server 2013 administration. Explain how to create and assign variables and use the SharePoint object model from Windows PowerShell.

Technologies 

Windows PowerShell, SharePoint Server 2013

Audience 

SharePoint Site Administrators and SharePoint IT Professionals

Scenario This lab takes you through an introduction to using the Windows PowerShell in SharePoint Server 2013 Search user experience with emphasis on exploring the cmdlet interface, new features, configuration options, and customization of SharePoint Servers.

Getting Started and Setting up your SharePoint 2013 Management Shell Environment Connect to the Lab Environment Log on to the SharePoint (SP) virtual machine (VM) as Administrator, with the credentials CONTOSO\Administrator, pass@word1. Microsoft Confidential

Page 2

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

After you have logged on to the VM, start Internet Explorer, navigate to the demo home page at http://intranet.contoso.com/sites/contoso and log into SharePoint as Administrator with the following credentials: Username: CONTOSO\Administrator Password: pass@word1

Microsoft Confidential

Page 3

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

Exercise 1: Explore the SharePoint 2013 Management Shell Estimated time to complete this exercise: 30 minutes In this exercise, you will familiarize yourself with the SharePoint 2013 Management Shell, learn how to find help and information, and start using PowerShell commands (known as cmdlets). You’ll also explore the SharePoint 2013 Management Shell and learn how to use Windows PowerShell to accomplish some simple administrative tasks. If you are familiar with previous versions of SharePoint Products and Technologies, you have probably used the stsadm command-line tool to perform various administrative tasks. Windows PowerShell offers a new approach to administration in SharePoint Server 2013 through the SharePoint 2013 Management Shell. Windows PowerShell combines the immediacy of the commandline with the power and flexibility of managed code. Task 1 – Log In and Configure the SharePoint 2013 Management Shell Environment In this task, you will get your environment set up to use the SharePoint Management Shell, and log into the system. To begin, make sure you are logged into the SharePoint Server 2013 Virtual Machine environment, and launch the SharePoint 2013 Management Shell. 1. Make sure you are logged on to the SharePoint Server 2013 virtual machine as: User: CONTOSO\Administrator Password: pass@word1 2. Launch the SharePoint 2013 Management Shell. Click Start, then click Microsoft SharePoint 2013 Products, and then click SharePoint 2013 Management Shell. The SharePoint 2013 Management Shell command line window will open.

Microsoft Confidential

Page 4

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

3. At the PS > prompt, type the following command and press ENTER: Get-Command –pssnapin "Microsoft.SharePoint.PowerShell" |more

This command displays all the available cmdlets in the Microsoft.SharePoint.PowerShell 2013 namespace. Press SPACE to page down the list. As you can see, there are hundreds of cmdlets relating to a wide range of administrative tasks. Note:

You can also type gcm as shorthand for Get-Command.

To get help and information on a particular cmdlet, you can use the Get-Help command. 4. Type the following command, and then press ENTER: get-help Get-SPSite

The console window displays information on how to use the Get-SPSite cmdlet. As you might have guessed, cmdlets that include the Get prefix return information. You can use these cmdlets to write information to the console, or to provide information to other cmdlets. Microsoft Confidential

Page 5

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

You can also use the Get-Help -detailed OR Get-Help -full command options to retrieve more detailed technical information on a specific cmdlet. As some of these help files can be very long, remember that you can append < | more, and then click to any command if you want to page the command output. 5. Type the following command, and then press ENTER: Get-SPSite –WebApplication http://intranet.contoso.com

This command returns all the site collections that exist in the http://intranet.contoso.com Web application.

You can use the same approach to retrieve information at each level of the SharePoint architecture. For example, you can retrieve all the Web applications in your server farm, all the site collections within a specific Web application, or all the individual sites within a site collection. We will explore this concept in the next task. Task 2 – Retrieving Information from the Server Farm The SharePoint snap-in for Windows PowerShell includes many straightforward cmdlets that can retrieve information from your SharePoint Server farm without requiring the ability to write sophisticated scripts. In addition to being invaluable tools for day-to-day administration tasks, the cmdlets are a starting point to learn Windows PowerShell. Some of these tasks include retrieving information from various sources so you can perform daily management tasks, such as a SharePoint server farm. In this task, you will explore cmdlets that retrieve information from a SharePoint server farm. When you’re not sure which cmdlet you need to perform a particular task, you can use the GetCommand cmdlet to find out what is available. Let's say you want to review which cmdlets can retrieve information from SharePoint Server 2013. Microsoft Confidential

Page 6

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

1. Type the following command, and press ENTER: Get-Command Get-SP* |more

The console window displays the names of all the cmdlets that start with Get-SP. The verb Get is common to all cmdlets that return information, and the noun prefix SP is common to all SharePoint cmdlets. The asterisk ( * ) is a wildcard character that returns any of a defined subset of all possible characters.

Note:

You can use the same approach to find cmdlets relating to specific areas of functionality. For example, will return all service-related cmdlets.

Microsoft Confidential

Page 7

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

Commonly Used Get Cmdlets Two of the commonly used Get cmdlets are Get-SPServiceApplication Identity and GetSPServiceApplication – Select ID, Name. 2. Type the following command, and then press ENTER: Get-SPServiceApplication | Select ID, Name

The console window displays the ID and Name properties of each service application that is running in the server farm. Let's say a review of the event logs identifies a problem with a Service Application, and provides a GUID of: < 66c958d1-d10a-4880-bfc3-8b2646b8e4cd >. You need to find out which of the current service applications is causing the problem to be able to fix it. Use the Get-SPServiceApplication – Identity cmdlet to find it. 3. Type the following command, and then press ENTER: Get-SPServiceApplication –Identity 66c958d1-d10a-4880-bfc3-8b2646b8e4cd

The console window provides details of the service application and identifies the corresponding IIS application pool with DisplayName, TypeName, and the ID of the service application.

Microsoft Confidential

Page 8

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

Task 3 – Feature Management In this task, you will learn how to use the PowerShell commands to find GUID and other information about features in the SharePoint server. In the course of many routine deployment and troubleshooting tasks, you will need to find the GUID that corresponds to a particular SharePoint feature. In previous versions of SharePoint products and technologies, this tended to be a long, complicated task. As a result, administrators would find themselves coming up with complicated strategies or resort to performing full-text searches of the Features folder on the file system. SharePoint Server 2013 provides a far more straightforward approach to this problem through Windows PowerShell. 1. To get a list of all the installed features in a SharePoint server farm, type the following command, and then press ENTER: Get-SPFeature |more

The console window provides a list of all the installed features in the server farm, together with an ID and scope for each feature. Note:

The full returned list of –SPFeatures can be very long. Use the < |more> syntax to shorten the list until you press the SPACE button.

In addition to retrieving a list all the installed features in a server farm, you may want to retrieve a list of all the features that are activated to a particular scope. To do this, you can add switch parameters to the Get-SPFeature cmdlet (Site, Web, WebApplication, or Farm). 2. Type the following command, and then press ENTER: Get-SPFeature –Site http://intranet.contoso.com |more

The console window shows a list of all activated features on the intranet.contoso.com site collection. Microsoft Confidential

Page 9

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

One particularly useful application of PowerShell for SharePoint Server 2013 administrators is PowerShell's built-in ability to parse SharePoint log files. 3. To parse a SharePoint log file, type the following command, and then press ENTER: Get-SPLogEvent | Select –Last 10

The console window displays the last ten messages that were written to the log files. Note: This command may take several seconds to complete.

The Select cmdlet can be used to limit the number of items returned from other cmdlets that return large numbers of results. As you work through the remaining exercises in this lab, you will see how to use this technique and others to create sophisticated filtering techniques to return specific information.

Microsoft Confidential

Page 10

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

Task 4 – Create Site Settings and Properties In this task, you will explore how you can put these cmdlets to use in your SharePoint server farm to create sites and manage site setting properties using the Set-SPSite cmdlet. In addition to retrieving information, the SharePoint snap-in for Windows PowerShell includes cmdlets that can set properties, create new items, and perform a wide range of other actions. Let’s start by examining the Set-SPSite cmdlet. You can use this cmdlet to configure a range of properties on a specific site collection. 1. Type the following command, and then press ENTER: Set-SPSite –Identity http://intranet.contoso.com –SecondaryOwnerAlias CONTOSO\alexd

2. To confirm that the site settings have changed, launch the SharePoint site’s Central Administration Web site. Click Start, then click SharePoint 2013 Products, and then click SharePoint 2013 Central Administration. 3. On the Central Administration home page, click the Application Management heading. 4. Under Site Collections, click Change site collection administrators. 5. On the Site Collection Administrators page, make the following changes: 6.

Change the Site Collection to intranet.contoso.com.

7. Verify that AlexD has been added as a secondary site collection administrator.

Microsoft Confidential

Page 11

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

On a single, isolated SharePoint site, it may be faster to set the secondary site collection administrator through the Central Administration user interface. However, let’s say you want to add a secondary site collection administrator to over two hundred (200) site collections. Rather than configuring hundreds of site collections individually in the Central Administration interface, you can use Set-SPSite to create a PowerShell script that loops through the site collections and change the site collection administrator on each one. In the second part of this task, let’s take a look at to use Windows PowerShell to create new sites. 8. Switch back to the SharePoint 2013 Management Shell. 9. Type the following command, and press ENTER: New-SPSite –Url http://intranet.contoso.com/sites/powershell –OwnerAlias CONTOSO\Administrator -Name "PowerShell Site" -Template STS#0

Note:

If you do not specify a template in this step, you will be directed to a template selection page when you browse to the site. This particular cmdlet may take up to five (5) minutes to complete. To continue with this HOL and move to Exercise 2, open a new Windows PowerShell Management window.

Microsoft Confidential

Page 12

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

10. When the command returns, switch to the browser window and browse to http://intranet.contoso.com/sites/powershell to verify that a new team site has been created at the URL. You may need to confirm your identity before accessing the site.

11. Optionally, you could also use the following cmdlet in Windows PowerShell sample to return a list of sites on http://intranet.contoso.com and confirm that the site has been added: Get-SPSite http://intranet.contoso.com/sites/*

Microsoft Confidential

Page 13

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

In this exercise, you learned to log in and configure your SharePoint 2013 Windows PowerShell environment. You also learned the basics of retrieving information from the server farm and some of the basic, most commonly-used Get- cmdlets. You began working with feature management cmdlets in Windows PowerShell, and you also learned how to create site settings and set the site properties using Windows Powershell.

Microsoft Confidential

Page 14

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

Exercise 2: Using Wildcards, Filters, and Combining cmdlets in Windows PowerShell Estimated time to complete this exercise: 30 minutes Up to this point in this HOL, you have only used simple SharePoint cmdlets in isolation – IE, on a single SharePoint Server site. But what if you want to manage multiple sites with single command scripts? The real benefits to administrators become apparent when you start to use some of the more sophisticated features of Windows PowerShell, such as combining cmdlets, creating filters, and using wildcards. Using these allows you to do multiple tasks and combine actions for more than one SharePoint Server and site. In this exercise, you will explore how to use these features to your advantage when you perform administrative tasks in the SharePoint 2013 Management Shell, using combinations of cmdlets, filters and wildcard searches, and use enumerated collections to perform complicated administration tasks for SharePoint server farms using the Windows PowerShell interface. Task 1 – Combining Cmdlets in Windows PowerShell In this task, you will learn how to combine cmdlets and how to pass data from one cmdlet to another. 1. In the SharePoint 2013 Management Shell, type the following command, and then press ENTER. Get-Help Get-SPWeb

Microsoft Confidential

Page 15

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

Note that the Site parameter accepts an object of type SPSitePipeBind. This indicates that in addition to providing a value for the parameter directly, you can also use the pipe character ( | ) to pass in the value from the output of another cmdlet. 2. Type the following command, and then press ENTER. Get-SPSite http://intranet.contoso.com | Get-SPWeb

This command uses the Get-SPSite cmdlet to get a reference to the site collection at intranet.contoso.com, then passing this reference to the Get-SPWeb cmdlet. The Get-SPWeb cmdlet then returns all the sites at the passed-in site collection. Functionally, this is equivalent to the following command: Get-SPWeb –Site http://intranet.contoso.com

Why use the pipe character ( | ) at all if you can achieve the same result without it? As your PowerShell scripts become more complex and sophisticated, you may not always know the values needed to pass into a cmdlet in advance. In those cases, using the pipe character ( | ) allows you to set wildcards and search strings that return usable values for your PowerShell script, and helps make your PowerShell script more efficient. You can also use cmdlets directly to provide parameter values for other cmdlets. Consider the GetSPLogEvent cmdlet from earlier in this HOL. Let's say you wanted to retrieve all events that happened in the last five minutes. 3. To obtain the log events from the last five minutes, type the following command, and then press ENTER. Get-SPLogEvent –StartTime (Get-Date).AddMinutes(-5)

The console window displays a list of the events which occurred in the last five minutes. Note:

As you haven’t yet configured event log throttling and filtering, you might note the returned

Microsoft Confidential

Page 16

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

list is a very long one. We'll learn how to limit the returned results later in this lab.

In this example, the Get-Date cmdlet returns an object of type DateTime. The DateTime class includes a method named AddMinutes that returns the current date and time adjusted by the number of minutes that you pass in – in this case, the five minutes previous to the exact current time and date (Get-Date).AddMinutes(-5). The Get- SPLogEvent cmdlet uses this value as the input for the StartTime parameter, retrieving all events that occurred in the five minutes previous to the timedate returned by the cmdlet. If you use the same approach to specify the EndTime parameter, you can retrieve all the events that occurred in a specific time window. 4. To get all the events that occurred between fifteen minutes ago and one minute ago, type the following command, and then press ENTER: Get-SPLogEvent –StartTime (Get-Date).AddMinutes(-15) –EndTime (Get-Date).AddMinutes(-1)

The console window now displays another long list of the events which occurred between fifteen minutes ago and one minute ago. The last technique we'll explore in this task is how you can use the Select keyword to process the results returned by a cmdlet. Let's say you want to retrieve a list of all the event categories in which a single unique error occurred. 5. To return a list of all the event categories in which events of a severity “WARNING” occurred, type the following command in the Windows PowerShell command window, and press ENTER. Get-SPLogEvent –MinimumLevel Warning | Select Category –Unique

The console window indicates that the only warning has occurred in the following categories.

Microsoft Confidential

Page 17

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

You can use the Select keyword in combination with any property of the objects that you are retrieving. In this case you are retrieving objects of type SPLogEvent. The SPLogEvent includes a property named Category. Task 2 – Using Filters and Wildcards with Windows PowerShell Cmdlets In this task, you will learn how to use filters and wildcards to refine the functionality of SharePoint cmdlets, using a simple wildcard character – the asterisk ( * ). Let's say you want to retrieve a list of site collections on the managed path: http://intranet.contoso.com/site. 1. Type the following command, and then press ENTER. Get-SPSite http://intranet.contoso.com/sites/*

Note:

This expression is shorthand for: Get-SPSite –Identity http://intranet.contoso.com/sites/*

The command window displays a list of site collections that lie on the managed path you specified.

The ability to use wildcards can be particularly useful when you combine cmdlets. Let's say you want to make Alex Darrow the secondary site collection administrator for all site collections on the managed path http://intranet.contoso.com/sites/. 2. Type the following command, and then press ENTER. Get-SPSite http://intranet.contoso.com/sites/* | Set-SPSite –SecondaryOwnerAlias CONTOSO\AlexD

Microsoft Confidential

Page 18

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

Note: This cmdlet can take a few minutes to complete, depending on the number of sites that are on the server farm. The more sites, the longer the process will take.

At this point, the advantages of using PowerShell should be much more transparent. With a single command, you just changed the secondary site collection administrator on multiple sites, instead of having to parse through every single site to make the changes. Note:

You can only use the Get-SPSite cmdlet if you are already a site owner. If you are not a site owner, you can use the Get-SPSiteAdministration cmdlet to retrieve the site object.

To verify that the command worked, use another cmdlet with the Select keyword. 3. Type the following command, and then press ENTER: Get-SPSite http://intranet.contoso.com/sites/* | Select Url, SecondaryContact

The command window displays a list of site collections by URL and secondary site collection administrator. You can see that all the sites on the http://intranet.contoso.com/sites managed path now have Alex Darrow as the secondary site collection administrator.

The primary PowerShell approach to filtering is to pass your cmdlet output to the Where-Object cmdlet. You can use the question mark character "?" as shorthand for Where-Object. This cmdlet filters the

Microsoft Confidential

Page 19

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

objects that are passed along the command pipeline according to a filter string that you specify. You can use Where-Object to filter the output from any cmdlet. Note: The reason to use the Where-Object cmdlet versus the Filter parameter is simple – the Where-Object is universally available, whereas the Filter parameter is only available on a select few cmdlets. The Where-Object cmdlet also provides extra functionality such as enabling you to use match and notmatch operators with regular expressions in the filter string. These are unavailable when you use the Filter parameter. 4. Type the following command, and then press ENTER. Get-SPLogEvent –StartTime (Get-Date).AddMinutes(-90) | ?{$_.Level –eq "Critical"} | Select Category, Timestamp, Area

The command window displays a list of all log events that occurred in the last 90 minutes where the value of the Level property is CRITICAL, with thexite Area and Timestamp of the events. Note: This cmdlet returns as many events as requested that exist on the server. If the cmdlet has a large number of LogEvents listed under Critical, the cmdlet may return a lot of lines. To limit the lines shown to the screen and to advance to the next by pressing the space bar, include the '|more' command at the end of the cmdlet code example.

You can also use regular expressions to constrain the results returned by a cmdlet. The Get-SPSite cmdlet includes a RegEx parameter that causes the URL provides for the Identity parameter to be treated as a regular expression. Task 3 – Enumerating Collection Commands and Administrative Tasks with PowerShell In this task, you will learn how to enumerate collections and perform actions on each object in the collection. When it comes to repetitive tasks, the ability to loop through collections of objects is an invaluable tool in the armory of the Windows PowerShell script writer. Windows PowerShell includes the ForEachMicrosoft Confidential

Page 20

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

Object cmdlet (or foreach in shorthand) that you can use to enumerate over a result set. To use the ForEach-Object cmdlet, you must pass in a collection of objects, and you must provide the actions to perform on each object within curly brackets ( { } ). Let's say you want to roll out and provision a team blog site on every site collection on the http://intranet.contoso.com/sites/ managed path using a standard blog template. 1. To create a series of team blog sites on every site collection on the managed path, type the following command, and then press ENTER. Get-SPSite http://intranet.contoso.com/sites/* | foreach{New-SPWeb –Url ($_.Url + "/blog") –Template BLOG#0}

The command window returns a list of URLs at which new blog sites have been created.

Note: This cmdlet can take more than a few minutes to complete, depending on the number of sites that are on the server farm. The more sites on the farm, the longer the process will take. 2. Browse to one or more of the URLs to verify that the blog sites were created correctly.

Microsoft Confidential

Page 21

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

In the final exercise of this lab, you’ll use more sophisticated ways of working with the ForEach-Object cmdlet when using the object model. In this exercise, you learned how to use wildcards to help filter your cmdlets to return specific information. You also learned how to combine cmdlets in Windows PowerShell to perform multiple operations and filter the results more cleanly. Finally, you also learned how to create enumerated collection commands and do more advanced administrative tasks in Windows PowerShell, like creating a blog subsite for each of the servers in your SharePoint farm.

Microsoft Confidential

Page 22

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

Exercise 3: Perform Advanced Tasks with PowerShell Estimated time to complete this exercise: 30 minutes In the previous exercise, you saw how some of the features of Windows PowerShell enable you to create scripts that go beyond the capabilities of previous command-line tools such as stsadm. In this exercise, you will take this a step further and learn how you can use the full power of the SharePoint object model from the Windows PowerShell command prompt, using local variables with PowerShell cmdlets, learning how to set granular control over memory usage settings, and use the object model to make changes to global site settings in a server farm. Task 1 – Using Local Variables with PowerShell cmdlets In this task, you’ll assign values to local variables to help manage memory usage and tasking when doing administrative tasks in Windows PowerShell and associated scripts. The ability to create and assign values to local variables is a powerful benefit when you use Windows PowerShell. If you have worked with the SharePoint object model you will know that certain objects (such as SPSite and SPWeb) need to be managed carefully to avoid excessive memory usage. In this task, you will learn how to assign and dispose of local variables in Windows PowerShell scripts. The SharePoint snap-in for Windows PowerShell provides two key cmdlets that you can use to manage variable assignment: Start-SPAssignment and Stop-SPAssignment. 1. In the SharePoint 2013 Management Shell, type each of the following commands separately, and press ENTER. Start-SPAssignment -Global $Web = Get-SPWeb http://intranet.contoso.com/sites/Assets $Web.Description = "Testing assignments" $Web.Update() Stop-SPAssignment -Global

Microsoft Confidential

Page 23

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

In this case, we are using the global assignment model. After calling Start-SPAssignment – Global, any objects that we create are assigned to a global assignment store. This makes sure that the objects remain available while we use the object model to set properties and call methods. When we call Stop-SPAssignment –Global, any objects in the global assignment store are disposed of and the memory is released. Note:

Use global assignment carefully. For example, if you use and then call , every site collection object on your server farm is loaded into memory. This will cause serious performance issues, or simply crash the whole server farm. Use global assignments sparingly.

2. Open a browser window and browse to: http://intranet.contoso.com/sites/Assets/Pages/default.aspx 3. Verify that the site description reflects your changes and shows "Testing assignments" in the information field by clicking the information icon next to the site title.

Task 2 – Using Granular Memory Control Cmdlets When you write complex, long-running scripts, you may require more granular control over how memory is assigned and released. Rather than assigning all your variables to a global assignment store, you can use named assignments stores that enable you to assign and release different objects at different points in your script. Let's say you want to create a news site on every site collection on a particular managed path, and then set a few properties on each news site. 1. Type the following command, and then press ENTER. Microsoft Confidential

Page 24

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

$SiteScope = Start-SPAssignment

2. Type the following text, and then press ENTER. Foreach($Site in ($SiteScope | Get-SPSite http://intranet.contoso.com/sites/*))

Note: When you press ENTER, the PowerShell prompt changes to a double chevron (>>). This indicates that Windows PowerShell recognizes that your procedure is incomplete, and will accept multiline input without attempting to execute every time you press ENTER. 3. Type the following command text, and press ENTER after each line. { New-SPWeb –Url ($Site.Url + "/teamnews") $WebScope = Start-SPAssignment $Web = $WebScope | Get-SPWeb ($Site.Url + "/teamnews") $Web.Title = "Team News" $Web.Description = "All the latest news and information" $Web.Update() Stop-SPAssignment $WebScope } Stop-SPAssignment $SiteScope

4. Press ENTER again. The entire command should now execute.

Note: The command may take a few minutes to execute, depending on the number of sites you choose to deploy in the database.

Microsoft Confidential

Page 25

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

5. You should now see the URL you just created at http://intranet.contoso.com/sites/assets/teamnews. Browse to the URL and verify that the site has been created.

Microsoft Confidential

Page 26

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

In this scenario, you are creating two different assignment scopes. The scope named $SiteScope is used to store references to site collections, and is not cleared until the entire procedure is complete. The scope named $WebScope is used to store references to individual sites, and is cleared after each individual site is processed. Note:

In addition to enabling you to create and assign values to local variables, Windows PowerShell also provides access to environment variables. For example, you can use $env:os to retrieve the local operating system, or $env:computername to retrieve the local computer name.

Task 3 – Using the Object Model in PowerShell to Change Site Settings In the previous task, you saw some examples of how you can assign local variables and then use the SharePoint object model to set properties and call methods on those variables. In this task, you will learn more about how to use the SharePoint object model in your PowerShell scripts, and use the object model to change site settings across multiple sites. When you start to use the object model, it is essential to be able to find out what methods and properties a particular object supports. Luckily, Windows PowerShell includes a cmdlet named GetMember that can help. You can pass any object into the Get-Member cmdlet to return a list of the available properties and methods on that object. 1. In the SharePoint 2013 Management Shell, type the following command, and then press ENTER. Get-SPSite | Get-Member

The command window displays a long list of the methods and properties that you can access on a SPSite object. You can experiment with the object model by writing property values to the screen. Remember to create a memory Startassignment before you create a local variable.

Microsoft Confidential

Page 27

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

2. Type the following command, and then press ENTER. Start-SPAssignment –Global

3. Type the following command, and then press ENTER. $Site = Get-SPSite http://intranet.contoso.com/sites/assets

4. Type the following command, and then press ENTER. Write-Host $Site.Url

The console window displays the URL of the site collection.

5. Type the following command, and then press ENTER. Write-Host $Site.Zone

The console window indicates that the site collection is in the Default zone. 6. Type the following command, and then press ENTER. Microsoft Confidential

Page 28

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

Write-Host $Site.Quota

Because the site quota is a complex type that is not well represented by a string, the console window displays the type name of the SPQuota object. As before, you can use the Get-Member cmdlet to find out more about the SPQuota object. 7. Type the following command, and then press ENTER. $Site.Quota | Get-Member

8. Type the following command, and then press ENTER. $Site.Quota | Select StorageMaximumLevel

The command window shows a maximum site storage of zero ( 0 ) bytes – which, by default, means the site does not have a usage quota defined. This means users are able to infinitely add to the site storage, which can rapidly overload your server. Microsoft Confidential

Page 29

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

To correct the oversight, you want to add a maximum user quota of 100MB and add a warning that occurs when a user’s quota exceeds 85MB. In order to do this, you need to set the $Site.Quota.StorageMaximumLevel in bytes for each user, as well as the $Site.Quota.StorageWarningLevel. 9. Type the following command, and then press ENTER. $Site.Quota.StorageMaximumLevel = 104857600

Note:

104857600 bytes is equal to 100MB.

10. Type the following command, and then press ENTER. $Site.Quota.StorageWarningLevel = 89128960

Note:

89128960 bytes is equal to 85MB.

11. To confirm the Maximum Storage levels for the site are set to 100MB and the Warning Level is set to 80MB , type the following commands, and press ENTER. $Site.Quota | Select StorageMaximumLevel $Site.Quota | Select StorageWarningLevel

12. To verify in SharePoint 2013 Central Administration, click the Application Management link, then click Specify quota templates. Verify that the site quotas have been changed to 100 and 80MB.

Microsoft Confidential

Page 30

Hands-on Lab

Introduction to Windows PowerShell with Sharepoint Server 2013

When you have finished using the $Site variable, remember to clear the memory assignment. 13. To clear the memory assignment, type the following command, and then press ENTER: Stop-SPAssignment –Global

In Exercise 3, you learned how to do more advanced tasks with Windows PowerShell. You learned how to use local variables with PowerShell cmdlets, explored the more granular memory techniques to make sure the server returns perform as expected, and used the Object Model in PowerShell to change the site settings for your server farm.

Summary Congratulations! In this lab, you were introduced to the Windows PowerShell to script administrative tasks in SharePoint Server 2013. In particular, the lab explored: 

How to get started with the SharePoint 2013 Management Shell.

Microsoft Confidential

Page 31

Hands-on Lab

 

Introduction to Windows PowerShell with Sharepoint Server 2013

How to use Windows PowerShell scripting techniques, such as pipes, filters, wildcards, and enumerations, for SharePoint Server 2013 administration. How to assign variables and use the SharePoint object model from Windows PowerShell.

For more information on SharePoint Server 2013, visit the SharePoint Products and Technologies team blog at http://blogs.msdn.com/SharePoint for regular announcements and updates.

Microsoft Confidential

Page 32

Suggest Documents