Skip to content

Instantly share code, notes, and snippets.

@joerodgers
Last active April 2, 2021 21:14
Show Gist options
  • Save joerodgers/91e8d70ae477ab84fe41c9395ffdcd82 to your computer and use it in GitHub Desktop.
Save joerodgers/91e8d70ae477ab84fe41c9395ffdcd82 to your computer and use it in GitHub Desktop.
Uses the SPWebConfigModifications class to deploy custom PageParserPath entries to web.config.
Add-PSSnapin Microsoft.SharePoint.PowerShell
function Add-PageParserPath
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SharePoint.Administration.SPWebApplication]$WebApplication,
[Parameter(Mandatory=$true)][string]$VirtualPath,
[Parameter(Mandatory=$false)][bool]$AllowServerSideScript = $false,
[Parameter(Mandatory=$false)][bool]$IncludeSubFolders = $false,
[Parameter(Mandatory=$false)][bool]$AllowUnsafeControls = $false,
[Parameter(Mandatory=$false)][switch]$WhatIf
)
begin
{
$modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification -Property @{
Name = "PageParserPath[@VirtualPath='$VirtualPath']"
Path = "configuration/SharePoint/SafeMode/PageParserPaths"
Value = "<PageParserPath VirtualPath=`"$VirtualPath`" CompilationMode=`"Always`" AllowServerSideScript=`"$AllowServerSideScript`" IncludeSubFolders=`"$IncludeSubFolders`" AllowUnsafeControls=`"$AllowUnsafeControls`" />"
Sequence = 100
Owner = "AllowServerSideScriptPageParserPath: $VirtualPath"
Type = [Microsoft.SharePoint.Administration.SPWebConfigModification+SPWebConfigModificationType]::EnsureChildNode
}
}
process
{
# basic validation
# VirtualPath must start with a /
if( $VirtualPath[0] -ne "/" )
{
throw New-Object ArgumentException("The VirtualPath value must start with a '/'")
}
# any wildcard reference must be the last character of the VirtualPath
if( $VirtualPath.IndexOf("*") -ge 1 -and $VirtualPath.IndexOf("*") -ne $VirtualPath.Length-1 )
{
throw New-Object ArgumentException("An asterisk can only appear as the last character of the VirtualPath value")
}
$WebApplication | Remove-PageParserPath -VirtualPath $VirtualPath -WhatIf:$WhatIf.IsPresent
if( $WhatIf )
{
Write-Output "Would have added new web config modification: $($modification.Value) to $($WebApplication.Url)"
}
else
{
Write-Verbose "Saving new modification: $($modification.Value) to $($WebApplication.Url)"
$WebApplication.WebConfigModifications.Add($modification)
$WebApplication.Update()
$WebApplication.WebService.ApplyWebConfigModifications()
}
}
end
{
}
}
function Remove-PageParserPath
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SharePoint.Administration.SPWebApplication]$WebApplication,
[Parameter(Mandatory=$true)][string]$VirtualPath,
[Parameter(Mandatory=$false)][switch]$WhatIf
)
begin
{
}
process
{
$update = $false
for( $i = $WebApplication.WebConfigModifications.Count-1; $i -ge 0; $i-- )
{
try
{
$existingVirtualPathXml = [xml]$WebApplication.WebConfigModifications[$i].Value
if( $existingVirtualPathXml.PageParserPath | ? VirtualPath -eq $VirtualPath )
{
if( $WhatIf.IsPresent )
{
Write-Output "Would have removed existing modification $($WebApplication.WebConfigModifications[$i].Value)"
}
else
{
Write-Verbose "Removing existing modification $($WebApplication.WebConfigModifications[$i].Value)"
$WebApplication.WebConfigModifications.Remove($WebApplication.WebConfigModifications[$i]) | Out-Null
$update = $true
}
}
}
catch
{
# Write-Error $_
}
if( $update )
{
$WebApplication.Update()
$WebApplication.WebService.ApplyWebConfigModifications()
}
}
}
end
{
}
}
# list existing WebConfigModifications
# Get-SPWebApplication | SELECT -ExpandProperty WebConfigModifications
# example how to deploy a new PageParserPath VirtualPath
Get-SPWebApplication | Add-PageParserPath -VirtualPath "/sites/finance/custompage.aspx" -AllowServerSideScript $true -IncludeSubFolders $true -AllowUnsafeControls $false -Verbose -WhatIf
# example how to retract any previously added (via SPWebConfigModifications) PageParserPath entries
Get-SPWebApplication | Remove-PageParserPath -VirtualPath "/sites/finance/custompage.aspx" -Verbose
Add-PSSnapin Microsoft.SharePoint.PowerShell
function Update-DirectFileDependenciesAttribute
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SharePoint.Administration.SPWebApplication]$WebApplication,
[Parameter(Mandatory=$true)][ValidateRange(10,50)][int]$DirectFileDependenciesValue,
[Parameter(Mandatory=$false)][switch]$WhatIf
)
begin
{
$owner = "DirectFileDependencies=`"$DirectFileDependenciesValue`""
$modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification -Property @{
Name = "DirectFileDependencies"
Path = "configuration/SharePoint/SafeMode"
Value = $DirectFileDependenciesValue
Sequence = 101
Owner = $owner
Type = [Microsoft.SharePoint.Administration.SPWebConfigModification+SPWebConfigModificationType]::EnsureAttribute
}
}
process
{
$WebApplication | Remove-DirectFileDependenciesAttribute -DirectFileDependenciesValue $DirectFileDependenciesValue -WhatIf:$WhatIf.IsPresent
if( $WhatIf )
{
Write-Output "Would have added new web config modification: $owner to $($WebApplication.Url)"
}
else
{
Write-Verbose "Saving new modification: $owner to $($WebApplication.Url)"
$WebApplication.WebConfigModifications.Add($modification)
$WebApplication.Update()
$WebApplication.WebService.ApplyWebConfigModifications()
}
}
end
{
}
}
function Remove-DirectFileDependenciesAttribute
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SharePoint.Administration.SPWebApplication]$WebApplication,
[Parameter(Mandatory=$true)][ValidateRange(10,50)][int]$DirectFileDependenciesValue,
[Parameter(Mandatory=$false)][switch]$WhatIf
)
begin
{
$owner = "DirectFileDependencies=`"$DirectFileDependenciesValue`""
}
process
{
$update = $false
for( $i = $WebApplication.WebConfigModifications.Count-1; $i -ge 0; $i-- )
{
try
{
if( $WebApplication.WebConfigModifications[$i].Owner -match "^DirectFileDependencies=" )
{
if( $WhatIf.IsPresent )
{
Write-Output "Would have removed existing modification $($WebApplication.WebConfigModifications[$i].Owner)"
}
else
{
Write-Verbose "Removing existing modification: $($WebApplication.WebConfigModifications[$i].Owner)"
$WebApplication.WebConfigModifications.Remove($WebApplication.WebConfigModifications[$i]) | Out-Null
$update = $true
}
}
}
catch
{
# Write-Error $_
}
if( $update )
{
$WebApplication.Update()
$WebApplication.WebService.ApplyWebConfigModifications()
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
while( $stopwatch.Elapsed.TotalMinutes -le 5 -and (Get-SPTimerjob -Identity "job-webconfig-modification" -Verbose:$false) )
{
Write-Verbose "Waiting for timer job to remove existing Web Config Modifications from all servers."
Start-Sleep -Seconds 5
}
}
}
}
end
{
}
}
# list existing WebConfigModifications
# Get-SPWebApplication | SELECT -ExpandProperty WebConfigModifications
# example how to deploy a new DirectFileDependencies Attribute value. Default is 10
Get-SPWebApplication | Update-DirectFileDependenciesAttribute -DirectFileDependenciesValue 20 -Verbose -WhatIf
# example how to retract the updated DirectFileDependencies Attribute value and return the default of 10
Get-SPWebApplication | Remove-DirectFileDependenciesAttribute -DirectFileDependenciesValue 20 -Verbose -WhatIf
Add-PSSnapin Microsoft.SharePoint.PowerShell
function Update-SafeControlMaxControlValue
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SharePoint.Administration.SPWebApplication]$WebApplication,
[Parameter(Mandatory=$true)][ValidateRange(200,1000)][int]$MaxControlsValue,
[Parameter(Mandatory=$false)][switch]$WhatIf
)
begin
{
$owner = "MaxControls=`"$MaxControlsValue`""
$modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification -Property @{
Name = "MaxControls"
Path = "configuration/SharePoint/SafeMode"
Value = $MaxControlsValue
Sequence = 201
Owner = $owner
Type = [Microsoft.SharePoint.Administration.SPWebConfigModification+SPWebConfigModificationType]::EnsureAttribute
}
}
process
{
if( $WhatIf )
{
$WebApplication | Remove-SafeControlMaxControlValue -WhatIf
Write-Output "Would have added new web config modification: $owner to $($WebApplication.Url)"
}
else
{
$WebApplication | Remove-SafeControlMaxControlValue
Write-Verbose "Saving new modification: $owner to $($WebApplication.Url)"
$WebApplication.WebConfigModifications.Add($modification)
$WebApplication.Update()
$WebApplication.WebService.ApplyWebConfigModifications()
}
}
end
{
}
}
function Remove-SafeControlMaxControlValue
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SharePoint.Administration.SPWebApplication]$WebApplication,
[Parameter(Mandatory=$false)][switch]$WhatIf
)
begin
{
}
process
{
$update = $false
for( $i = $WebApplication.WebConfigModifications.Count-1; $i -ge 0; $i-- )
{
try
{
if( $WebApplication.WebConfigModifications[$i].Owner -match "^MaxControls=" )
{
if( $WhatIf.IsPresent )
{
Write-Output "Would have removed existing modification $($WebApplication.WebConfigModifications[$i].Owner)"
}
else
{
Write-Verbose "Removing existing modification: $($WebApplication.WebConfigModifications[$i].Owner)"
$WebApplication.WebConfigModifications.Remove($WebApplication.WebConfigModifications[$i]) | Out-Null
$update = $true
}
}
}
catch
{
Write-Error $_
}
if( $update )
{
$WebApplication.Update()
$WebApplication.WebService.ApplyWebConfigModifications()
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
while( $stopwatch.Elapsed.TotalMinutes -le 5 -and (Get-SPTimerjob -Identity "job-webconfig-modification" -Verbose:$false) )
{
Write-Verbose "Waiting for timer job to remove existing Web Config Modifications from all servers."
Start-Sleep -Seconds 5
}
}
}
}
end
{
}
}
# list existing WebConfigModifications
# Get-SPWebApplication | SELECT -ExpandProperty WebConfigModifications
# example how to deploy a new MaxControls Attribute value. Default is 200
Get-SPWebApplication | Update-SafeControlMaxControlValue -MaxControlsValue 400 -Verbose #-WhatIf
# example how to retract the updated MaxControls Attribute value and return the default of 200
# Get-SPWebApplication | Remove-SafeControlMaxControlValue -Verbose -WhatIf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment