SharePoint Automation Gary Lapointe – Founding Partner, Aptillon, Inc.

15May/122

International SharePoint Conference 2012 Follow-up

First off I apologize for the delay in getting this post out – I’ve been fraught with injuries and illness since my return from London and just haven’t had the time or mental capacity to think about writing anything.

During my sessions at the ISC I demonstrated (along with Spence Harbar and Chan Kulathilake) how we could use PowerShell to provision the entire SharePoint 2010 Farm used throughout the IT track at the conference (yeah, no pressure – everyone presenting after me was depending on my scripts functioning). This Farm consisted of numerous servers beyond the SharePoint servers but my scripts were only responsible for getting SharePoint configured. For reference here’s a screenshot showing the server topology:

image

Before we could run the scripts on any servers there was some prep work that we had to do ahead of time. Specifically we did the following on each of the SharePoint servers (SP01-06):

  • Installed SharePoint 2010 and Office Web Applications with SP1 and the October 2011 CU
    • The installation of the bits could have been scripted but doing so during a live session at a conference just wasn’t practical due to time limitations.
  • Disable: UAC, Firewall, IE ESC
    • This isn’t something you’d typically do for a production environment but doing so reduced potential complications and annoyances when it came to doing demos. I typically will disable all these things when doing my provisioning and then selectively re-enable them after I’ve confirmed that the Farm is running properly – this makes troubleshooting much easier.
  • PowerShell Prep
    • I updated the all users all hosts profile on each server so that the SharePoint PowerShell snap-in would be loaded for any editor so that we wouldn’t be tied to the management shell. (We also installed the PowerShell ISE).
    • I enabled remoting on all servers so that I could build each server remotely, thereby foregoing the need to have to log into each server to kick off the script in parallel. I also created a custom configuration session which loaded the SharePoint PowerShell snap-in automatically and made sure the threading model was set appropriately. The following shows the commands I executed to enable remoting:
      Enable-PSRemoting
      Enable-WSmanCredSSP -Role Server
      Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1024
      Register-PSSessionConfiguration -Name "SharePoint" -StartupScript "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\sharepoint.ps1" -Force -ThreadOptions ReuseThread
      Set-PSSessionConfiguration -Name "SharePoint" -ShowSecurityDescriptorUI -Force
      
    • Because the we’re using scripts and because those scripts are stored on a network share we had to set the PowerShell execution policy on each server to Bypass.
  • SPInstall Permissions
    • The scripts were all run using a single named account which we called SPInstall. In order to perform all the required tasks this account was made a local administrator on all the SharePoint servers (none of the others) and the account was granted the dbcreator and securityadmin roles on SQL01 and SQL02 (or primary and failover SQL instances).
  • Add SQL Alias and DNS Entries
    • The DNS servers were updated with the appropriate entries for all of our Web Applications (we did not use hosts files) and, to connect to SQL Server, we used a SQL alias which needed to be added on each of the SharePoint servers.

Controller Scripts

So that constitutes the extent of the prep work that we did before we could kick off the scripts that I put together. To do the provisioning I used a main “controller” script which I called ConfigureServer.ps1. This script was responsible for loading all the subsequent scripts which created the initial Farm (or connected to it), provisioned the Web Applications and Site Collections, and finally, created all the relevant Service Applications:

param(
  [switch]$Connect
)

Write-Host "Script Start Time: $(Get-Date)" -ForegroundColor Green

#Main Farm
. .\FarmCreation\Build-SPFarm.ps1
$farmConfigFile = Resolve-Path .\FarmCreation\FarmConfigurations.xml
try {
    if ($Connect) {
        Join-SPFarm $farmConfigFile
    } else {
        New-SPFarm $farmConfigFile
    }
} catch {
    Write-Warning "Unable to build or join Farm!"
    $_
    return
}

#Web Applications
if (!$Connect) {
    #Only need to do this once so do it when we create the farm initially and not for each connection.
    try {
        . .\WebApplications\Provision-WebApplications.ps1
        Provision-WebApplications (Resolve-Path .\WebApplications\WebApplications.xml)
    } catch {
        Write-Warning "Unable to create web applications!"
        $_
        return
    }
}

#Services
try {
    #State Service
    . .\ServiceApplications\StateServices\Start-StateServices.ps1
    Start-StateServices (Resolve-Path .\ServiceApplications\StateServices\StateServices.xml)

    #Usage Service
    . .\ServiceApplications\UsageService\Start-UsageService.ps1
    Start-UsageService (Resolve-Path .\ServiceApplications\UsageService\UsageService.xml)

    #Secure Store Services
    . .\ServiceApplications\SecureStoreServices\Start-SecureStoreServices.ps1
    Start-SecureStoreServices (Resolve-Path .\ServiceApplications\SecureStoreServices\SecureStoreServices.xml)

    #Web Analytics Services
    . .\ServiceApplications\WebAnalyticsServices\Start-WebAnalyticsServices.ps1
    Start-WebAnalyticsServices (Resolve-Path .\ServiceApplications\WebAnalyticsServices\WebAnalyticsServices.xml)

    #User Code Services
    . .\ServiceApplications\UserCodeServices\Start-UserCodeServices.ps1
    Start-UserCodeServices (Resolve-Path .\ServiceApplications\UserCodeServices\UserCodeServices.xml)

    #BCS Services
    . .\ServiceApplications\BCSServices\Start-BCSServices.ps1
    Start-BCSServices (Resolve-Path .\ServiceApplications\BCSServices\BCSServices.xml)

    #Excel Services
    . .\ServiceApplications\ExcelServices\Start-ExcelServices.ps1
    Start-ExcelServices (Resolve-Path .\ServiceApplications\ExcelServices\ExcelServices.xml)

    #Word Automation Services
    . .\ServiceApplications\WordAutomationServices\Start-WordAutomationServices.ps1
    Start-WordAutomationServices (Resolve-Path .\ServiceApplications\WordAutomationServices\WordAutomationServices.xml)

    #Metadata Services
    . .\ServiceApplications\MetadataServices\Start-MetadataServices.ps1
    Start-MetadataServices (Resolve-Path .\ServiceApplications\MetadataServices\MetadataServices.xml)

    #C2WTS
    . .\ServiceApplications\ClaimsToWindowsTokenServices\Start-ClaimsToWindowsTokenServices.ps1
    Start-ClaimsToWindowsTokenServices (Resolve-Path .\ServiceApplications\ClaimsToWindowsTokenServices\ClaimsToWindowsTokenServices.xml)

    #PowerPoint Services
    . .\ServiceApplications\PowerPointServices\Start-PowerPointServices.ps1
    Start-PowerPointServices (Resolve-Path .\ServiceApplications\PowerPointServices\PowerPointServices.xml)

    #Word Viewing Services
    . .\ServiceApplications\WordViewingServices\Start-WordViewingServices.ps1
    Start-WordViewingServices (Resolve-Path .\ServiceApplications\WordViewingServices\WordViewingServices.xml)
    
    #User Profile Services
    . .\ServiceApplications\UserProfileServices\Start-UserProfileServices.ps1
    Start-UserProfileServices (Resolve-Path .\ServiceApplications\UserProfileServices\UserProfileServices.xml)

    #Enterprise Search Services
    . .\ServiceApplications\EnterpriseSearchServices\Start-EnterpriseSearchServices.ps1
    Start-EnterpriseSearchServices (Resolve-Path .\ServiceApplications\EnterpriseSearchServices\EnterpriseSearchServices.xml)

} catch {
    Write-Warning "Service Application creation failed!"
    $_
    return
}
finally {
    Write-Host "Script End Time: $(Get-Date)" -ForegroundColor Green
}

 

This script was executed on each server, however, we ran it first on SP03 which is where the User Profile Synchronization Service was to be run. Due to issues with getting this guy to work using remoting we chose to RDP directly into the server and run this one script while logged into the server. Once this server was configured and the initial Farm created then we’d go ahead and run the script on the remaining servers by using PowerShell remoting to execute the script from a single location.

This is the script, which I named simply go.ps1, that I used to call the ConfigureServer.ps1 script for all the remaining SharePoint servers (everything other than SP03):

$servers = @("SP01","SP02","SP04","SP05","SP06")
$cred = Get-Credential "isclondon\spinstall"

foreach ($server in $servers) {
    Write-Host "---------------------------------------------------------------" -ForegroundColor Green
    Write-Host "$(Get-Date): Connecting to server $server." -ForegroundColor Green
    Write-Host "---------------------------------------------------------------" -ForegroundColor Green
    $session = New-PSSession -ComputerName $server -Authentication CredSSP -Credential $cred -ConfigurationName "SharePoint"
    Invoke-Command -Session $session -ScriptBlock {Set-ExecutionPolicy Bypass -Scope Process}
    Invoke-Command -Session $session -ScriptBlock {cd \\sp03\c$\Scripts}
    Invoke-Command -Session $session -ScriptBlock {. .\ConfigureServer.ps1 -Connect}
    Remove-PSSession $session
    Write-Host "$(Get-Date): Finished for server $server" -ForegroundColor Green
}

 

Build-SPFarm.ps1

So the preceding two scripts constitute the “controller” scripts that I used to call out to the core scripts which did all the real work. Now let’s look at the first and most important script as it is responsible for creating the Farm (or connecting to the Farm): Build-SPFarm.ps1.

This script, like all the remaining scripts is driven via an XML file; in other words, I store all the configuration information in a series of XML files that I pass into the function contained within the script. By taking this approach I can write a single script which is not tied to any particular environment and only change the contents of the XML file.

I think something that a lot of people forget when writing PowerShell scripts is that you are writing code, and just as you would never deploy code written by any old developer to your production environment without it first going through proper testing in a test environment, so should you never deploy (or execute in this case) scripts to production without first testing those scripts. And, with any code, if you make changes to that code you should follow proper testing practices and do a full regression test of any code that was modified (yes, this includes changing variable values because the value you specify could actually break the script if improperly formatted – think of someone forgetting a closing quote or adding a special character to the contents of a string variable). For these reasons, and many others, I encourage people to reduce the changes made to scripts by pulling the configuration information out into an XML file.

So lets look at the FarmConfigurations.xml XML file that I used for the Build-SPFarm.ps1 script:

<Farm ConfigDB="ISC_Config"
      ConfigDBFailoverDatabaseServer=""
      AdminContentDB="ISC_Content_CentralAdmin" 
      AdminContentDBFailoverDatabaseServer=""
      DatabaseServer="ISCSharePoint1" 
      Passphrase="p@ssw0rd"
      OutgoingEmailServer="192.168.1.101"
      OutgoingEmailFromAddr="administrator@isclondon.com"
      OutgoingEmailReplyToAddr="administrator@isclondon.com">
    <FarmAccount AccountName="isclondon\spfarm" AccountPassword="password" />
    <CentralAdmin Port="2010" AuthProvider="NTLM">
        <Servers>
            <Server Name="SP01" />
            <Server Name="SP02" />
            <Server Name="SP03" />
        </Servers>
    </CentralAdmin>
    <Services>
        <WebApplicationService>
            <Servers>
                <Server Name="SP01" />
                <Server Name="SP02" />
            </Servers>
        </WebApplicationService>
        <WorkflowTimerService>
            <Servers>
                <Server Name="SP01" />
                <Server Name="SP02" />
            </Servers>
        </WorkflowTimerService>
        <IncomingEmailService>
            <Servers>
                <Server Name="SP01" />
                <Server Name="SP02" />
            </Servers>
        </IncomingEmailService>
        <TraceService>
            <Account AccountName="isclondon\sptrace" AccountPassword="password" />
        </TraceService>
    </Services>
 </Farm>

 

Note that in this XML we’ve embedded the passwords used – I don’t actually recommend doing this, but if you do you should make sure the files are properly secured and, ideally, remove the password after provisioning is complete (we embedded the passwords so that we wouldn’t be continually prompted for credentials during the build as this would make it very difficult to talk through our presentation).

Before I show the contents of the Build-SPFarm.ps1 script I just wanted to point out one more design consideration which you may have clued in on by looking at the ConfigureServer.ps1 script. With rare exceptions (the ConfigureServer.ps1 script being one of them), I always make my scripts so that running the script will actually do nothing other than load a function in memory which must then be called in order to perform any action. This is to prevent someone from accidentally causing a script to execute (for example, you intend to edit the file so you double-click it and rather than open in an editor it opens in the console and is executed). The only reason the ConfigureServer.ps1 script does not work this way is because I wanted to show that you can pass parameters into a script file.

With that out of the way, let’s look at the Build-SPFarm.ps1 script:

function Join-SPFarm ([string]$settings = $(throw "-settings is required")) {
    Build-SPFarm $true $settings
}

function New-SPFarm ([string]$settings = $(throw "-settings is required")) {
    Build-SPFarm $false $settings
}

