Skip to content

Commit

Permalink
Merge pull request #886 from jensotto/spwebapppropertybag
Browse files Browse the repository at this point in the history
SPWebAppPropertyBag: Added new resource for managing web application property bag.
  • Loading branch information
ykuijs committed Sep 25, 2018
2 parents 597ec5d + e91be51 commit 47263b9
Show file tree
Hide file tree
Showing 7 changed files with 421 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,8 @@
* Fixed issue with numerical Content Sources name;
* SPWebAppSuiteBar
* Fixed incorrect test method that resulted in this resource to never apply changes.
* SPWebAppPropertyBag
* New resource to manage web application property bag

## 2.5

Expand Down
@@ -0,0 +1,169 @@
function Get-TargetResource()
{
[CmdletBinding()]
[OutputType([System.Collections.HashTable])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$WebAppUrl,

[Parameter(Mandatory = $true)]
[System.String]
$Key,

[Parameter()]
[System.String]
$Value,

[Parameter()]
[ValidateSet("Present","Absent")]
[System.String]
$Ensure = 'Present',

[Parameter()]
[System.Management.Automation.PSCredential]
$InstallAccount
)

Write-Verbose -Message "Looking for SPWebApplication property '$Key'"

$result = Invoke-SPDSCCommand -Credential $InstallAccount `
-Arguments $PSBoundParameters `
-ScriptBlock {
$params = $args[0]

$spWebApp = Get-SPWebApplication -Identity $params.WebAppUrl -ErrorAction SilentlyContinue

if ($null -eq $spWebApp)
{
$currentValue = $null
$localEnsure = 'Absent'
}
else
{
if ($spWebApp.Properties)
{
if ($spWebApp.Properties.Contains($params.Key) -eq $true)
{
$localEnsure = "Present"
$currentValue = $spWebApp.Properties[$params.Key]
}
else
{
$localEnsure = "Absent"
$currentValue = $null
}
}
}

return @{
WebAppUrl = $params.WebAppUrl
Key = $params.Key
Value = $currentValue
Ensure = $localEnsure
}
}
return $result
}

function Set-TargetResource()
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$WebAppUrl,

[Parameter(Mandatory = $true)]
[System.String]
$Key,

[Parameter()]
[System.String]
$Value,

[Parameter()]
[ValidateSet("Present","Absent")]
[System.String]
$Ensure = 'Present',

[Parameter()]
[System.Management.Automation.PSCredential]
$InstallAccount
)

Write-Verbose -Message "Setting SPWebApplication property '$Key'"

Invoke-SPDSCCommand -Credential $InstallAccount `
-Arguments $PSBoundParameters `
-ScriptBlock {
$params = $args[0]

$spWebApp = Get-SPWebApplication -Identity $params.WebAppUrl -ErrorAction SilentlyContinue

if ($params.Ensure -eq 'Present')
{
Write-Verbose -Message "Adding property '$($params.Key)'='$($params.value)' to SPWebApplication.Properties"
$spWebApp.Properties[$params.Key] = $params.Value
$spWebApp.Update()
}
else
{
Write-Verbose -Message "Removing property '$($params.Key)' from SPWebApplication.Properties"
$spWebApp.Properties.Remove($params.Key)
$spWebApp.Update()
}
}
}

function Test-TargetResource()
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$WebAppUrl,

[Parameter(Mandatory = $true)]
[System.String]
$Key,

[Parameter()]
[System.String]
$Value,

[Parameter()]
[ValidateSet("Present","Absent")]
[System.String]
$Ensure = 'Present',

[Parameter()]
[System.Management.Automation.PSCredential]
$InstallAccount
)

Write-Verbose -Message "Testing SPWebApplication property '$Key'"

$CurrentValues = Get-TargetResource @PSBoundParameters

if($Ensure -eq 'Present')
{
return Test-SPDscParameterState -CurrentValues $CurrentValues `
-DesiredValues $PSBoundParameters `
-ValuesToCheck @('Ensure','Key', 'Value')
}
else
{
return Test-SPDscParameterState -CurrentValues $CurrentValues `
-DesiredValues $PSBoundParameters `
-ValuesToCheck @('Ensure','Key')

}

}

Export-ModuleMember -Function *-TargetResource
@@ -0,0 +1,9 @@
[ClassVersion("1.0.0.0"), FriendlyName("SPWebAppPropertyBag")]
class MSFT_SPWebAppPropertyBag : OMI_BaseResource
{
[Key, Description("The URL of the web application")] string WebAppUrl;
[Key, Description("The key of the SPWebApplication property")] string Key;
[Write, Description("Value of the SPWebApplication property")] String Value;
[Write, Description("Set to present to ensure the SPWebApplication property exists, or absent to ensure it is removed"), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] string Ensure;
[Write, Description("POWERSHELL 4 ONLY: The account to run this resource as, use PsDscRunAsCredential if using PowerShell 5"), EmbeddedInstance("MSFT_Credential")] String InstallAccount;
};
@@ -0,0 +1,10 @@
# Description

**Type:** Distributed

This resource is used to work with SharePoint Property Bags
at the web application level.
The account that runs this resource must be a farm administrator.

The default value for the Ensure parameter is Present. When not specifying this
parameter, the property bag is configured.
@@ -0,0 +1,28 @@
<#
.EXAMPLE
This example shows how add property bag value in a web application.
#>

Configuration Example
{
param
(
[Parameter(Mandatory = $true)]
[PSCredential]
$SetupAccount
)

Import-DscResource -ModuleName SharePointDsc

node localhost
{
SPWebAppPropertyBag APPLICATION_APPCodeProperty
{
PsDscRunAsCredential = $SetupAccount
WebAppUrl = "https://web.contoso.com"
Key = "KeyToAdd"
Value = "ValueToAddOrModify"
Ensure = "Present"
}
}
}
@@ -0,0 +1,27 @@
<#
.EXAMPLE
This example shows how remove a property bag value in a web application.
#>

Configuration Example
{
param
(
[Parameter(Mandatory = $true)]
[PSCredential]
$SetupAccount
)

Import-DscResource -ModuleName SharePointDsc

node localhost
{
SPWebAppPropertyBag APPLICATION_APPCodeProperty
{
PsDscRunAsCredential = $SetupAccount
WebAppUrl = "https://web.contoso.com"
Key = "KeyToRemove"
Ensure = "Absent"
}
}
}

0 comments on commit 47263b9

Please sign in to comment.