Managing SharePoint 2010 with Windows PowerShell : Managing SharePoint 2010 Web Applications
PS > Get-Command -Noun SPWebApplication CommandType Name Definition ----------- ---- ---------- Cmdlet Get-SPWebApplication Get-SPWebApplication [[-Identity] < Cmdlet New-SPWebApplication New-SPWebApplication -Name <String> Cmdlet Remove-SPWebApplication Remove-SPWebApplication [-Identity] Cmdlet Set-SPWebApplication Set-SPWebApplication [-Identity] <S
We’ll look at each of these cmdlets, starting with Get-SPWebApplication.
Getting Web Applications in SharePoint 2010
The Get-SPWebApplication cmdlet returns the web applications in SharePoint 2010. You can use the Identity parameter to return a specific web application. If you don’t specify a web application, all web applications are returned:
PS > Get-SPWebApplication DisplayName Url ----------- --- SharePoint - 80 http://spserver01/
By default, Central Administration is excluded from the set of returned web applications. Using the IncludeCentralAdministration switch parameter forces the Central Administration web application to be included:
PS > Get-SPWebApplication -IncludeCentralAdministration DisplayName Url ----------- --- SharePoint - 80 http://spserver01/ SharePoint Central Administ... http://spserver01:23815/
Modifying Web Applications in SharePoint 2010
To configure web applications in SharePoint 2010, use the Set-SPWebApplication cmdlet. This cmdlet supports a variety of parameters corresponding to different configuration settings of web applications, such as e-mail addresses, SMTP server, and time zone.
First, let’s look at how to configure the From and Reply-to e-mail addresses. When configuring the e-mail addresses used by the web application, you must specify an SMTP server to use.
PS > Get-SPWebApplication -Identity http://SPServer01 | >> Set-SPWebApplication -OutgoingEmailAddress from@nima.com ` >> -ReplyToEmailAddress reply@nima.com -SMTPServer ExcServer01
In this example, we use Get-SPWebApplication to retrieve a specific web application, and then pipe the object to the Set-SPWebApplication cmdlet, where we use the SMTPServer parameter to specify which SMTP server to use. We also specify the From and Reply-to e-mail addresses that should be used by the web application. Figure 1 shows what the configuration looks like in Central Administration.
Figure 1. Setting the From e-mail address, Reply-to e-mail address, and SMTP mail server through PowerShell
It is also possible to allow anonymous access with the Set-WebApplication cmdlet. Allowing anonymous access at the web application level enables individual site collection administrators to turn on anonymous access.
PS > Get-SPWebApplication -Identity http://SPServer01 | >> Set-SPWebApplication -AllowAnonymousAccess -Zone Default
To disable anonymous access, use the same switch parameter followed by :$False:
PS > Get-SPWebApplication -Identity http://SPServer01 | >> Set-SPWebApplication -AllowAnonymousAccess:$False -Zone Default
Caution
If anonymous access is turned off when using Forms-based authentication, Formsaware client applications may fail to authenticate correctly.
Use the Set-SPWebApplication cmdlet’s DefaultTimeZone parameter to set the time zone that should be used for new site collections:
PS > Get-SPWebApplication -Identity http://SPServer01 | >> Set-SPWebApplication -DefaultTimeZone 4
In this example, we use the DefaultTimeZone parameter to set the time zone used by new site collections to 4, which corresponds to Western Europe Standard Time. You can list the other values for time zones using the SPRegionalSettings class as demonstrated below.
PS > [Microsoft.SharePoint.SPregionalSettings]::Globaltimezones
Creating a New Web Application in SharePoint 2010
You can create new web applications in SharePoint 2010 with the NewSPWebApplication cmdlet. This cmdlet has several parameters for configuring the new web application, including the following:
The Name parameter specifies the name of the new web application.
The Port parameter sets the port from which the web application can be accessed.
The ApplicationPool parameter specifies the application pool to use. If the application pool does not exist, a new application pool will be created.
When creating new application pools with the New-SPWebApplication cmdlet, an application pool account must be specified. You can specify the account through the ApplicationPoolAccount parameter.
Here is a basic example showing how to create a web application through Windows PowerShell:
PS > New-SPWebApplication -Name "My WebApplication" -Port 5077 ` >> -ApplicationPool "MyAppPool" ` >> -ApplicationPoolAccount (Get-SPManagedAccount powershell\managedaccount) DisplayName Url ----------- --- My WebApplication http://spserver01:5077/
In this example, we use the Get-SPManagedAccount cmdlet to retrieve an account that is registered in the configuration database.
When the command is run, a content database is also created. Since we didn’t specify a name for the content database in this example, a name will be autogenerated in the format WSS_Content_<GUID>. Alternatively, you can specify a custom name through the DatabaseName parameter.
Removing a Web Application in SharePoint 2010
The Remove-SPWebApplication cmdlet removes an existing web application. You can use the Zone parameter to remove a specific zone. If the zone is not specified, all zones are removed. The cmdlet also supports switch parameters that allow you to delete the IIS web site associated with the target zone (or all zones) and the content database associated with the web application. Here’s an example:
PS > Remove-SPWebApplication -Identity http://SPServer01:5077 ' >> -DeleteIISSite -RemoveContentDatabases Confirm Are you sure you want to perform this action? Performing operation "Remove-SPWebApplication" on Target "My WebApplication". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y
In this example, we remove the web application that we previously created. We also add the DeleteIISSite and RemoveContentDatabase switch parameters to remove the IIS web site and content database associated with the web application. As with the other removal cmdlets, Windows PowerShell prompts for a confirmation before the command is executed.