function Build-SPFarm ([bool]$connectToExisting = $false, [string]$settings = $(throw "-settings is required")) {   
    [xml]$config = Get-Content $settings

    if ([string]::IsNullOrEmpty($config.Farm.FarmAccount.AccountPassword)) {
        $farmAcct = Get-Credential $config.Farm.FarmAccount.AccountName
    } else {
        $farmAcct = New-Object System.Management.Automation.PSCredential $config.Farm.FarmAccount.AccountName, (ConvertTo-SecureString $config.Farm.FarmAccount.AccountPassword -AsPlainText -force)
    }

    $configDb = $config.Farm.ConfigDB
    $contentDB = $config.Farm.AdminContentDb
    $server = $config.Farm.DatabaseServer
    if ($config.Farm.Passphrase.Length -gt 0) {
        $passphrase = (ConvertTo-SecureString $config.Farm.Passphrase -AsPlainText -force)
    } else {
        Write-Warning "Using the Farm Admin's password for a passphrase"
        $passphrase = $farmAcct.Password
    }
    
    #Only build the farm if we don't currently have a farm created
    if ([Microsoft.SharePoint.Administration.SPFarm]::Local -eq $null) {
        psconfig -cmd upgrade -inplace b2b
        
        if ($connectToExisting) {
            #Connecting to farm
            Write-Host "Connecting to Farm..."
            Connect-SPConfigurationDatabase -DatabaseName $configDb -DatabaseServer $server -Passphrase $passphrase
        } else {
            #Creating new farm
            Write-Host "Creating Farm..."
            New-SPConfigurationDatabase `
                -DatabaseName $configDb `
                -DatabaseServer $server `
                -AdministrationContentDatabaseName $contentDB `
                -Passphrase $passphrase `
                -FarmCredentials $farmAcct
            
            if (![string]::IsNullOrEmpty($config.Farm.ConfigDBFailoverDatabaseServer)) {
                Set-FailoverDatabase $configDb $config.Farm.ConfigDBFailoverDatabaseServer
            }
            if (![string]::IsNullOrEmpty($config.Farm.AdminContentDBFailoverDatabaseServer)) {
                Set-FailoverDatabase $contentDB $config.Farm.AdminContentDBFailoverDatabaseServer
            }
        }
        #Verifying farm creation
        $spfarm = Get-SPFarm -ErrorAction SilentlyContinue
        if ($spfarm -eq $null) {
            throw "Unable to verify farm creation."
        }

        #ACLing SharePoint Resources
        Write-Host "Calling Initialize-SPResourceSecurity..."
        Initialize-SPResourceSecurity

        #Installing Services
        Write-Host "Calling Install-SPService..."
        Install-SPService

        #Installing Features
        Write-Host "Calling Install-SPFeature..."
        Install-SPFeature -AllExistingFeatures
        
        Remove-PsSnapin Microsoft.SharePoint.PowerShell
        Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    } else {
        Write-Warning "Farm exists. Skipping creation."
    }
    
    if ((Get-Service sptimerv4).Status -ne "Running") {
        Write-Host "Starting SPTimerV4 Service"
        Start-Service sptimerv4
    }

    $scaConfig = $config.Farm.CentralAdmin
    $installSCA = (($scaConfig.Servers.Server | where {$_.Name -eq $env:computername}) -ne $null)
    if ($installSCA) {
        $auth = $scaConfig.AuthProvider
        $port = $scaConfig.Port
        $url = "http://$($env:computername):$port"
        $sca = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($url)
        if ($installSCA -and $sca -eq $null) {
            #Provisioning Central Administration
            Write-Host "Creating Central Admin Site at $url..."
            New-SPCentralAdministration -Port $port -WindowsAuthProvider $auth

            #Installing Help
            Write-Host "Calling Install-SPHelpCollection..."
            Install-SPHelpCollection -All

            #Installing Application Content
            Write-Host "Calling Install-SPApplicationContent..."
            Install-SPApplicationContent
        }
    }
    
    if (!$connectToExisting) {
        $server = $config.Farm.OutgoingEmailServer
        $from = $config.Farm.OutgoingEmailFromAddr
        $replyTo = $config.Farm.OutgoingEmailReplyToAddr
        $charSet = 65001
        $wa = [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]::Local
        if ($wa -ne $null) {
            $wa.UpdateMailSettings($server, $from, $replyTo, $charSet)
        }
    }
    
    #Stop or Start key service instances
    $startWebAppSvc = (($config.Farm.Services.WebApplicationService.Servers.Server | where {$_.Name -eq $env:computername}) -ne $null)
    Set-ServiceInstanceState "Microsoft SharePoint Foundation Web Application" $startWebAppSvc

    $startWorkflowSvc = (($config.Farm.Services.WorkflowTimerService.Servers.Server | where {$_.Name -eq $env:computername}) -ne $null)
    Set-ServiceInstanceState "Microsoft SharePoint Foundation Workflow Timer Service" $startWorkflowSvc

    $startIncomingEmailSvc = (($config.Farm.Services.IncomingEmailService.Servers.Server | where {$_.Name -eq $env:computername}) -ne $null)
    Set-ServiceInstanceState "Microsoft SharePoint Foundation Incoming E-Mail" $startIncomingEmailSvc
    
    
    #Set SPTraceV4 to run as domain account
    $accountNode = $config.Farm.Services.TraceService.Account
    if ($accountNode -ne $null -and ![string]::IsNullOrEmpty($accountNode.AccountName)) {
        if ([string]::IsNullOrEmpty($accountNode.AccountPassword)) {
            $svcAccount = Get-Credential $accountNode.AccountName
        } else {
            $svcAccount = New-Object System.Management.Automation.PSCredential $accountNode.AccountName, (ConvertTo-SecureString $accountNode.AccountPassword -AsPlainText -force)
        }
        $user = Get-SPManagedAccount -Identity $svcAccount.Username -ErrorAction SilentlyContinue
        if ($user -eq $null) {
            $user = New-SPManagedAccount -Credential $svcAccount
        }
        $tracingSvc = (Get-SPFarm).Services | ? {$_.Name -eq "SPTraceV4"}
        if ($tracingSvc.ProcessIdentity.Username -ne $user.Username) {
            $tracingSvc.ProcessIdentity.ManagedAccount = $user
            $tracingSvc.ProcessIdentity.CurrentIdentityType = "SpecificUser"
            $tracingSvc.ProcessIdentity.Update()
            $tracingSvc.ProcessIdentity.Deploy()
        }
        Write-Host "Adding `"$($user.Username)`" to Performance Log Users group..."
        $p = Start-Process -PassThru -FilePath "c:\windows\system32\net.exe" -ArgumentList "localgroup `"Performance Log Users`" `"$($user.Username)`" /add" -Wait -NoNewWindow
        if ($p.ExitCode -ne 0) { 
            Write-Warning "Unable to add `"$($user.Username)`" to Performance Log Users group!" 
        } else { 
            Restart-Service SPTraceV4
            iisreset
        }
    }

} 
function Set-ServiceInstanceState($typeName, $enable) {
    $svc = Get-SPServiceInstance -Server $env:computername | where {$_.TypeName -eq $typeName}
    if ($svc -eq $null) {
        throw "Unable to retrieve $typeName Service Instance."
    }
    if ($svc.Status -ne "Online" -and $enable) {
        Write-Host "Starting $typeName service instance..."
        $svc | Start-SPServiceInstance
    }
    if ($svc.Status -eq "Online" -and !$enable) {
        Write-Host "Stopping $typeName service instance..."
        $svc | Stop-SPServiceInstance -Confirm:$false
    }
}

function Set-FailoverDatabase($databaseName, [string]$failoverDbServer) {
    if (![string]::IsNullOrEmpty($failoverDbServer)) {
        $db = Get-SPDatabase | where {$_.Name -eq $databaseName}
        if ($db -eq $null) { 
            throw "Unable to retrieve the database to set the failover server: $databaseName" 
        }
        if (($db.FailoverServiceInstance -eq $null) -or ![string]::Equals($db.FailoverServiceInstance.NormalizedDataSource, $failoverDbServer, [StringComparison]::OrdinalIgnoreCase)) {
            try {
                Write-Host "Adding failover database instance..."
                $db.AddFailoverServiceInstance($failoverDbServer)
                $db.Update()
            } catch {
                Write-Warning "Unable to set failover database server. $_"
            }
        }
    }
}

 

I honestly don’t have the time to explain everything that this script is doing but suffice it to say it is doing a bit more than just creating or connecting to the Farm. One thing in particular is that I chose to put the enabling and disabling of some of the simple services (Web Application, Workflow, and Incoming Email) in this script rather than in the script(s) that provision the Service Applications (this was really just a decision of convenience for me and in my personal scripts this stuff is stored elsewhere).

One other thing I wanted to highlight was this line within the script: “psconfig -cmd upgrade -inplace b2b”. The reason this is there is because we installed the Office Web Applications which incorrectly sets a registry key that indicates that an upgrade is required. We can either fix the registry key or simply open and then close the configuration wizard on each server (without actually letting the wizard run) or we can just run this command on each server just prior to calling the New-SPConfigurationDatabase or Connect-SPConfigurationDatabase cmdlets. The command will actually generate an error but you can safely ignore it – all we care is that it fixes the registry key for us so that we can move on to the actual provisioning.

Provision-WebApplications.ps1

The next script I demoed during the first session was the Provision-WebApplications.ps1 script. This script, aside from the obvious of provisioning Web Applications, also provisioned any initial Site Collections and managed paths that we needed. The following illustration details the three Web Applications we created along with their Application Pool association and other relevant details:

image

Now let’s take a look at the XML file that I used to provide all the configuration information:

<Farm>
  <WebApplications>
    <WebApplication Name="SharePoint MySites" HostHeader="my.isclondon.local" Port="80" DefaultTimeZone="2" Path="c:\sharepoint\webs\mysites" Ssl="false" LoadBalancedUrl="http://my.isclondon.local" AllowAnonymous="false" SelfServiceSiteCreation="true" RequireContactForSsc="false" AuthenticationMode="Classic" EnableWindowsAuthentication="false" AuthenticationMethod="NTLM" EnableBasicAuthentication="false" EnableFormsBasedAuthentication="false">
      <ApplicationPool Name="SharePoint Collab" AccountName="isclondon\SPCollab" AccountPassword="password" />
      <PortalSuperUserAccount AccountName="isclondon\spcacheuser" AccountPassword="password" />
      <PortalSuperReaderAccount AccountName="isclondon\spcachereader" AccountPassword="password" />
      <ManagedPaths>
        <ManagedPath Explicit="false" RelativeURL="/personal" />
      </ManagedPaths>
      <UserPolicies>
        <UserPolicy UserLogin="isclondon\SPInstall" UserDisplayName="SharePoint Administrator" Zone="All">
          <Permission Name="Full Control" />
        </UserPolicy>
      </UserPolicies>
      <ProxyGroup Name="Default" />
      <SPDesigner AllowDesigner="true" AllowRevertFromTemplate="true" AllowMasterPageEditing="true" ShowURLStructure="true" />
      <ContentDatabases>
        <ContentDatabase Default="true" Server="ISCSharePoint1" FailoverDatabaseServer="" Name="ISC_Content_MySites" MaxSiteCount="15000" WarningSiteCount="9000">
          <SiteCollections>
            <SiteCollection Name="My Sites" Description="My Sites Host Site Collection" Url="http://my.isclondon.local" Template="SPSMSITEHOST#0" LCID="1033" OwnerLogin="isclondon\administrator" OwnerEmail="administrator@isclondon.com">
            </SiteCollection>
          </SiteCollections>
        </ContentDatabase>
      </ContentDatabases>
    </WebApplication>
    <WebApplication Name="SharePoint Team Sites" HostHeader="team.isclondon.local" Port="80" DefaultTimeZone="2" Path="c:\sharepoint\webs\teams" Ssl="false" LoadBalancedUrl="http://team.isclondon.local" AllowAnonymous="false" SelfServiceSiteCreation="false" RequireContactForSsc="false" AuthenticationMode="Classic" EnableWindowsAuthentication="false" AuthenticationMethod="NTLM" EnableBasicAuthentication="false" EnableFormsBasedAuthentication="false">
      <ApplicationPool Name="SharePoint Collab" AccountName="isclondon\SPCollab" AccountPassword="password" />
      <PortalSuperUserAccount AccountName="isclondon\spcacheuser" AccountPassword="password" />
      <PortalSuperReaderAccount AccountName="isclondon\spcachereader" AccountPassword="password" />
      <ManagedPaths>
      </ManagedPaths>
      <UserPolicies>
        <UserPolicy UserLogin="isclondon\SPInstall" UserDisplayName="SharePoint Administrator" Zone="All">
          <Permission Name="Full Control" />
        </UserPolicy>
      </UserPolicies>
      <ProxyGroup Name="Default" />
      <SPDesigner AllowDesigner="true" AllowRevertFromTemplate="false" AllowMasterPageEditing="true" ShowURLStructure="true" />
      <ContentDatabases>
        <ContentDatabase Default="true" Server="ISCSharePoint1" FailoverDatabaseServer="" Name="ISC_Content_TeamSites" MaxSiteCount="15000" WarningSiteCount="9000">
          <SiteCollections>
            <SiteCollection Name="Team Sites" Url="http://team.isclondon.local" Template="CMSPUBLISHING#0" LCID="1033" OwnerLogin="isclondon\administrator" OwnerEmail="administrator@isclondon.com">
            </SiteCollection>
          </SiteCollections>
        </ContentDatabase>
      </ContentDatabases>
      <Features />
    </WebApplication>
    <WebApplication Name="SharePoint Intranet" HostHeader="intranet.isclondon.local" Port="80" DefaultTimeZone="2" Path="c:\sharepoint\webs\intranet" Ssl="false" LoadBalancedUrl="http://intranet.isclondon.local" AllowAnonymous="false" SelfServiceSiteCreation="false" RequireContactForSsc="false" AuthenticationMode="Claims" EnableWindowsAuthentication="true" AuthenticationMethod="NTLM" EnableBasicAuthentication="false" EnableFormsBasedAuthentication="false">
      <ApplicationPool Name="SharePoint Content" AccountName="isclondon\SPContent" AccountPassword="password" />
      <PortalSuperUserAccount AccountName="isclondon\spcacheuser" AccountPassword="password" />
      <PortalSuperReaderAccount AccountName="isclondon\spcachereader" AccountPassword="password" />
      <ManagedPaths>
        <ManagedPath Explicit="true" RelativeURL="/CTHub" />
      </ManagedPaths>
      <UserPolicies>
        <UserPolicy UserLogin="isclondon\SPInstall" UserDisplayName="SharePoint Administrator" Zone="All">
          <Permission Name="Full Control" />
        </UserPolicy>
      </UserPolicies>
      <ProxyGroup Name="Default" />
      <SPDesigner AllowDesigner="true" AllowRevertFromTemplate="false" AllowMasterPageEditing="true" ShowURLStructure="true" />
      <ContentDatabases>
        <ContentDatabase Default="true" Server="ISCSharePoint1" FailoverDatabaseServer="" Name="ISC_Content_Intranet" MaxSiteCount="15000" WarningSiteCount="9000">
          <SiteCollections>
            <SiteCollection Name="Intranet" Url="http://intranet.isclondon.local" Template="STS#0" LCID="1033" OwnerLogin="isclondon\administrator" OwnerEmail="administrator@isclondon.com">
            </SiteCollection>
            <SiteCollection Name="Content Type Hub" Url="http://intranet.isclondon.local/CTHub" Template="STS#1" LCID="1033" OwnerLogin="isclondon\administrator" OwnerEmail="administrator@isclondon.com">
            </SiteCollection>
          </SiteCollections>
        </ContentDatabase>
      </ContentDatabases>
      <Features />
    </WebApplication>    
  </WebApplications>
</Farm>

 

At first glance the XML file, due to it’s size, may seem a bit overwhelming but hopefully as you go through it you’ll see that it really is a pretty natural and somewhat obvious structure. (Think about the possibilities if you had a great UI to manage this XML structure – wink, wink).

Okay, now for the code – and there’s a fair bit of it:

function Provision-WebApplications() {
    <#
    .Synopsis
        Creates the web applications.
    .Description
        Creates the web applications.
    .Example
        PS C:\> . .\Provision-WebApplications.ps1
        PS C:\> Provision-WebApplication -SettingsFile c:\WebApplications.xml
    .Example
        PS C:\> . .\Provision-WebApplications.ps1
        PS C:\> Provision-WebApplications -ConfigXml ([xml](Get-Content c:\farmconfiguration.xml))
    .Parameter SettingsFile
        The path to an XML file.
    #>
    [CmdletBinding(DefaultParameterSetName="FilePath")]
    param (
        [Parameter(Mandatory=$true, Position=0, ParameterSetName="XmlElement")]
        [ValidateNotNull()]
        [System.Xml.XmlElement]$ConfigElement,

        [Parameter(Mandatory=$true, Position=0, ParameterSetName="XmlDocument")]
        [ValidateNotNull()]
        [xml]$ConfigXml,

        [Parameter(Mandatory=$true, Position=0, ParameterSetName="FilePath")]
        [ValidateNotNullOrEmpty()]
        [string]$SettingsFile
    )
    switch ($PsCmdlet.ParameterSetName) { 
        "XmlDocument" { 
            if ($ConfigXml.Farm.WebApplications.WebApplication -ne $null) {
                foreach ($appConfig in $ConfigXml.Farm.WebApplications.WebApplication) {
                    Provision-WebApplications -ConfigElement $appConfig
                }
                return
            }
        }
        "FilePath" {
            Provision-WebApplications -ConfigXml ([xml](Get-Content $SettingsFile))
            return
        }
    }
    $config = $ConfigElement

    if ($config -eq $null) {
        Write-Warning "No web application defined. Skipping."
        return
    }

    
    $webApp = Get-SPWebApplication -Identity $config.Name -ErrorAction SilentlyContinue
    
    if ($webApp -eq $null) {
        $allowAnon = [bool]::Parse($config.AllowAnonymous.ToString())
        $ssl = [bool]::Parse($config.Ssl.ToString())

        $db = $null
        if ($config.ContentDatabases -eq $null) {
            throw "A content database configuration setting could not be found for `"$($config.Name)`"."
        }
        if ($config.ContentDatabases.ChildNodes.Count -gt 1) {
            $db = $config.ContentDatabases.ContentDatabase | where {$_.Default -eq "true"}
            if ($db -is [array]) {
                Write-Warning "Multiple content databases set as default for `"$($config.Name)`" (using first)"
                $db = $db[0]
            }
        } else {
            $db = $config.ContentDatabases.ContentDatabase
        }
        if ($db -eq $null) {
            throw "A content database configuration setting could not be found for `"$($config.Name)`"."
        }
        
        $poolName = $config.ApplicationPool.Name
        $poolAcctName = $config.ApplicationPool.AccountName
        $poolAcctPwd = $config.ApplicationPool.AccountPassword
        $poolAcct = $null
        
        #Check for existing Application Pool
        $pools = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.ApplicationPools
        if (($pools | ? {$_.Name -eq $poolName}) -eq $null) {
            $poolAcct = Get-SPManagedAccount $poolAcctName -ErrorAction SilentlyContinue
            if ($poolAcct -eq $null) {
                $securePwd = ConvertTo-SecureString $poolAcctPwd -AsPlainText -force
                $cred = New-Object System.Management.Automation.PSCredential $poolAcctName, $securePwd
                $poolAcct = New-SPManagedAccount -Credential $cred
            }
        }
        
        $loadBalancedUrl = $config.LoadBalancedUrl
        $port = $config.Port
        if (![string]::IsNullOrEmpty($loadBalancedUrl)) {
            $loadBalancedUri = New-Object System.Uri $config.LoadBalancedUrl
            $port = $loadBalancedUri.Port
            $loadBalancedUrl = "$($loadBalancedUri.Scheme)://$($loadBalancedUri.Host)/"
        } else {
            if (![string]::IsNullOrEmpty($config.HostHeader)) {
                $loadBalancedUrl = "http://$($config.HostHeader)"
            }
        }
        if ([string]::IsNullOrEmpty($port)) { $port = "80" }

        if ($config.AuthenticationMode -eq "Claims") {
            $authProviders = @()
            $enableWindowsAuthentication = $false
            if (![string]::IsNullOrEmpty($config.EnableWindowsAuthentication)) { $enableWindowsAuthentication = [bool]::Parse($config.EnableWindowsAuthentication) }
            $enableFormsBasedAuthentication = $false
            if (![string]::IsNullOrEmpty($config.EnableFormsBasedAuthentication)) { $enableFormsBasedAuthentication = [bool]::Parse($config.EnableFormsBasedAuthentication) }

            if ($enableWindowsAuthentication) {
                $enableBasicAuthentication = $false
                if (![string]::IsNullOrEmpty($config.EnableBasicAuthentication)) { $enableBasicAuthentication = [bool]::Parse($config.EnableBasicAuthentication) }
                $disableKerberos = $config.AuthenticationMethod -eq "NTLM"
                $authProviders += New-SPAuthenticationProvider `
                    -UseWindowsIntegratedAuthentication:$enableWindowsAuthentication `
                    -DisableKerberos:$disableKerberos `
                    -UseBasicAuthentication:$enableBasicAuthentication `
                    -AllowAnonymous:$allowAnon
            }
            if ($enableFormsBasedAuthentication) {
                $authProviders += New-SPAuthenticationProvider `
                    -ASPNETMembershipProvider $config.ASPNETMembershipProviderName `
                    -ASPNETRoleProviderName $config.ASPNETRoleManagerName 
            }
            Write-Host "Creating Web Application: `"$($config.Name)`""
            $webApp = New-SPWebApplication -SecureSocketsLayer:$ssl `
                -AllowAnonymousAccess:$allowAnon `
                -ApplicationPool $poolName `
                -ApplicationPoolAccount $poolAcct `
                -Name $config.Name `
                -AuthenticationProvider $authProviders `
                -DatabaseServer $db.Server `
                -DatabaseName $db.Name `
                -HostHeader $config.HostHeader `
                -Path $config.Path `
                -Port $port `
                -Url $loadBalancedUrl
        } else {
            $authMethod = "NTLM"
            if (![string]::IsNullOrEmpty($config.AuthenticationMethod)) { $authMethod = $config.AuthenticationMethod }

            Write-Host "Creating Web Application: `"$($config.Name)`""
            $webApp = New-SPWebApplication -SecureSocketsLayer:$ssl `
                -AllowAnonymousAccess:$allowAnon `
                -ApplicationPool $poolName `
                -ApplicationPoolAccount $poolAcct `
                -Name $config.Name `
                -AuthenticationMethod $authMethod `
                -DatabaseServer $db.Server `
                -DatabaseName $db.Name `
                -HostHeader $config.HostHeader `
                -Path $config.Path `
                -Port $port `
                -Url $loadBalancedUrl
        }

        Set-SPWebApplication -Identity $webApp -DefaultTimeZone $config.DefaultTimeZone

        #Re-get the web app to avoid update conflicts
        $webApp = Get-SPWebApplication -Identity  $webApp
        $webApp.SelfServiceSiteCreationEnabled = [bool]::Parse($config.SelfServiceSiteCreation)
        $webApp.RequireContactForSelfServiceSiteCreation = [bool]::Parse($config.RequireContactForSsc)
        if ($config.ProxyGroup -ne $null -and ![string]::IsNullOrEmpty($config.ProxyGroup.Name)) {
            $proxyGroupName = $config.ProxyGroup.Name
            if ($proxyGroupName -ne $null) {
                $webApp.ServiceApplicationProxyGroup = Get-ProxyGroup $proxyGroupName $true
            }
        }
        $webApp.Update()
        Write-Host """$($config.Name)"" successfully created."
    } else {
        #We got the web app so return it and don't attempt to recreate
        Write-Host """$($config.Name)"" already exists, skipping creation."
    }
    $setObjectCacheAccounts = $true
    if (![string]::IsNullOrEmpty($config.PortalSuperUserAccount) -and ![string]::IsNullOrEmpty($config.PortalSuperReaderAccount)) {
        if ($config.PortalSuperUserAccount.AccountName -eq $config.PortalSuperReaderAccount.AccountName) {
            Write-Warning "The Portal Super User Account and Portal Super Reader Account must not be the same account as this will result in a security hole. The accounts will not be set."
            $setObjectCacheAccounts = $false
        }
    }
    $claimsPrefix = ""
    if ($config.AuthenticationMode -eq "Claims") {
        $claimsPrefix = "i:0#.w|"
    }
    if (![string]::IsNullOrEmpty($config.PortalSuperUserAccount.AccountName) -and $setObjectCacheAccounts) {
        $webApp.Properties["portalsuperuseraccount"] = "$claimsPrefix$($config.PortalSuperUserAccount.AccountName)"
        Set-WebAppUserPolicy $webApp "$claimsPrefix$($config.PortalSuperUserAccount.AccountName)" $config.PortalSuperUserAccount.AccountName "Full Control"
    }
    if (![string]::IsNullOrEmpty($config.PortalSuperReaderAccount.AccountName) -and $setObjectCacheAccounts) {
        $webApp.Properties["portalsuperreaderaccount"] = "$claimsPrefix$($config.PortalSuperReaderAccount.AccountName)"
        Set-WebAppUserPolicy $webApp "$claimsPrefix$($config.PortalSuperReaderAccount.AccountName)" $config.PortalSuperReaderAccount.AccountName "Full Read"
    }


    if ($config.UserPolicies) {
        Write-Host "Creating user policies..."
        [bool]$updateRequired = $false
        foreach ($userPolicyConfig in $config.UserPolicies.UserPolicy) {
            if ($userPolicyConfig -eq $null) { continue }

            [string]$zoneName = $userPolicyConfig.Zone
            [Microsoft.SharePoint.Administration.SPPolicyCollection]$policies = $webApp.Policies
            if ($zoneName.ToLower() -ne "all") {
                $policies = $webApp.ZonePolicies($zoneName)
            }
            Write-Host "Adding user policy for: $claimsPrefix$($userPolicyConfig.UserLogin)..."
            [Microsoft.SharePoint.Administration.SPPolicy]$policy = $policies.Add("$claimsPrefix$($userPolicyConfig.UserLogin)", $userPolicyConfig.UserDisplayName)
            foreach ($permConfig in $userPolicyConfig.Permission) {
                [Microsoft.SharePoint.Administration.SPPolicyRole]$policyRole = $webApp.PolicyRoles | where {$_.Name -eq $permConfig.Name}
                if ($policyRole -ne $null) {
                    Write-Host "Adding policy role: $($permConfig.Name)..."
                    $policy.PolicyRoleBindings.Add($policyRole)
                }
            }
            $updateRequired = $true
        }
        if ($updateRequired) {
            $webApp.Update()
        }
    }
    
    
    if ($config.ManagedPaths) {
        Write-Host "Creating managed paths..."
        foreach ($mpConfig in $config.ManagedPaths.ManagedPath) {
            if ($mpConfig -eq $null) { continue }

            $path = Get-SPManagedPath -Identity $mpConfig.RelativeURL `
                -WebApplication $webApp -ErrorAction SilentlyContinue
            if ($path -eq $null) {
                Write-Host "Creating ""$($mpConfig.RelativeURL)""..."
                New-SPManagedPath -RelativeURL $mpConfig.RelativeURL `
                    -Explicit :( [bool]::Parse($mpConfig.Explicit)) -WebApplication $webApp
            } else {
                Write-Host "Managed path already exists: ""$($mpConfig.RelativeURL)""."
            }
        }
    }
    
    if ($config.SPDesigner) {
        Write-Host "Configuring SharePoint Designer Settings..."
        sleep 5
        #Get the web app again to avoid an update conflict
        $webApp = Get-SPWebApplication $webApp
        $webApp | Set-SPDesignerSettings `
            -AllowDesigner ([bool]::Parse($config.SPDesigner.AllowDesigner)) `
            -AllowRevertFromTemplate ([bool]::Parse($config.SPDesigner.AllowRevertFromTemplate)) `
            -AllowMasterPageEditing ([bool]::Parse($config.SPDesigner.AllowMasterPageEditing)) `
            -ShowURLStructure ([bool]::Parse($config.SPDesigner.ShowURLStructure))
        $webApp.Update()
    }

    foreach ($dbConfig in $config.ContentDatabases.ContentDatabase) {
        $db = Get-SPContentDatabase $dbConfig.Name -ErrorAction SilentlyContinue
        if ($db -eq $null) { 
            $db = New-SPContentDatabase -Name $dbConfig.Name `
                -WebApplication $webApp `
                -DatabaseServer $dbConfig.Server `
                -MaxSiteCount $dbConfig.MaxSiteCount `
                -WarningSiteCount $dbConfig.WarningSiteCount
        }
        $failoverDbServer = $dbConfig.FailoverDatabaseServer
        if (![string]::IsNullOrEmpty($failoverDbServer)) {
            if (($db.FailoverServiceInstance -eq $null) -or ![string]::Equals($db.FailoverServiceInstance.NormalizedDataSource, $failoverDbServer, [StringComparison]::OrdinalIgnoreCase)) {
                try {
                    Write-Host "Adding failover database instance..."
                    $db.AddFailoverServiceInstance($failoverDbServer)
                    $db.Update()
                } catch {
                    Write-Warning "Unable to set failover database server. $_"
                }
            }
        }
        foreach ($siteConfig in $dbConfig.SiteCollections.SiteCollection) {
            $site = Get-SPSite $siteConfig.Url -ErrorAction SilentlyContinue
            if ($site -ne $null) {
                Write-Host "Site Collection $($siteConfig.Url) already exists. Skipping."
                $site.Dispose()
                continue
            }
            
            $lcid = $siteConfig.LCID
            if ([string]::IsNullOrEmpty($lcid)) { $lcid = [Microsoft.SharePoint.SPRegionalSettings]::GlobalServerLanguage.LCID }

            $optionalParams = @{}
            if (![string]::IsNullOrEmpty($siteConfig.SecondaryLogin)) { 
                $optionalParams += @{"SecondaryOwnerAlias"=$siteConfig.SecondaryLogin} }
            if (![string]::IsNullOrEmpty($siteConfig.SecondaryEmail)) { 
                $optionalParams += @{"SecondaryEmail"=$siteConfig.SecondaryEmail} }
            if (![string]::IsNullOrEmpty($siteConfig.OwnerEmail)) { 
                $optionalParams += @{"OwnerEmail"=$siteConfig.OwnerEmail} }

            Write-Host "Creating Site Collection $($siteConfig.Url)..."
            $site = New-SPSite `
                -Url $siteConfig.Url `
                -Description $siteConfig.Description `
                -Language $lcid `
                -Name $siteConfig.Name `
                -OwnerAlias $siteConfig.OwnerLogin `
                -Template $siteConfig.Template `
                -ContentDatabase $db `
                -ErrorAction Continue @optionalParams

            if ($site -eq $null) { 
                Write-Warning "Site collection was not created!"
            } else {
                Write-Host "Site collection successfully created: $($siteConfig.Url)"
                Write-Host "Setting default associated security groups..."
                sleep 5
                $refreshedSite = $site | Get-SPSite
                $secondaryLogin = $siteConfig.SecondaryLogin
                if (![string]::IsNullOrEmpty($secondaryLogin)) { $secondaryLogin = "$claimsPrefix$secondaryLogin" }
                $refreshedSite.RootWeb.CreateDefaultAssociatedGroups("$claimsPrefix$($siteConfig.OwnerLogin)", $secondaryLogin, $siteConfig.Name)
                $refreshedSite.Dispose()
                $site.Dispose()
            }
        }
    }
}

function Get-ProxyGroup([string]$name, [bool]$createIfMissing = $true) {
    if ($name -eq "Default" -or [string]::IsNullOrEmpty($name)) { return [Microsoft.SharePoint.Administration.SPServiceApplicationProxyGroup]::Default }
    
    $proxyGroup = Get-SPServiceApplicationProxyGroup $name -ErrorAction SilentlyContinue -ErrorVariable err
    if ($err -and !$createIfMissing) { throw $err }
    if ($proxyGroup -eq $null) {
        $proxyGroup = New-SPServiceApplicationProxyGroup -Name $name
    }
    return $proxyGroup
}

function Set-WebAppUserPolicy($webApp, $userName, $userDisplayName, $perm) {
    [Microsoft.SharePoint.Administration.SPPolicyCollection]$policies = $webApp.Policies
    [Microsoft.SharePoint.Administration.SPPolicy]$policy = $policies.Add($userName, $userDisplayName)
    [Microsoft.SharePoint.Administration.SPPolicyRole]$policyRole = $webApp.PolicyRoles | where {$_.Name -eq $perm}
    if ($policyRole -ne $null) {
        $policy.PolicyRoleBindings.Add($policyRole)
    }
    $webApp.Update()
}

 

I think one of the more interesting things to point out about this script is that if you’re provisioning a Web Application for Claims versus Classic mode there are differences in how the New-SPWebApplication cmdlet must be called and, when using Claims, there’s more work that needs to be done to make sure the authentication providers are set properly. Outside of that I really think the script is pretty straightforward, if not a bit lengthy. One little hack/cheat that I wanted to point out is my use of the claims prefix, "i:0#.w|" – most of the time my little hack to get this prefix will be more than sufficient but technically, if I wanted my script to be truly generic, I should have done this to get the encoded username (as opposed to just assuming the prefix value and prepending it to the username):

$claim = [Microsoft.SharePoint.Administration.Claims.SPClaimProviderManager]::CreateUserClaim("isclondon\spcacheuser", "Windows")
[Microsoft.SharePoint.Administration.Claims.SPClaimProviderManager]::Local.EncodeClaim($claim)

SecureStoreServices.ps1

The last script that I walked through during the sessions was the SecureStoreServices.ps1 script. (It would have obviously been impossible to walk through all the Service Application related scripts during a single 1 hour presentation so the presentation itself focused on the general patterns, and lack thereof, and we then walked through this one script as a simple example which ties it all together).

Like all the other scripts this one was driven by another XML file:

<Farm>
    <Services>
        <SecureStoreServices>
          <Servers>
            <Server Name="SP03" />
            <Server Name="SP04" />
          </Servers>
          <SecureStoreServiceApplications>
            <SecureStoreServiceApplication 
                Name="ISC Secure Store Service" 
                DatabaseName="ISC_SecureStore" 
                DatabaseServer="ISCSharePoint1" 
                FailoverDatabaseServer=""
                AuditingEnabled="true" 
                AuditLogMaxSize="30" 
                Sharing="false" 
                KeyPassphrase="p@ssw0rd" 
                Partitioned="false">
              <ApplicationPool Name="SharePoint Services App Pool" 
                      AccountName="isclondon\SPServices" 
                    AccountPassword="password" />
              <Proxy Name="ISC Secure Store Service">
                <ProxyGroup Name="Default" />
              </Proxy>
            </SecureStoreServiceApplication>
          </SecureStoreServiceApplications>
        </SecureStoreServices>    
    </Services>
</Farm>

Hopefully you’ve picked up on the fact that the XML node structure is such that you could easily stitch all the XML files together into a single XML file which would be easier to maintain (think search and replace); I’ve intentionally structured my scripts and XML files for this conference so that they are all essentially standalone which made walking through the code in a presentation much easier as there was a lot less context switching. For my personal scripts I use a series of shared library scripts where I’ve put common functions and all my XML is contained in one XML file (which I manage using a custom WPF application that I’ve developed).

Time for the actual script:

function Start-SecureStoreServices {
    [CmdletBinding()]
    param 
    (  
        [Parameter(Mandatory=$true, Position=0)]
        [ValidateNotNullOrEmpty()]
        [string]$SettingsFile
    )
    
    [xml]$config = Get-Content $settingsFile

    $install = (($config.Farm.Services.SecureStoreServices.Servers.Server | where {$_.Name -eq $env:computername}) -ne $null)
    if (!$install) { 
        Write-Host "Machine not specified in Servers element, service will not be installed on this server."
        return
    }
    
    $svc = Get-SPServiceInstance -Server $env:computername | where {$_.TypeName -eq "Secure Store Service"}
    if ($svc -eq $null) {
        throw "Unable to retrieve Service Instance."
    }
    if ($svc.Status -ne "Online") {
        Write-Host "Starting service instance..."
        $svc | Start-SPServiceInstance
    
        #Make sure the service is online before attempting to add a svc app.
        while ($true) {
            Start-Sleep 2
            $svc = Get-SPServiceInstance -Server $env:computername | where {$_.TypeName -eq "Secure Store Service"}
            if ($svc.Status -eq "Online") { break }
        }
    }


    foreach ($appConfig in $config.Farm.Services.SecureStoreServices.SecureStoreServiceApplications.SecureStoreServiceApplication) {
        $app = Get-SPServiceApplication | where {$_.Name -eq $appConfig.Name}
        $updateKey = $false
        if ($app -eq $null) {
            $poolName = $appConfig.ApplicationPool.Name
            $identity = $appConfig.ApplicationPool.AccountName
            $password = $appConfig.ApplicationPool.AccountPassword
            $pool = Get-ServicePool $poolName $identity $password

            Write-Host "Creating secure store service application..."
            $app = New-SPSecureStoreServiceApplication -Name $appConfig.Name `
                -ApplicationPool $pool `
                -DatabaseServer $appConfig.DatabaseServer `
                -DatabaseName $appConfig.DatabaseName `
                -AuditingEnabled :( [bool]::Parse($appConfig.AuditingEnabled)) `
                -AuditLogMaxSize $appConfig.AuditLogMaxSize `
                -FailoverDatabaseServer $appConfig.FailoverDatabaseServer `
                -PartitionMode:([bool]::Parse($appConfig.Partitioned)) `
                -Sharing:([bool]::Parse($appConfig.Sharing))
            $updateKey = $true
        } else {
            Write-Host "Secure store service application already exists, skipping creation."
        }

        $proxy = Get-SPServiceApplicationProxy | where {$_.Name -eq $appConfig.Proxy.Name}
        if ($proxy -eq $null) {
            Write-Host "Creating secure store service application proxy..."
            $proxy = New-SPSecureStoreServiceApplicationProxy `
                -Name $appConfig.Proxy.Name `
                -ServiceApplication $app `
                -DefaultProxyGroup:$false
        } else {
            Write-Host "Secure store service application proxy already exists, skipping creation."
        }
        $proxy | Set-ProxyGroupsMembership $appConfig.Proxy.ProxyGroup

        
        if ($updateKey) {
            Update-SPSecureStoreMasterKey -ServiceApplicationProxy $proxy -Passphrase $appConfig.KeyPassphrase
            while ($true) {
                try {
                    Start-Sleep -Seconds 5
                    Update-SPSecureStoreApplicationServerKey -ServiceApplicationProxy $proxy -Passphrase $appConfig.KeyPassphrase
                    break
                } catch { }
            }
        }
        
    }
}



function Get-ServicePool($poolName, $identity, $password) {
    $pool = Get-SPServiceApplicationPool $poolName -ErrorAction SilentlyContinue
    if ($pool -eq $null) {
        if ($identity -ne $null) {
            $acct = Get-SPManagedAccount $identity -ErrorAction SilentlyContinue
        }
        if ($acct -eq $null) {
            if ([string]::IsNullOrEmpty($password)) {
                $cred = Get-Credential $identity
            } else {
                $cred = New-Object System.Management.Automation.PSCredential $identity, (ConvertTo-SecureString $password -AsPlainText -force)
            }
            $acct = Get-SPManagedAccount $cred.UserName -ErrorAction SilentlyContinue
            if ($acct -eq $null) {
                $acct = New-SPManagedAccount $cred
            }
        }
        Write-Host "Creating application pool..."
        $pool = New-SPServiceApplicationPool  -Name $poolName -Account $acct
    }
    return $pool
}

function Set-ProxyGroupsMembership([System.Xml.XmlElement[]]$groups, [Microsoft.SharePoint.Administration.SPServiceApplicationProxy[]]$InputObject) {
    begin {}
    process {
        if ($_ -eq $null -and $InputObject -ne $null) {
            $InputObject | Set-ProxyGroupsMembership $groups
            return
        }
        $proxy = $_
        
        #Clear any existing proxy group assignments
        Get-SPServiceApplicationProxyGroup | where {$_.Proxies -contains $proxy} | ForEach-Object {
            $proxyGroupName = $_.Name
            if ([string]::IsNullOrEmpty($proxyGroupName)) { $proxyGroupName = "Default" }
            $group = $null
            [bool]$matchFound = $false
            foreach ($g in $groups) {
                $group = $g.Name
                if ($group -eq $proxyGroupName) { 
                    $matchFound = $true
                    break 
                }
            }
            if (!$matchFound) {
                Write-Host "Removing ""$($proxy.DisplayName)"" from ""$proxyGroupName"""
                $_ | Remove-SPServiceApplicationProxyGroupMember -Member $proxy -Confirm:$false -ErrorAction SilentlyContinue
            }
        }
        
        foreach ($g in $groups) {
            $group = $g.Name

            $pg = $null
            if ($group -eq "Default" -or [string]::IsNullOrEmpty($group)) {
                $pg = [Microsoft.SharePoint.Administration.SPServiceApplicationProxyGroup]::Default
            } else {
                $pg = Get-SPServiceApplicationProxyGroup $group -ErrorAction SilentlyContinue -ErrorVariable err
                if ($pg -eq $null) {
                    $pg = New-SPServiceApplicationProxyGroup -Name $name
                }
            }
            
            $pg = $pg | where {$_.Proxies -notcontains $proxy}
            if ($pg -ne $null) { 
                Write-Host "Adding ""$($proxy.DisplayName)"" to ""$($pg.DisplayName)"""
                $pg | Add-SPServiceApplicationProxyGroupMember -Member $proxy 
            }
        }
    }
    end {}
}

 

Again, I’m not going to explain what’s going on in this script – my book covers it in detail.

Final Thoughts

So on that final note about my book I’d like to reiterate that everything I demoed during the conference and virtually everything that my scripts did to provision the conference Farm is detailed in great depth in my Automating Microsoft SharePoint 2010 Administration with Windows PowerShell 2.0 book (I don’t cover the Office Web Applications in the book). Not only that, but most of the scripts are also available for download as part of the book. Are all of them available? No. Why? Because I’m a self employed consultant who needs to be able to provide for my family and if I give away everything I ever produce then I’ll slowly be putting myself out of a job and my daughter can forget about going to college so please, please, please do not ask me to post the full scripts because it’s simply not going to happen – if you want the full scripts then hire me to build your Farm or talk to me about purchasing my SharePoint 2010 Farm Builder application. (I know during the conference people were tweeting and blogging that I would be providing all my scripts – I never said I would do this – I simply said that I would provide the scripts which I walked through during the session and this is what I have done in this post).

To those that attended my sessions I truly hope you got some value out of the content that Spence, Chan and I provided and I’d like to thank everyone (speakers, organizers, and attendees) for such a wonderful overall event and for making me feel so welcome so far from home. And of course a special thanks to Spence and Chan for all they did to help pull these sessions off.

-Gary

16Mar/124

Exporting and Importing SharePoint 2010 Terms

Ever had the need to migrate Terms from the Managed Metadata Term Store from one environment to another? Do you find the flat, CSV, import approach provided out of the box to be insufficient (especially with its lack of support for alternate labels)? When I first started working with Terms over two years ago I was extremely frustrated by the lack of export and import capabilities so I decided to solve the problem myself by creating two cmdlets, Export-SPTerms and Import-SPTerms. I’ve actually had these cmdlets publicly available for about two years now but I suspect very few people actually know they exist so I thought I’d put together this short post just to highlight them (I also recently pushed out an update which adds support for Site Collection scoped Groups if you have SP1 deployed).

I don’t want to go into a lot of detail regarding how these cmdlets work as they’re really very simple and the bulk of the code is just about iterating through the Term Store structure and turning the various objects into an XML structure (you can download the code from my downloads page). So with brevity in mind, here’s the full help for the Export-SPTerms cmdlet:

NAME
    Export-SPTerms
   
SYNOPSIS
    Export the Managed Metadata Terms.
   
SYNTAX
    Export-SPTerms [-TaxonomySession] <SPTaxonomySessionPipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Export-SPTerms [-TermStore] <SPTaxonomyTermStorePipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Export-SPTerms [-Group] <SPTaxonomyGroupPipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Export-SPTerms [-TermSet] <SPTaxonomyTermSetPipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Export-SPTerms [-Term] <SPTaxonomyTermPipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
   
DESCRIPTION
    Export the Managed Metadata Terms.
   
    Copyright 2011 Falchion Consulting, LLC
    > For more information on this cmdlet and others:
    >http://blog.falchionconsulting.com/
    > Use of this cmdlet is at your own risk.
    > Gary Lapointe assumes no liability.
   

PARAMETERS
    -TaxonomySession <SPTaxonomySessionPipeBind>
        The TaxonomySession object containing the Term Stores to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    -TermStore <SPTaxonomyTermStorePipeBind>
        The TermStore object containing the terms to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -Group <SPTaxonomyGroupPipeBind>
        The Group object containing the terms to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -TermSet <SPTaxonomyTermSetPipeBind>
        The TermSet object containing the terms to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -Term <SPTaxonomyTermPipeBind>
        The Term object containing the terms to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -OutputFile [<String>]
        The path to the file to save the terms to.
       
        Required?                    false
        Position?                    2
        Default value               
        Accept pipeline input?       false
        Accept wildcard characters?  false
       
    -AssignmentCollection [<SPAssignmentCollection>]
        Manages objects for the purpose of proper disposal. Use of objects, such as SPWeb or SPSite, can use large amounts of memory and use of these objects in Windows PowerShell scripts requires proper memory management. Using the SPAssignment object, you can assign objects to a variable and dispose of the objects after they are needed to free up memory. When SPWeb, SPSite, or SPSiteAdministration objects are used, the objects are automatically disposed of if an assignment collection or the Global parameter is not used.
       
        When the Global parameter is used, all objects are contained in the global store. If objects are not immediately used, or disposed of by using the Stop-SPAssignment command, an out-of-memory scenario can occur.
       
        Required?                    false
        Position?                    named
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        "get-help about_commonparameters".
   
INPUTS
   
   
   
OUTPUTS
   
   
   
NOTES
   
   
    For more information, type "Get-Help Export-SPTerms -detailed". For technical information, type "Get-Help Export-SPTerms -full".
   
    ------------------EXAMPLE 1-----------------------
   
    PS C:\> Export-SPTerms -TaxonomySession "http://site/" -OutputFile "c:\terms.xml"
   
   
    This example exports the terms for all term stores associated with the site and saves to c:\terms.xml.
   
    ------------------EXAMPLE 2-----------------------
   
    PS C:\> Export-SPTerms -Group (Get-SPTaxonomySession -Site "http://site/").TermStores[0].Groups[0] -OutputFile "c:\terms.xml"
   
   
    This example exports the first Group of the first Term Store and saves to c:\terms.xml.
   
   
RELATED LINKS
    Import-SPTerms
    Get-SPTaxonomySession

 

Now lets look at the Import-SPTerms cmdlet (it’s very similar):


NAME
    Import-SPTerms
   
SYNOPSIS
    Import the Managed Metadata Terms.
   
SYNTAX
    Import-SPTerms [-TaxonomySession] <SPTaxonomySessionPipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Import-SPTerms [-ParentTermStore] <SPTaxonomyTermStorePipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Import-SPTerms [-ParentGroup] <SPTaxonomyGroupPipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Import-SPTerms [-ParentTermSet] <SPTaxonomyTermSetPipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Import-SPTerms [-ParentTerm] <SPTaxonomyTermPipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
   
DESCRIPTION
    Import the Managed Metadata Terms.
   
    Copyright 2011 Falchion Consulting, LLC
    > For more information on this cmdlet and others:
    > http://blog.falchionconsulting.com/
    > Use of this cmdlet is at your own risk.
    > Gary Lapointe assumes no liability.
   

PARAMETERS
    -TaxonomySession <SPTaxonomySessionPipeBind>
        The TaxonomySession object to import Term Stores into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    -ParentTermStore <SPTaxonomyTermStorePipeBind>
        The TermStore object to import Groups into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -ParentGroup <SPTaxonomyGroupPipeBind>
        The Group object to import Term Sets into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -ParentTermSet <SPTaxonomyTermSetPipeBind>
        The TermSet object to import Terms into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -ParentTerm <SPTaxonomyTermPipeBind>
        The Term object to import Terms into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -InputFile <XmlDocumentPipeBind>
        The path to the file containing the terms to import or an XmlDocument object or XML string.
       
        Required?                    true
        Position?                    2
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    -AssignmentCollection [<SPAssignmentCollection>]
        Manages objects for the purpose of proper disposal. Use of objects, such as SPWeb or SPSite, can use large amounts of memory and use of these objects in Windows PowerShell scripts requires proper memory management. Using the SPAssignment object, you can assign objects to a variable and dispose of the objects after they are needed to free up memory. When SPWeb, SPSite, or SPSiteAdministration objects are used, the objects are automatically disposed of if an assignment collection or the Global parameter is not used.
       
        When the Global parameter is used, all objects are contained in the global store. If objects are not immediately used, or disposed of by using the Stop-SPAssignment command, an out-of-memory scenario can occur.
       
        Required?                    false
        Position?                    named
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        "get-help about_commonparameters".
   
INPUTS
   
   
   
OUTPUTS
   
   
   
NOTES
   
   
    For more information, type "Get-Help Import-SPTerms -detailed". For technical information, type "Get-Help Import-SPTerms -full".
   
    ------------------EXAMPLE 1-----------------------
   
    PS C:\> Import-SPTerms -ParentTermStore (Get-SPTaxonomySession -Site "http://site/").TermStores[0] -InputFile "c:\terms.xml"
   
   
    This example imports the Group from c:\terms.xml to the first Term Store.
   
    ------------------EXAMPLE 2-----------------------
   
    PS C:\> Import-SPTerms -TaxonomySession "http://site/" -InputFile "c:\terms.xml"
   
   
    This example imports the terms from c:\terms.xml to the Term Store associated with http://site.
   
   
RELATED LINKS
    Export-SPTerms
    Get-SPTaxonomySession

 

Using these cmdlets I can easily export terms from one environment, such as production, to another environment, such as my test environment. This avoids the need to have backup and restore the database associated with the term store (which requires removing and re-provisioning the service application. And the way I’ve written the cmdlets all the Term Set, Group, and Term IDs remain consistent and, because the exported XML is easily modified, when it comes to Site Collection scoped Groups you can do a simple search and replace to change Site Collection URLs to match the new target (note that I first check for a Site Collection with the specified URL and if not found then I use the ID and if I still can’t locate the Site Collection then I create the Group as a standard shared Group).

-Gary

11Mar/126

“Stamping” PDF Files Downloaded from SharePoint 2010

First off I want to clarify that the subject of this post is not my idea as it is something that my friend Roman Kobzarev put together for his company and I merely assisted with getting the code to work. The problem that Roman was trying to solve was that his company provided numerous PDF files that registered/paying members could download and, unfortunately, they were finding some of those files being posted to various other sites without their permission; so in an attempt to at least discourage this they wanted to stamp the PDF files with some information about the user who downloaded the file.

There are various ways in which this problem can be solved but perhaps one of the simpler approaches, and the approach outlined here, was to create an HTTP Handler which intercepted requests for any PDF files and then simply retrieve the file from SharePoint, modify it, and then send it on its way. The cool thing about this approach and the pattern shown here is that it can easily be applied to any file type which requires some user or request specific modifications applied to it.

Before I get to the actual code I want to point out one third party dependency that we used: iTextSharp. This is an open source .NET library for PDF generation and manipulation. There are many options available when looking to manipulate PDF files and in this case this one was chosen due to its cost (free). So let’s get to the code.

In terms of code there really isn’t that much which is what makes this such a nice solution. The first piece is the actual implementation of the IHttpHandler interface:

using System.IO;
using System.Web;
using Microsoft.SharePoint;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Aptillon.SharePoint.PDFWatermark
{
    public class PDFWatermarkHttpHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {

            SPFile file = SPContext.Current.Web.GetFile(context.Request.Url.ToString());
            byte[] content = file.OpenBinary();
            SPUser currentUser = SPContext.Current.Web.CurrentUser;

            string watermark = null;
            if (currentUser != null)
                watermark = "This download was specially prepared for " + currentUser.Name;

            if (watermark != null)
            {
                PdfReader pdfReader = new PdfReader(content);
                using (MemoryStream outputStream = new MemoryStream())
                using (PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream))
                {
                    for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
                    {
                        //Rectangle class in iText represent geometric representation... 
                        //in this case, rectangle object would contain page geometry
                        Rectangle pageRectangle = pdfReader.GetPageSizeWithRotation(pageIndex);
                        //PdfContentByte object contains graphics and text content of page returned by PdfStamper
                        PdfContentByte pdfData = pdfStamper.GetUnderContent(pageIndex);
                        //create font size for watermark
                        pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 8);
                        //create new graphics state and assign opacity
                        PdfGState graphicsState = new PdfGState();
                        graphicsState.FillOpacity = 0.4F;
                        //set graphics state to PdfContentByte
                        pdfData.SetGState(graphicsState);
                        //indicates start of writing of text
                        pdfData.BeginText();
                        //show text as per position and rotation
                        pdfData.ShowTextAligned(Element.ALIGN_CENTER, watermark, pageRectangle.Width / 4, pageRectangle.Height / 44, 0);
                        //call endText to invalid font set
                        pdfData.EndText();
                    }
                    pdfStamper.Close();
                    content = outputStream.ToArray();
                }
            }
            context.Response.ContentType = "application/pdf";
            context.Response.BinaryWrite(content);
            context.Response.End();
        }

        public bool IsReusable
        {
            get { return false; }
        }
    }
}

 

As you can see from the previous code listing, the bulk of the code involves the actual processing of the PDF file but the core SharePoint specific piece is in the beginning of the ProcessRequest() method where we use the SPContext.Current.Web.GetFile() method to retrieve the actual file requested and then, if we can get an actual SPUser object, we create a simple message that will be added to the bottom of the PDF. I’m not going to cover what’s happening with the iTextSharp objects as the point of this article is to demonstrate the pattern which can easily be applied to other file types and not how to use iTextSharp.

To deploy this class I created an empty SharePoint 2010 project using Visual Studio 2010 and added the file to the project. I then created a new Web Application scoped Feature which I use to add the appropriate web.config settings which will register the HTTP Handler. The following screenshot shows the final project structure:

SNAGHTML8af78243

Note that I also added the iTextSharp.dll to the project and added it as an additional assembly to the package by double clicking the Package.package element and then, in the package designer, click Advanced to add additional assemblies:

image

Before I show the code for the Web Application Feature I first want to show what settings I set for the Feature after adding it:

image

When you add a new Feature to a project it’s going to name it Feature1 and set the default scope to Web. The first thing I do is rename the Feature to something meaningful – in this case, because I know I’ll only have the one Feature I go ahead and name it the same as the project name: Aptillon.SharePoint.PDFWatermark (I always follow the same naming convention for my projects, which equate to WSP file names and Features: <Company>.SharePoint.<Something Appropriate for the Contained Functionality>). The next thing I do is change the Deployment Path property for the Feature so that it only uses the Feature name and does not prepend the project name; and finally I set the scope and title of the Feature. Now I’m ready to add my Feature activation event receiver.

The code that I want to include in the event receiver will handle the addition and removal of the web.config handler elements. I do this using the SPWebConfigModification class. Now there’s debate on whether this should be used or not; this is one of those classes where you might say (as my friend Spence Harbar puts it), “Just because you should use it doesn’t mean you can.” The simple explanation for this is that ideally you should be using this class to make web.config modifications but the reality is that this guy is fraught with issues and usually doesn’t work. That said, what I usually do is, where it makes sense, use this class to add and remove my entries but work under the premise that it will probably not work and plan on making these changes manually (or write a timer job which will do what this guy is attempting to do, but that’s out of scope of this article). So here’s the FeatureActivated() and FeatureDeactivating() methods that I use to add and remove the appropriate web.config entries which register the previously defined HTTP Handler:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    string asmDetails = typeof(PDFWatermarkHttpHandler).AssemblyQualifiedName;

    SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
    if (webApp == null) return;

    SPWebConfigModification modification = new SPWebConfigModification("add[@name=\"PDFWatermark\"]", "configuration/system.webServer/handlers");
    modification.Value = string.Format("<add name=\"PDFWatermark\" verb=\"*\" path=\"*.pdf\" type=\"{0}\" preCondition=\"integratedMode\" />", asmDetails);

    modification.Sequence = 1;
    modification.Owner = asmDetails;
    modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
    webApp.WebConfigModifications.Add(modification);

    webApp.Update();
    webApp.WebService.ApplyWebConfigModifications();
}


public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    string asmDetails = typeof(PDFWatermarkHttpHandler).AssemblyQualifiedName;

    SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
    if (webApp == null) return;

    List<SPWebConfigModification> configModsFound = new List<SPWebConfigModification>();
    Collection<SPWebConfigModification> modsCollection = webApp.WebConfigModifications;
    for (int i = 0; i < modsCollection.Count; i++)
    {
        if (modsCollection[i].Owner == asmDetails)
        {
            configModsFound.Add(modsCollection[i]);
        }
    }

    if (configModsFound.Count > 0)
    {
        foreach (SPWebConfigModification mod in configModsFound)
            modsCollection.Remove(mod);

        webApp.Update();
        webApp.WebService.ApplyWebConfigModifications();
    }
}

 

With all the code in place I can deploy to my Web Application and now any time a PDF is downloaded I’ll have a nice little message displayed on the bottom of each page. Again, the intent here is to show the simplicity of the pattern and approach – with a little imagination you can easily come up with lots of other uses for this (applying security or password protection to the PDF, adding an image watermark, removing pages based on registration status thus providing “sample” versions, prefilling form fields with user data, adding a version history page, etc.). And of course all this can also be applied to other file types such as the Office files or images (though image handling would take a little more logic to ignore images not coming from document libraries).

22Dec/1118

Updating SharePoint 2010 User Information

One of my clients recently had an issue where a particularly high profile user (CEO) had their title spelled incorrectly in Active Directory; unfortunately the error wasn’t noticed right away and now, despite changing the information in Active Directory, SharePoint was still showing the wrong title in the People Picker when granting the user rights to a Site Collection. Fortunately I had a partial PowerShell script to fix the issue and just needed to only slightly modify it – you can see the original script on pages 299 and 300 of my book. So before I show the modified script it’s first important to understand the problem and why I needed to use a script and why what I had in the book is somewhat incomplete.

Whenever you grant a user rights to a Site Collection or when that user creates/updates/deletes any item within a Site Collection, an entry for the user will be added to a hidden user information list, if not already there. This “User Information List” is located at http://<SiteCollectionUrl>/_catalogs/users/detail.aspx:

SNAGHTMLa064e71

By looking at this list you can see that several key pieces of information are stored here – unfortunately, when you change this information in Active Directory the information stored here is not updated (even after running a full or incremental import via UPS). To complicate matters there is no way to edit the information via the browser, thus the need for a PowerShell script. If you click the user’s name you’ll see the additional properties, including an “Edit Item” option, however, the edit dialog is simply a read-only display of the username, helpful right?:

SNAGHTMLa089b49

So let’s first consider the scenario that my book addresses and assume that a user had had their name and/or email address changed. To accommodate this scenario we simply use the Set-SPUser cmdlet along with the -SyncFromAD parameter. The following script is taken directly from my book and simply iterates through all Site Collections and calls the Set-SPUser cmdlet for the provided user:

function Sync-SPUser([string]$userName) {
  Get-SPSite -Limit All | foreach {
    $web = $_.RootWeb
    if ($_.WebApplication.UseClaimsAuthentication) {
      $claim = New-SPClaimsPrincipal $userName -IdentityType WindowsSamAccountName
      $user = $web | Get-SPUser -Identity $claim -ErrorAction SilentlyContinue
    } else {
      $user = $web | Get-SPUser -Identity $userName -ErrorAction SilentlyContinue
    }
    if ($user -ne $null) {
      $web | Set-SPUser -Identity $user -SyncFromAD
    }
    $web.Dispose()
    $_.Dispose()
  }
}

 

Before I make any changes to demonstrate this script and the modifications we’ll make to it, let’s first see how my user is currently set in the Site Collection:

image

And as shown in the People Picker:

SNAGHTMLa14e91b

Note the “Name”/”Display Name”, “Work e-mail”/”E-Mail”, and “Title” fields.

Now I’ll change these values in Active Directory (make the “p” in my last name capitalized, change the title, and set the email) and then run the script (I saved the script as Sync-SPUser.ps1):

SNAGHTMLa17239b

(Note that lowercase “p” is the correct spelling for my name, just in case you were wondering Smile). Now if we look at the user details in the Site Collection and the People Picker we should see the following:

image

SNAGHTMLa1a344d

Notice that the the “Name” / “Display Name” and “Work e-mail” / “E-Mail” fields were updated but not the “Title” field. This is because the Set-SPUser cmdlet and -SyncFromAD parameter only updates these two fields. So how do you update the remaining fields? We simply need to add some code to our function which will grab the SPListItem corresponding to the user from the hidden “User Information List” and then update the corresponding fields manually. The following modified script does this for the “Title” field (note that I’ve changed the function signature to take the title in as a parameter):

function Sync-SPUser([string]$userName, [string]$title) {
  Get-SPSite -Limit All | foreach {
    $web = $_.RootWeb
    if ($_.WebApplication.UseClaimsAuthentication) {
      $claim = New-SPClaimsPrincipal $userName -IdentityType WindowsSamAccountName
      $user = $web | Get-SPUser -Identity $claim -ErrorAction SilentlyContinue
    } else {
      $user = $web | Get-SPUser -Identity $userName -ErrorAction SilentlyContinue
    }
    if ($user -ne $null) {
      $web | Set-SPUser -Identity $user -SyncFromAD
      
      $list = $web.Lists["User Information List"]
      $query = New-Object Microsoft.SharePoint.SPQuery
      $query.Query = "<Where><Eq><FieldRef Name='Name' /><Value Type='Text'>$userName</Value></Eq></Where>"
      foreach ($item in $list.GetItems($query)) {
        $item["JobTitle"] = $title
        $item.SystemUpdate()
      }
    }
    $web.Dispose()
    $_.Dispose()
  }
}

The changes to the original function have been highlighted. Note that the internal field name for the “Title” field is “JobTitle” and that is what we are using to set the Title. Now if we run this modified script we should see the Title field updated:

SNAGHTMLa21bc70

SNAGHTMLa236af7

Okay, so what about the other fields (Department, Mobile Number, etc.)? You can see what fields are available to edit by running the following:

SNAGHTMLa265249

In the preceding example I’m grabbing a specific item (in this case the item corresponding to my user) so that I can see the internal field names in context with the data stored by the field – this helps to make sure that I grab the correct field name (i.e., “JobTitle” vs. “Title”). Now you can just add additional fields to update right before the call to SystemUpdate() – simply follow the pattern established for the title field.

So, add this guy to your script library and you’ll be good to go next time someone changes their name, email, or job title.

-Gary

19Nov/110

SharePoint Saturday Denver 2011 Slide Decks

Last weekend I had a great time at SharePoint Friday/Saturday Denver 2011 – all the folks who helped organize the event did a fantastic job (and I have to say, it was nice this year not being one of the organizers as I was able to just enjoy the event for once which makes me appreciate their efforts even more). So, at the event I presented two talks, one on PowerShell (of course) and the other was the presentation I gave at the SharePoint Conference in Anaheim last month. The PowerShell talk was a new talk that I’d never done before and it was basically just a bunch of random tips that I’ve found useful over the years. My apologies to those who attended my sessions last weekend for not making these available sooner but I figure better late than never. Anyways, here’s the links to the decks – enjoy!

20Aug/116

Resetting SharePoint 2010 Themes – Part 2, the Reset-SPTheme cmdlet

Yesterday I threw up a quick post showing how to reset a SharePoint 2010 theme using a reasonably simple Windows PowerShell script. In that post I promised that I’d convert the script to a cmdlet and make it part of my downloadable extensions. Well, as promised I’ve updated my extensions so that they now include a Reset-SPTheme cmdlet. I added on minor enhancement over the previously shown script in that I allow you to pass in either an SPSite or an SPWeb object and by default it will not force all child webs to inherit from the relevant SPWeb object. This way, if you have a child Site with it’s own theme it won’t wipe out that theme. If you have multiple Sites with a custom theme setting within a Site Collection then you’ll want to provide the -Site parameter and pass in an SPSite reference – this will result in all Sites with custom themes within the Site Collection to be reset. If you only wish to reset a single Site then use the -Web parameter and pass in a SPWeb reference.

Here’s the full help for the Reset-SPTheme cmdlet:

NAME
    Reset-SPTheme

SYNOPSIS
    Resets a theme by applying all user specified theme configuration settings to the original source files. This is particularly helpful when the original source files have changed to a Feature upgrade.

SYNTAX
    Reset-SPTheme [-Web] <SPWebPipeBind> [-SetSubWebsToInherit <SwitchParameter>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]

    Reset-SPTheme [-Site] <SPSitePipeBind> [-SetSubWebsToInherit <SwitchParameter>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]

DESCRIPTION
    Resets a theme by applying all user specified theme configuration settings to the original source files. This is particularly helpful when the original source files have changed to a Feature upgrade.

    Copyright 2011 Falchion Consulting, LLC
    > For more information on this cmdlet and others:
    >
http://blog.falchionconsulting.com/
    > Use of this cmdlet is at your own risk.
    > Gary Lapointe assumes no liability.

PARAMETERS
    -Web <SPWebPipeBind>
        Specifies the URL or GUID of the Web containing the theme to reset.

        The type must be a valid GUID, in the form 12345678-90ab-cdef-1234-567890bcdefgh; a valid name of Microsoft SharePoint Foundation 2010 Web site (for example, MySPSite1); or an instance of a valid SPWeb object.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false

    -Site <SPSitePipeBind>
        The site containing the theme to reset.

        The type must be a valid GUID, in the form 12345678-90ab-cdef-1234-567890bcdefgh; a valid URL, in the form http://server_name; or an instance of a valid SPSite object.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false

    -SetSubWebsToInherit [<SwitchParameter>]
        If specified, all child webs will be reset to inherit the theme of the specified web or root web.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -AssignmentCollection [<SPAssignmentCollection>]
        Manages objects for the purpose of proper disposal. Use of objects, such as SPWeb or SPSite, can use large amounts of memory and use of these objects in Windows PowerShell scripts requires proper memory management. Using the SPAssignment object, you can assign objects to a variable and dispose of the objects after they are needed to free up memory. When SPWeb, SPSite, or SPSiteAdministration objects are used, the objects are automatically disposed of if an assignment collection or the Global parameter is not used.

        When the Global parameter is used, all objects are contained in the global store. If objects are not immediately used, or disposed of by using the Stop-SPAssignment command, an out-of-memory scenario can occur.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        "get-help about_commonparameters".

INPUTS

OUTPUTS

NOTES

        For more information, type "Get-Help Reset-SPTheme -detailed". For technical information, type "Get-Help Reset-SPTheme -full".

    ------------------EXAMPLE 1-----------------------

    PS C:\> Get-SPSite http://server_name | Reset-SPTheme -SetSubWebsToInherit

    This example resets the theme for the site collection http://server_name and resets all child webs to inherit from the root web.

    ------------------EXAMPLE 2-----------------------

    PS C:\> Get-SPWeb http://server_name/sub-web | Reset-SPTheme

    This example resets the theme for the web http://server_name/sub-web.

RELATED LINKS
    Get-SPWeb
    Get-SPSite

 

In the following example I’m resetting the theme(s) for an entire Site Collection. If one any child Sites within the Site Collection have a custom theme then they’ll be updated, not just the root (inheritance will not be changed):

PS C:\> Reset-SPTheme -Site http://example.com

In this next example I’m resetting all child Sites to inherit whatever theme has been specified for the root Site and I’m updating the root Site’s theme with changes to the source files:

PS C:\> Reset-SPTheme -Site http://example.com -SetSubWebsToInherit

For this last example I’m resetting the theme of a specific sub-Site:

PS C:\> Reset-SPTheme -Web http://example.com

As you can see, this is pretty easy to use and, if you’re deploying your branding via Features and you have theme support then a cmdlet like this can be quite critical when you need to push out updates to that brand.

19Aug/112

Resetting SharePoint 2010 Themes

UPDATE 8/20/2011: I’ve reworked this script and included it as part of my SharePoint 2010 cmdlet downloads. See “Resetting SharePoint 2010 Themes – Part 2, the Reset-SPTheme cmdlet” for details.

One of my current clients is a local school district here in Denver and we (Aptillon) have recently helped them release a new public facing site for the main district office as well as all the schools in the district. The district has chosen a somewhat fixed brand which has had full theming support added so that the individual schools can adjust the color scheme to match the school’s colors. The master page, page layouts, CSS files, and associated images were all deployed to the Farm using various Solution Packages (WSPs). This allows us to make corrections/additions to the brand related files and quickly deploy them to the Farm, thereby updating the district and school sites quite easily. The problem, however, is that the way theming works within SharePoint 2010 is that it makes a copy of all the CSS and image files and stores them in the /_catalogs/theme/Themed/{THEMEID} folder, as shown in the following figure:

ThemedFolder

Whenever you apply a theme it will copy all the necessary CSS and image files to a new folder in the Themed folder. This means that the site is no longer basing its look and feel off of the Feature deployed files. So if a school has gone in and customized their site to use themes then any updates that we push out to the style sheets and images will be ignored. So I needed a way to effectively “reset” the applied theme after we push out an update to our branding solution. By reset I mean preserve the various color and font settings but re-apply them against the Feature deployed files.

I did some digging with my favorite tool, Reflector, and found that the out of the box theme settings page just uses the Microsoft.SharePoint.Utilities.ThmxTheme class to manipulate the themes. So, after a little experimenting I ended up with some code which will regenerate the current theme using all the source files and the user provided theme settings:

#NOTE: Run in a seperate console instance each time otherwise the changes won't get applied
function Reset-SPTheme([Microsoft.SharePoint.PowerShell.SPSitePipeBind]$spSite) {
    $site = $spSite.Read()
    try {
        # Store some variables for later use
        $tempFolderName = "TEMP"
        $themedFolderName = "$($site.ServerRelativeUrl)/_catalogs/theme/Themed"
        $themesUrlForWeb = [Microsoft.SharePoint.Utilities.ThmxTheme]::GetThemeUrlForWeb($site.RootWeb)
        Write-Host "Old Theme URL: $themesUrlForWeb"
        
        # Open the existing theme
        $webThmxTheme = [Microsoft.SharePoint.Utilities.ThmxTheme]::Open($site, $themesUrlForWeb)
        
        # Generate a new theme using the settings defined for the existing theme
        # (this will generate a temporary theme folder that we'll need to delete)
        $webThmxTheme.GenerateThemedStyles($true, $site.RootWeb, $tempFolderName)
        
        # Apply the newly generated theme to the root web
        [Microsoft.SharePoint.Utilities.ThmxTheme]::SetThemeUrlForWeb($site.RootWeb, "$themedFolderName/$tempFolderName/theme.thmx", $true)
        
        # Delete the TEMP folder created earlier
        $site.RootWeb.GetFolder("$themedFolderName/$tempFolderName").Delete()
        
        # Reset the theme URL just in case it has changed (sometimes it will)
        $site.Dispose()
        $site = $spSite.Read()
        $themesUrlForWeb = [Microsoft.SharePoint.Utilities.ThmxTheme]::GetThemeUrlForWeb($site.RootWeb)
        Write-Host "New Theme URL: $themesUrlForWeb"

        # Set all child webs to inherit.
        if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($site.RootWeb)) {
            $pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($site.RootWeb)
            $pubWeb.ThemedCssFolderUrl.SetValue($pubWeb.Web.ThemedCssFolderUrl, $true)
        } else {
            foreach ($web in $site.AllWebs) {
                if ($web.isRootWeb) { continue }
                Write-Host "Setting theme for $($web.ServerRelativeUrl)..."
                try {
                    [Microsoft.SharePoint.Utilities.ThmxTheme]::SetThemeUrlForWeb($web, $themesUrlForWeb)
                } finally {
                    $web.Dispose()
                }
            }   
        }
    } finally {
        if ($site -ne $null) { $site.Dispose() }
    }
}

I saved this to a file named Reset-SPTheme so I can then call the function like so:

. c:\Scripts\Reset-SPTheme.ps1
Reset-SPTheme "http://example.com/"

One odd thing I found, however, is that every time I run this it must be run in a new console instance; otherwise the changes are not picked up (this is basically a combination of a threading and caching issue within the code when executed from a PowerShell context). So remember, start a new PowerShell console every time you need to run this script (yeah, I wasted a couple of hours banging my head against the wall trying to figure that little nugget out).

BTW: I intend to add this to my cmdlet extensions as I believe it will be something I’ll need often so look for an updated build to come out this weekend.

7Jul/112

SharePoint 2010 SP1 Public API Changes

I recently published a post detailing the SharePoint 2010 SP1 PowerShell changes and, in that post, I mentioned that I was probably going to detail the API changes. Well, here they are. Note that the list below is not a comprehensive one in that I’m not showing every assembly (I do have the changes for every assembly but frankly I just got tired of translating the results into a readable format so I kept it to the more prominent assemblies (or at least the ones that I just happened to have done at the time)). In reviewing the list you see that there’s honestly not a whole lot of noteworthy changes, but that’s okay as part of my reasoning for doing this was to discover whether there were any (don’t get me wrong, there are some, in fact, for me there’s 1 very big one that made this whole exercise worth it – I’ll let you figure out which one that is). If you find any I missed please add a comment so that others can see it as well.

  • Microsoft.SharePoint.dll
    • Microsoft.SharePoint.SPRecycleBinItemType
      • New enum value:
        • Web
    • Microsoft.SharePoint.SPWeb
      • New method:
        • public void Recycle()
    • Microsoft.SharePoint.Strings
      • New constants:
        • public const string CannotRecycleRootWeb
        • public const string HealthRule_Explanation_BcsShimsAreEnabled
        • public const string HealthRule_Remedy_BcsShimsAreEnabled
        • public const string RecycleBinWebMissingContainerError
        • public const string SPStorageMetricsProcessingJobDescription
        • public const string SPUsageUserCodeRequestsDescription
        • public const string SPUsageUserCodeRequestsMonitoredDataDescription
        • public const string SiteAlreadyExists
        • public const string StorageMetricsDBObjectsNotFound
        • public const string StorageMetricsFreshnessWarning
        • public const string StorageMetricsNotAvailable
        • public const string TimerJobTitleStorageMetricsProcessing
    • Microsoft.SharePoint.Administration.SPAce<T>
      • New properties:
        • public Byte[] BinaryId() { get; }
        • public Microsoft.SharePoint.Administration.SPIdentifierType BinaryIdType() { get; }
    • Microsoft.SharePoint.Administration.SPAcl<T>
      • New method:
        • public SPAce<T> Add(string principalName, string displayName, SPIdentifierType identifierType, byte[] identifier, T grantRightsMask, T denyRightsMask)
    • Microsoft.SharePoint.Administration.SPContentDatabase
      • New methods:
        • public Microsoft.SharePoint.Administration.SPDeletedSite GetDeletedSite(System.Guid id)
        • public void Move(SPContentDatabase destinationDb, List<SPSite> sitesToMove, Dictionary<string, string> rbsProviderMap, out Dictionary<SPSite, string> failedSites)
    • Microsoft.SharePoint.Administration.SPContentDatabaseCollection
      • New method:
        • public SPContentDatabase Add(string strDatabaseServer, string strDatabaseName, string strDatabaseUsername, string strDatabasePassword, int warningSiteCount, int maximumSiteCount, int status, bool flushChangeLog, bool changeSyncKnowledge)
    • Microsoft.SharePoint.Administration.SPDatabase
      • New method:
        • public void ChangeDatabaseInstance(string databaseServiceInstance)
    • Microsoft.SharePoint.Administration.SPIncomingEmailService
      • New property:
        • public int RetryDeliveryInterval { get; set; }
    • Microsoft.SharePoint.Administration.SPPolicy
      • New method:
        • protected Void OnDeserialization()
    • Microsoft.SharePoint.Administration.SPSiteLookupProvider
      • Changed method (breaking change!)
        • public Void RenameHostHeaderSite(Guid siteId, string newHostHeader) => public Void RenameHostHeaderSite(Guid siteId, Uri newHostHeaderSiteUri)
    • Microsoft.SharePoint.Administration.SPUsageApplication
      • New property:
        • public int UsageInsertionTimeOut { get; set; }
    • Microsoft.SharePoint.Administration.SPUserCodeExecutionTier
      • New property:
        • public int PriorityPerProcess { get; set; }
    • Microsoft.SharePoint.Administration.SPWebApplication
      • New properties:
        • public int StorageMetricsProcessingDuration { get; set; }
        • public uint MaxDiscussionBoardItemsForSiteDataFolderQuery { get; set; }
        • public uint? UserDefinedWorkflowMaximumComplexity { get; set; }
      • New methods:
        • public SPDeletedSiteCollection GetDeletedSites()
        • public SPDeletedSiteCollection GetDeletedSites(SPDeletedSiteQuery query)
        • public SPDeletedSiteCollection GetDeletedSites(Guid siteId)
        • public SPDeletedSiteCollection GetDeletedSites(string sitePath)
        • public void MigrateUsers(IMigrateUserCallback callback)
    • Microsoft.SharePoint.Administration.SPWebService
      • New properties:
        • public int ImagingDownloadSizeLimit { get; set; }
        • public bool EnableHostHeaderSiteBasedSchemeSelection { get; set; }
    • Microsoft.SharePoint.Administration.Claims.SPActiveDirectoryClaimProvider
      • New method:
        • protected override void FillDefaultLocalizedDisplayName(CultureInfo culture, out string localizedName)
    • Microsoft.SharePoint.Administration.Claims.SPAllUserClaimProvider
      • New method:
        • protected override void FillDefaultLocalizedDisplayName(CultureInfo culture, out string localizedName)
    • Microsoft.SharePoint.Administration.Claims.SPClaimHierarchyProvider
      • New methods:
        • protected override void FillDefaultLocalizedDisplayName(CultureInfo culture, out string localizedName)
        • public string GetLocalizedDisplayName()
    • Microsoft.SharePoint.Administration.Claims.SPClaimProvider
      • New property:
        • public virtual bool SupportsUserKey { get; }
      • New methods:
        • protected override void FillDefaultLocalizedDisplayName(CultureInfo culture, out string localizedName)
        • public string GetLocalizedDisplayName()
        • public SPClaim UserKeyForEntity(SPClaim entity)
        • public virtual string GetClaimTypeForUserKey()
        • protected virtual SPClaim GetUserKeyForEntity(SPClaim entity)
    • Microsoft.SharePoint.Administration.Claims.SPClaimProviderDefinition
      • New property:
        • public bool IsVisible { get; set; }
    • Microsoft.SharePoint.Administration.Claims.SPClaimProviderOperationOptions
      • New enum value:
        • OverrideVisibleFlag
    • Microsoft.SharePoint.Administration.Claims.SPFormsClaimProvider
      • New method:
        • protected override void FillDefaultLocalizedDisplayName(CultureInfo culture, out string localizedName)
    • Microsoft.SharePoint.Administration.Claims.SPSystemClaimProvider
      • New method:
        • protected override void FillDefaultLocalizedDisplayName(CultureInfo culture, out string localizedName)
    • Microsoft.SharePoint.BusinessData.Administration.LobSystem
      • New static method:
        • public static LobSystem MergeXml(string xml, out string[] errors, PackageContents packageContents, AdministrationMetadataCatalog metadataCatalog, string settingId)
    • Microsoft.SharePoint.BusinessData.Administration.TypeDescriptor
      • New static method:
        • public static TypeDescriptor MergeXml(string xml, out string[] errors, PackageContents packageContents, Parameter parameter, TypeDescriptor parent, string settingId)
    • Microsoft.SharePoint.BusinessData.SharedService.BdcServiceApplicationProxy
      • New methods:
        • public bool IsSystemTypeEnabled(SystemType systemType)
        • public void EnableSystemType(SystemType systemType, bool value)
    • Microsoft.SharePoint.JSGrid.GridSerializer
      • New method:
        • public void ApplyPostViewIncrementalInsertsAndDeletes(IEnumerable changes, Func> fnGetDefaultValuesForPostViewInserts)
    • Microsoft.SharePoint.JSGrid.HierarchyNode
      • New property:
        • public HierarchyNode Parent { get; set; }
    • Microsoft.SharePoint.Utilities.SPUtility
      • New static method:
        • public static Stream ExecuteCellStorageBinaryRequest(SPFile spfile, Stream request, bool coalesce, ref Guid partitionID, string userName, bool coauthVersioning, string etagMatching, bool fExpectNoFileExists, string contentChangeUnit, string clientFileID, string bypassSchemaID, long nLockType, string lockID, long nTimeout, bool createParentFolder, out string etagReturn, out bool allRequestSucceeded, out int coalesceHRESULT, out string coalesceErrorMessage, out bool containHotboxData, out bool haveOnlyDemotionChanges, ref int binaryReqCountQuota)
    • Microsoft.SharePoint.WebPartPages.ListFormWebPart
      • New method:
        • public bool ShouldSerializeTemplateName()
    • New classes:
      • Microsoft.SharePoint.Administration.SPDeletedSite
      • Microsoft.SharePoint.Administration.SPDeletedSiteCollection
      • Microsoft.SharePoint.Administration.SPDeletedSiteLookupInfo
      • Microsoft.SharePoint.Administration.SPDeletedSiteQuery
      • Microsoft.SharePoint.Administration.SPUsageUserCodeRequests
      • Microsoft.SharePoint.Administration.SPUsageUserCodeRequestsEntry
      • Microsoft.SharePoint.Administration.SPUsageUserCodeRequestsMonitoredData
      • Microsoft.SharePoint.Administration.SPUsageUserCodeRequestsMonitoredDataEntry
      • Microsoft.SharePoint.Administration.Health.SPHealthAnalysisRuleInstance
      • Microsoft.SharePoint.WebControls.IEVersionMetaTag
    • New enum types:
      • Microsoft.SharePoint.Administration.SPIdentifierType
    • New interfaces:
      • Microsoft.SharePoint.Administration.IMigrateUserCallback
      • Microsoft.SharePoint.Administration.ISPSiteLookupProviderRecycleBin
  • Microsoft.SharePoint.Publishing.dll
    • Microsoft.SharePoint.Publishing.Internal.CodeBehind
      • New property:
        • protected bool IsCurrentUserSiteAdmin { get; }
    • Microsoft.SharePoint.Publishing.WebControls.SpellCheckV4Action
      • New method:
        • protected bool ShouldRenderWithoutTabs()
    • Microsoft.SharePoint.Publishing.WebControls.EditingMenuActions.ConsoleAction
      • New method:
        • protected bool ShouldRenderWithoutTabs()
  • Microsoft.SharePoint.Taxonomy.dll
    • Microsoft.SharePoint.Taxonomy.TermStore
      • New method:
        • public Group GetSiteCollectionGroup(SPSite currentSite)
  • Microsoft.SharePoint.Portal.dll
    • Microsoft.Office.Server.UserProfiles.UserProfileService
      • New methods:
        • public void AddLeader(string accountName)
        • public Leader[] GetLeaders()
        • public void RemoveLeader(string accountName)
  • Microsoft.Office.Server.UserProfiles.dll
    • Microsoft.Office.Server.SocialData.PluggableSocialSecurityTrimmerManager
      • New methods:
        • public static string[] GetUrlFoldersRequiringTrim(SPServiceContext serviceContext)
        • public static string[] GetUrlFoldersToAlwaysAllow(SPServiceContext serviceContext)
        • public static void SetTrimmerSettings(SPServiceContext serviceContext, bool enableTrimming)
        • public static void SetTrimmerSettings(SPServiceContext serviceContext, string[] urlFoldersRequiringTrim, string[] urlFoldersToAlwaysAllow)
    • Microsoft.Office.Server.UserProfiles.BusinessDataCatalogConnection
      • New method:
        • public void Delete()
    • Microsoft.Office.Server.UserProfiles.ConnectionManager
      • New method:
        • public DirectoryServiceConnection AddActiveDirectoryConnection(ConnectionType type, string displayName, string server, bool useSSL, string accountDomain, string accountUsername, SecureString accountPassword, List<DirectoryServiceNamingContext> namingContexts, string spsClaimProviderTypeValue, string spsClaimProviderIdValue, string adClaimIDMapAttribute)
    • Microsoft.Office.Server.UserProfiles.UserProfileManager
      • New methods:
        • public void AddLeader(string accountName)
        • public Leader[] GetLeaders()
        • public void RemoveLeader(string accountName)
    • New classes:
      • Microsoft.Office.Server.UserProfiles.Leader
  • Microsoft.Office.Server.Search.dll
    • Microsoft.Office.Server.Search.Administration.CrawlTopologyState
      • New enum values:
        • ActiveToBeRemoved
        • DeactivatingToBeRemoved
    • Microsoft.Office.Server.Search.Administration.SearchServiceApplication
      • New property:
        • public uint CrawlLogCleanupIntervalInDays { get; set; }
    • Microsoft.Office.Server.Search.Administration.SearchServiceApplicationProxy
      • New property:
        • public LocationConfigurationCollection LocationConfigurations { get; }
    • Microsoft.Office.Server.Search.Query.QueryInfo
      • New property:
        • public string CorrelationId { get; set; }
    • Microsoft.Office.Server.Search.Query.QueryManager
      • New method:
        • public System.Xml.XmlDocument GetResults()
  • Microsoft.SharePoint.PowerShell.dll
    • New classes:
      • Microsoft.SharePoint.PowerShell.SPDeletedSitePipeBind
      • Microsoft.SharePoint.PowerShell.SPHealthAnalysisRuleInstancePipeBind
28Jun/110

SharePoint Server 2010 Service Pack 1 PowerShell Changes

As most people know by now, Service Pack 1 for SharePoint 2010 was released to the public today. There’s already been a lot of hype over some of the new capabilities such as the site recycle bin and some folks have documented/demonstrated some of the new PowerShell cmdlets that are available to manage this new feature; but what about all the other new and changed PowerShell cmdlets – there’s a bunch! So, let’s take a look at what is new, and what has changed.

We’ll start with the new stuff – here’s a quick bulleted list of all the new cmdlets:

  • Add-SPProfileLeader
  • Get-SPProfileLeader
  • Remove-SPProfileLeader
  • Remove-SPProfileSyncConnection
  • Add-SPProfileSyncConnection
  • Disable-SPHealthAnalysisRule
  • Enable-SPHealthAnalysisRule
  • Get-SPHealthAnalysisRule
  • Get-SPDeletedSite
  • Remove-SPDeletedSite
  • Restore-SPDeletedSite
  • Move-SPSocialComments

I haven’t had a chance to try any of these out but I think there’s some cool new functionality here beyond just the site recycle bin. There’s absolutely no documentation for any of these but some of them are fairly straightforward based on the names. For instance, the Add-SPProfileSyncConnection cmdlet (and equivalent Remove cmdlet) are obviously for managing the synchronization connections for UPS. This was a big whole in RTM when it came to doing an end-to-end scripted installation as there was no practical way to add a synchronization connection using PowerShell. The health analysis rule cmdlets are also pretty obvious and, again, this goes a long way towards enabling administrators to script a deployment and enable or disable rules consistently across Farms.

I think the Move-SPSocialComments also has a lot of potential; if it does what I’m guessing it does then this could potentially solve the issue where comments and tags are stored with the absolute URL of the item that has been tagged/commented on – I’m *guessing* that running this command will effectively retarget those items, which is great in situations in which you’ve moved a list or site.

As for the *-SPProfileLeader cmdlets – I have no idea what those guys do so I’ll have to revisit them when I learn more. (The *-SPDeletedSite cmdlets have already been covered quite a bit by others so I’ll forgo any further discussion here).

Alright, so that’s the new stuff – what about the stuff that’s changed? Here’s a quick list with the changes:

  • Mount-SPContentDatabase
    • New Switch Parameter: ChangeSyncKnowledge (I’ve no idea what this does – it’s not yet documented)
  • New-SPContentDatabase
    • New Switch Parameter: ChangeSyncKnowledge (again, not yet documented)
  • Move-SPSite
    • New Parameter: RbsProviderMapping <Hashtable>
      • From TechNet: “Used to move an RBS-enabled site collection from one RBS-enabled content database to another RBS-enabled content database without moving the underlying BLOB content. If the content database has more than one RBS provider associated with it, you must specify all providers. The same providers must be enabled on the target content database and the source content database.”
  • New-SPPerformancePointServiceApplication
    • New Parameter: AnalyticResultCacheMinimumHitCount <Int32> (not yet documented but I think it’s fairly obvious what it does)
    • New Parameters: DatabaseServer <string>, DatabaseName <string>, DatabaseFailoverServer <string>, DatabaseSQLAuthenticationCredential <PSCredential>
      • Can I get a hurray for this! This was the only Service Application that didn’t allow us to set the database information when we created it so we were left with this nasty GUID in the name. Hurray! We can finally get rid of the last database GUID! Woohoo!
  • Set-SPPerformancePointServiceApplication
    • New Parameter: AnalyticResultCacheMinimumHitCount <Int32> (not yet documented but I think it’s fairly obvious what it does)
    • New Parameters: DatabaseServer <string>, DatabaseName <string>, DatabaseFailoverServer <string>, DatabaseSQLAuthenticationCredential <PSCredential>, DatabaseUseWindowsAuthentication
  • Remove-SPWeb
    • New Switch Parameter: Recycle
      • That’s right, you can in fact cause an SPWeb to go to the new recycle bin by simply providing this switch parameter.
  • Update-SPProfilePhotoStore (I think this one is a bit of mess and may need a CU or two but I could just be reading the code wrong – it is kind of late right now)
    • Update 6/29/2011: From Spence Harbar: “Update-SPProfilePhotoStore change is to address common bug/issue with resize and is for upgrade scenarios”
    • New Switch Parameter: CreateThumbnailsForImportedPhotos
      • I’m not entirely sure what this is supposed to do as thumbnails were created previously and will be created without this; however, I should note that they coded this wrong so both of the following syntaxes have the same affect as they are simply checking that the bool? type has a value and not what the value is:
        • -CreateThumbnailsForImportedPhotos $true
        • -CreateThumbnailsForImportedPhotos $false
    • New Switch Parameter: NoDelete
      • I’m not 100% on this but I believe that the original behavior of this cmdlet was to copy the image and not actually move it (despite the name); they appear to have changed the behavior to delete the original images after the copy but you can preserve the original behavior by simply adding this switch parameter. However, this is *only* true when the CreateThumbnailsForImportedPhotos parameter is provided (in my opinion this is a bug – it should be irrelevant if that parameter is provided).

Well, that’s all I’ve been able to discover with the core SharePoint Server 2010 cmdlets – I may update this post to account for the Office Web Applications but I don’t currently have that installed on the server in which I just installed SP1 so it may take me a bit to do that analysis. At some point I may write something up to inspect the public classes and their members and do a similar post for the developers out there who want to know what API changes have occurred so keep an eye out for that.

So I think it’s pretty cool that we’ve got some improvements in the PowerShell cmdlets, especially in some of those that have frustrated me when it comes to automated deployments; the real frustrating thing, however, is that some of my just released book content is already out of date! Ugh! Maybe there’ll be a second edition Smile

Happy PowerShelling!

-Gary

26Jun/114

Getting (and taking ownership of) Checked Out Files using Windows PowerShell

Often when I’m working on a project I need to generate a list of all checked out files and provide that to my client just prior to release to production. Sometimes the client will manually inspect each of them and act as they see fit and other times they’ll ask me to just batch publish all of them (for which I use my Publish-SPListItems cmdlet). So, how do I generate the report for the client? It’s actually pretty easy using PowerShell and a couple of quick loops. Here’s an example that loops through every Site Collection in the Farm and generates a nice report:

function Get-CheckedOutFiles() {
    foreach ($web in (Get-SPSite -Limit All | Get-SPWeb -Limit All)) {
        Write-Host "Processing Web: $($web.Url)..."
        foreach ($list in ($web.Lists | ? {$_ -is [Microsoft.SharePoint.SPDocumentLibrary]})) {
            Write-Host "`tProcessing List: $($list.RootFolder.ServerRelativeUrl)..."
            foreach ($item in $list.CheckedOutFiles) {
                if (!$item.Url.EndsWith(".aspx")) { continue }
                $hash = @{
                    "URL"=$web.Site.MakeFullUrl("$($web.ServerRelativeUrl.TrimEnd('/'))/$($item.Url)");
                    "CheckedOutBy"=$item.CheckedOutBy;
                    "CheckedOutByEmail"=$item.CheckedOutByEmail
                }
                New-Object PSObject -Property $hash
            }
            foreach ($item in $list.Items) {
                if ($item.File.CheckOutStatus -ne "None") {
                    if (($list.CheckedOutFiles | where {$_.ListItemId -eq $item.ID}) -ne $null) { continue }
                    $hash = @{
                        "URL"=$web.Site.MakeFullUrl("$($web.ServerRelativeUrl.TrimEnd('/'))/$($item.Url)");
                        "CheckedOutBy"=$item.File.CheckedOutByUser;
                        "CheckedOutByEmail"=$item.File.CheckedOutByUser.Email
                    }
                    New-Object PSObject -Property $hash
                }
            }
        }
        $web.Dispose()
    }
}
Get-CheckedOutFiles | Out-GridView

Running the above will generate a fairly nice report with URLs and usernames and whatnot; you could also use the Export-Csv cmdlet to dump the results to a CSV file that you can then hand off to your end-users. One cool thing to point out about this is that it will also show you files that you normally can’t see – that is files that have been created by other users but have never had a check in. This is actually pretty cool and I stumbled upon this when trying to fine tune my Publish-SPListItems cmdlet. You see, if the file has never been checked in then iterating through the SPListItemCollection object will not reveal the item (or file I should say); this meant that my cmdlet, as it was previously written, was missing a bunch of files. So to work around this all I had to do was add an additional loop to iterate over the collection returned by the SPDocumentLibrary’s CheckedOutFiles property. For each SPCheckedOutFile object in that collection I then call TakeOverCheckOut() to grab the checked out file so that I can then publish.

I use this enough that I decided to turn it into a cmdlet that is now part of my custom extensions. Like the above script, I return back a custom object that contains the full URLs and other  useful information (such as the List, Site, and Site Collection identifiers). I also exposed a TakeOverCheckOut() and Delete() method which simply calls Microsoft’s implementation of those methods.

I called this cmdlet Get-SPCheckedOutFiles (note that I’d previously released this cmdlet under the name Get-SPFilesCheckedOut but have since reworked and renamed that original implementation).

Here’s the full help for the cmdlet:

PS C:\Users\spadmin> help Get-SPCheckedOutFiles -full

NAME
Get-SPCheckedOutFiles

SYNOPSIS
Retrieves check out details for a given List, Web, or Site.

SYNTAX
Get-SPCheckedOutFiles [-Site] <SPSitePipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]

