220 likes | 393 Views
PowerShell for SharePoint Configuration and Administration. William Xi Microsoft Consulting Services. Agenda. PowerShell Introduction Basic Syntax Advanced SharePoint SharePoint Object Model Combine with STSAdmin, PSConfig Large System Deployment with Scripts Question.
E N D
PowerShell for SharePoint Configuration and Administration William Xi Microsoft Consulting Services
Agenda • PowerShell • Introduction • Basic Syntax • Advanced • SharePoint • SharePoint Object Model • Combine with STSAdmin, PSConfig • Large System Deployment with Scripts • Question
Windows PowerShell New command-line shell and scripting language designed for system administration and automation Based on .NET Framework and native support for XML 100+ commands to perform system admin tasks Full access to .NET Framework and object models Sophisticated expression parsing and .NET object manipulation at the command line, including pipelining of objects Extensible interface via custom code in C#/VB or functions Standard utilities for managing Windows data in different stores and formats: Active Directory (ADSI), WMI, COM, ADO, HTML and XML
PowerShell Core Components • Cmdlet • Single-function command-line tool • 100+ built-in, and extendable by users • Function • Block of scripts with specific function for repeated usage • Support variables and objects as parameters • Alias • Shortname for commands • Set-Alias -Name stsadm -Value $env:CommonProgramFiles”\Microsoft Shared\Web Server Extensions\12\BIN\STSADM.EXE” • Access to .Net Objects • Support standard system/application or your own custom assembly • [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") • [System.Reflection.Assembly]::LoadFrom("e:\wxi\powershell\Evt_Library.DLL")
SharePoint Object Models • Microsoft.SharePoint.Administration • Provides administrative types and members for managing a Windows SharePoint Services deployment • $spfarm = [Microsoft.SharePoint.Administration.SPFarm]::Local • Microsoft.SharePoint.Administration.SPWebApplication • Microsoft.SharePoint • Provides types and members for working with a top-level site and its subsites or lists • SPSite, SPList, SPContext, SPFolder • SPList oListCur = SPContext.Current.List; • SPWeb oWeb = SPContext.Current.Web; • SPSite oSite = SPContext.Current.Site;
Create Web Application function new-SPWebApplication() { # Get the local SPFarm object $spfarm = [Microsoft.SharePoint.Administration.SPFarm]::Local # Use SPWebApplicationBuilder to create web app with defaults $wabuilder = new-object Microsoft.SharePoint.Administration.SPWebApplicationBuilder $spfarm $webapp = $wabuilder.Create() $webapp.DefaultTimeZone = [Microsoft.SharePoint.SPRegionalSettings]::TimeZone.ID $webapp.Update() # Queue the web application for creation $webapp.Provision() $webapp } function new-SPSite([Microsoft.SharePoint.Administration.SPWebApplication] $webapp) { $spsite = $webapp.Sites.Add($Path, $WebSiteName, $WebSiteName, 1033, "STS", “domain\wixi”, “MOSSAdmin”, “williamx@microsoft.com”) $spsite } $newapp = new-SPWebApplication set-apidentity $newapp $args[0] $args[1] $spsite = new-SPSite $newapp $spsite | select URL
Get Server Configuration # Get local SharePoint server configuration functionGet-LocalFarm { return [Microsoft.SharePoint.Administration.SPFarm]::Local } functionGet-RemoteFarm { return$spfarm= [Microsoft.SharePoint.Administration.SPFarm]::Open("Data Source=SQLServer\SQLInstance01;Initial Catalog=MOSS_Config_DB;Integrated Security=SSPI") } filterGet-WebService { $webServices=new-objectMicrosoft.SharePoint.Administration.SPWebServiceCollection($_) $webServices } filterGet-WebApplication { $_.WebApplications } filterGet-SiteCollection { $_.Sites } filterGet-Web { $_.AllWebs } Get-localfarm |get-webservice | get-webapplication | get-sitecollection | get-web
Work With Document Library - List Documents $spsite = new-object Microsoft.SharePoint.SPSite($args[0]) $spweb = spsite.OpenWeb() $spfolder = spweb.GetFolder($args[0]) $spfolder.Files | select Name,Title,TimeCreated, CheckOutStatus, ServerRelativeUrl c:\wxi\ListDocuments.ps1 http://moss.litwareinc.com/Documents
Work With Document Library • Upload Document • function GetDestinationName([string] $filename, [string] $path) • { • $path + “/Shared%20Documents/” + $(split-path –leaf $filename) • } • $client = new-object System.Net.WebClient • $client.Credentials = [System.Net.CredentialCache]::DefaultCredentials • dir $args[0] | % { • $uploadfile = GetDesinationName $_ $args[1]; • $wc.UploadFile($uploadfile, “PUT”, $_.FullName) • .\ UploadDocuments.ps1 *.doc http://moss.litwareinc.com/Documents
Create Site Structures function new-SPWeb([Microsoft.SharePoint.SPSite] $spsite, [string] $url, [string] $title, [string] $description, [string] $template) { $spweb = $spsite.AllWebs.Add($url, $title, $description, [int]1033, $template, $false, $false) $spweb } function AddWebs([string] $csvfile, [string] $url) { $spsite = new-object Microsoft.SharePoint.SPSite($url) Import-Csv $csvfile | foreach-object { $spweb = new-SPWeb $spsite $_.RelativeUrl $_.Title $_.Description $_.Template } } function ShowWebs([string] $url) { $spsite = new-object Microsoft.SharePoint.SPSite($url) $spsite.AllWebs | select ServerRelativeUrl, Title, WebTemplate, Description | sort ServerRelativeUrl } AddWebs $args[0] $args[1] ShowWebs $args[1]
Work with Site Info function FormatSiteData([Microsoft.SharePoint.Administration.SPSiteCollection] $sites) { $sites | select Url, ContentDatabase, LastContentModifiedData | sort LastContentModifiedDate, Url | out-default | format-table –auto } # Get the local SPFarm object $spfarm = [Microsoft.SharePoint.Administration.SPFarm]::Local # Get the Web Application Service $spservice = $spfarm.Services | WHERE {$_.TypeName –eq “Windows SharePoint Services Web Application”} # Loop through all of the Web Applications in the SPFarm $spservice.WebApplications | foreach-object { # Test to see if the web application contains sites if ($_.Sites.Count –ne 0) { FormatSiteData $_.Sites # Export the site data to csv $filename = $_.Name + “.csv” $_.Sites | export-csv $filename } }
PowerShell for SharePoint Advantage SharePoint has a rich .NET Object Model PowerShell interacts natively with .NET SharePoint’s internal values are hidden Lots of functionality not accessible via STSAdmin Mixed use of STSAdm, .Net code, WMI Easy to reference and call your own custom dll
Make It Real – Large Deployment • Farm Configuration Data • All configuration data are stroed in XML via InfoPath from • Server Name • Service Account • Database Name • Web Application, Application Pool ID • Parent/Child Farm, SSP
SharePoint Configuration in XML <my:Farm> <my:FarmName>Maintenance Farm</my:FarmName> <my:FarmID></my:FarmID> <my:Facility>CHVPK</my:Facility> <my:FarmType>Collaboration Host Child-Farm</my:FarmType> <my:FarmSvcAccts> <my:FarmSvcAcct> <my:FarmServiceAcctName>s-SP-G05D-T00A</my:FarmServiceAcctName> <my:FarmServiceAcctType>Farm Administrator</my:FarmServiceAcctType> </my:FarmSvcAcct> </my:FarmSvcAccts> <my:FarmDBs> <my:FarmDB> <my:FarmDBName>G05D_BOC_Config</my:FarmDBName> <my:FarmDBConnStr>BOCX64SSMC1\sql02</my:FarmDBConnStr> </my:FarmDB> </my:FarmDBs> <my:FarmWebApps> <my:FarmWebApp> <my:FarmWebAppName>TeamSite001</my:FarmWebAppName> <my:FarmWebAppURL>http://bocnt3ah3v002:8090</my:FarmWebAppURL> <my:FarmWebAppPhysicalDir>E:\Inetpub\moss\web\TeamSite001</my:FarmWebAppPhysicalDir> <my:FarmWebAppIPAddress>196.168.1.1</my:FarmWebAppIPAddress> <my:FarmWebAppPoolAcct>MOSSAppPool</my:FarmWebAppPoolAcct> </my:FarmWebApp> </my:FarmWebApps> <my:FarmServers> <my:FarmServer> <my:FarmServerName>bocnt3ah3v</my:FarmServerName> <my:FarmServerType>Web Front-End</my:FarmServerType> <my:FarmServerDomain></my:FarmServerDomain> </my:FarmServer> </my:FarmServers> </my:Farm>
References • Resource Center for Scripting with Windows PowerShell http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx • Beginner’s Guide http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/default.mspx • Windows PowerShell Team Blog http://blogs.msdn.com/powershell/default.aspx • SharePoint Object Model and Namespace http://msdn2.microsoft.com/en-us/library/ms473633.aspx http://msdn2.microsoft.com/en-us/library/ms453225.aspx • TechNet Virtual Labs: Introduction to Windows PowerShell http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?culture=en-US&EventID=1032314395&EventCategory=3 • TechNet Virtual Lab: PowerShell for SharePoint Administration and Customization http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?EventID=1032346304&EventCategory=3&culture=en-US&CountryCode=US • Helpful PowerShell for SharePoint Websites http://blogs.flexnetconsult.co.uk/colinbyrne/SearchView.aspx?q=powershell http://sharepoint.microsoft.com/blogs/zach/Lists/Categories/Category.aspx?Name=PowerShell http://darrinbishop.com/blog/archive/2007/11/28/psfivefunctions.aspx