Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.