Get-SPCheckedOutFiles [-Web] <SPWebPipeBind> [-ExcludeChildWebs <SwitchParameter>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]

Get-SPCheckedOutFiles [[-Web] <SPWebPipeBind>] [-List] <SPListPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]


DESCRIPTION
Retrieves check out details for a given List, Web, or Site.

Copyright 2010 Falchion Consulting, LLC
> For more information on this cmdlet and others:
>
http://blog.falchionconsulting.com/
> Use of this cmdlet is at your own risk.
> Gary Lapointe assumes no liability.

PARAMETERS
-Site <SPSitePipeBind>
Specifies the URL or GUID of the Site to inspect.

The type must be a valid GUID, in the form 12345678-90ab-cdef-1234-567890bcdefgh; a valid URL, in the form
http://server_name; or an instance of a valid SPSite object.

Required? true
Position? 1
Default value
Accept pipeline input? true (ByValue, ByPropertyName)
Accept wildcard characters? false

-Web <SPWebPipeBind>
Specifies the URL or GUID of the Web to inspect.

The type must be a valid GUID, in the form 12345678-90ab-cdef-1234-567890bcdefgh; a valid URL, in the form
http://server_name; or an instance of a valid SPWeb object.

Required? true
Position? 1
Default value
Accept pipeline input? true (ByValue, ByPropertyName)
Accept wildcard characters? false

-List <SPListPipeBind>
The list whose checked out files are to be returned.

The value must be a valid URL in the form
http://server_name/lists/listname or /lists/listname. If a server relative URL is provided then the Web parameter must be provided.

Required? true
Position? 1
Default value
Accept pipeline input? true (ByValue, ByPropertyName)
Accept wildcard characters? false

-ExcludeChildWebs [<SwitchParameter>]
Excludes all child sites and only considers the specified site.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-AssignmentCollection [<SPAssignmentCollection>]
Manages objects for the purpose of proper disposal. Use of objects, such as SPWeb or SPSite, can use large amounts of memory and use of these objects in Windows PowerShell scripts requires proper memory management. Using the SPAssignment object, you can assign objects to a variable and dispose of the objects after they are needed to free up memory. When SPWeb, SPSite, or SPSiteAdministration objects are used, the objects are automatically disposed of if an assignment collection or the Global parameter is not used.

When the Global parameter is used, all objects are contained in the global store. If objects are not immediately used, or disposed of by using the Stop-SPAssignment command, an out-of-memory scenario can occur.

Required? false
Position? named
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters? false

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type, "get-help about_commonparameters".

INPUTS

OUTPUTS

NOTES

For more information, type "Get-Help Get-SPCheckedOutFiles -detailed". For technical information, type "Get-Help Get-SPCheckedOutFiles -full".

------------------EXAMPLE------------------

PS C:\> Get-SPCheckedOutFiles -Site "
http://server_name/"


This example outputs a list of files that are checked out for the given Site Collection


RELATED LINKS
Get-SPFile

 

In the following example I’m retrieving pages from the root Pages library that are checked out:

image

In this example I am running the cmdlet as the aptillon\spadmin user and I’m now able to see the checkout by the user aptillon\glapointe. I ran the cmdlet twice so you could see the default tabular view as well as the more detailed view. Again, you could easily use the Export-Csv cmdlet to dump this information to a file that you can provide your end-users.

I hope you find this cmdlet useful – it personally has proven invaluable to me, particularly when working on anonymous access internet sites as end-users are notorious about creating pages and not getting them checked in.

P.S. With this release the Publish-SPListItems cmdlet has been updated to now consider files that don’t have any existing check-ins.