Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE: xWebConfigProperty: Added Location to allow access to locked sections of ApplicationHost.Config #400

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 86 additions & 62 deletions DSCResources/MSFT_xWebConfigProperty/MSFT_xWebConfigProperty.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ data LocalizedData
.PARAMETER Filter
Required. Filter used to locate property to update.

.PARAMETER Location
Required. Location tag to use for property.

.PARAMETER PropertyName
Required. Name of the property to update.
#>
Expand All @@ -40,41 +43,43 @@ function Get-TargetResource
[string]
$Filter,

[Parameter(Mandatory = $true)]
[AllowEmptyString()]
[string]
$Location,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
$PropertyName
)
# Retrieve the value of the existing property if present.
Write-Verbose `
-Message ($LocalizedData.VerboseTargetCheckingTarget -f $PropertyName, $Filter, $WebsitePath )
-Message ($LocalizedData.VerboseTargetCheckingTarget -f $PropertyName, $Filter, $WebsitePath)

$existingValue = Get-ItemValue `
-WebsitePath $WebsitePath `
$existingValue = Get-ItemValue -WebsitePath $WebsitePath `
-Filter $Filter `
-Location $Location `
-PropertyName $PropertyName

$result = @{
WebsitePath = $WebsitePath
Filter = $Filter
Location = $Location
PropertyName = $PropertyName
Ensure = 'Present'
Value = $existingValue
}

if (-not($existingValue))
{
if ( -not($existingValue) ) {
# Property was not found.
Write-Verbose `
-Message ($LocalizedData.VerboseTargetPropertyNotFound -f $PropertyName )
Write-Verbose -Message ($LocalizedData.VerboseTargetPropertyNotFound -f $PropertyName)

$result.Ensure = 'Absent'
}
else
{
else {
# Property was found.
Write-Verbose `
-Message ($LocalizedData.VerboseTargetPropertyFound -f $PropertyName )
Write-Verbose -Message ($LocalizedData.VerboseTargetPropertyFound -f $PropertyName)
}

return $result
Expand All @@ -90,6 +95,9 @@ function Get-TargetResource
.PARAMETER Filter
Required. Filter used to locate property to update.

.PARAMETER Location
Required. Location tag to use for property.

.PARAMETER PropertyName
Required. Name of the property to update.

Expand All @@ -114,6 +122,11 @@ function Set-TargetResource
[string]
$Filter,

[Parameter(Mandatory = $true)]
[AllowEmptyString()]
[string]
$Location,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
Expand All @@ -128,40 +141,35 @@ function Set-TargetResource
[string]
$Ensure = 'Present'
)
if ($Ensure -eq 'Present')
{
if ($Ensure -eq 'Present') {
# Property needs to be updated.
Write-Verbose `
-Message ($LocalizedData.VerboseSetTargetEditItem -f $PropertyName )
Write-Verbose -Message ($LocalizedData.VerboseSetTargetEditItem -f $PropertyName)

$propertyType = Get-ItemPropertyType -WebsitePath $WebsitePath -Filter $Filter -PropertyName $PropertyName
$propertyType = Get-ItemPropertyType -WebsitePath $WebsitePath `
-Filter $Filter `
-Location $Location `
-PropertyName $PropertyName

if ($propertyType -match 'Int32|Int64')
{
$setValue = Convert-PropertyValue -PropertyType $propertyType -InputValue $Value
}
{ $setValue = Convert-PropertyValue -PropertyType $propertyType -InputValue $Value }
else
{
$setValue = $Value
}
{ $setValue = $Value }

Set-WebConfigurationProperty `
Set-WebConfigurationProperty -PSPath $WebsitePath `
-Filter $Filter `
-PSPath $WebsitePath `
-Location $Location `
-Name $PropertyName `
-Value $setValue `
-WarningAction Stop
-WarningAction "Stop"
}
else
{
else {
# Property needs to be removed.
Write-Verbose `
-Message ($LocalizedData.VerboseSetTargetRemoveItem -f $PropertyName )
Write-Verbose -Message ($LocalizedData.VerboseSetTargetRemoveItem -f $PropertyName)

Clear-WebConfiguration `
-Filter "$($Filter)/@$($PropertyName)" `
-PSPath $WebsitePath `
-WarningAction Stop
Clear-WebConfiguration -PSPath $WebsitePath `
-Filter "$($Filter)/@$($PropertyName)" `
-Location $Location `
-WarningAction "Stop"
}
}

Expand All @@ -175,6 +183,9 @@ function Set-TargetResource
.PARAMETER Filter
Required. Filter used to locate property to update.

.PARAMETER Location
Required. Location tag to use for property.

.PARAMETER PropertyName
Required. Name of the property to update.

Expand All @@ -200,6 +211,11 @@ function Test-TargetResource
[string]
$Filter,

[Parameter(Mandatory = $true)]
[AllowEmptyString()]
[string]
$Location,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
Expand All @@ -215,39 +231,31 @@ function Test-TargetResource
$Ensure = 'Present'
)
# Retrieve the value of the existing property if present.
Write-Verbose `
-Message ($LocalizedData.VerboseTargetCheckingTarget -f $PropertyName, $Filter, $WebsitePath )
Write-Verbose -Message ($LocalizedData.VerboseTargetCheckingTarget -f $PropertyName, $Filter, $WebsitePath)

$targetResource = Get-TargetResource `
-WebsitePath $WebsitePath `
$targetResource = Get-TargetResource -WebsitePath $WebsitePath `
-Filter $Filter `
-PropertyName $PropertyName
-PropertyName $PropertyName `
-Location $Location

if ($Ensure -eq 'Present')
{
if ( ($null -eq $targetResource.Value) -or ($targetResource.Value.ToString() -ne $Value) )
{
if ($Ensure -eq 'Present') {
if ( ($null -eq $targetResource.Value) -or ($targetResource.Value.ToString() -ne $Value) ) {
# Property was not found or didn't have expected value.
Write-Verbose `
-Message ($LocalizedData.VerboseTargetPropertyNotFound -f $PropertyName )
Write-Verbose -Message ($LocalizedData.VerboseTargetPropertyNotFound -f $PropertyName)

return $false
}
}
else
{
if ( ($null -ne $targetResource.Value) -and ($targetResource.Value.ToString().Length -ne 0 ) )
{
else {
if ( ($null -ne $targetResource.Value) -and ($targetResource.Value.ToString().Length -ne 0 ) ) {
# Property was found.
Write-Verbose `
-Message ($LocalizedData.VerboseTargetPropertyWasFound -f $PropertyName )
Write-Verbose -Message ($LocalizedData.VerboseTargetPropertyWasFound -f $PropertyName)

return $false
}
}

Write-Verbose `
-Message ($LocalizedData.VerboseTargetPropertyWasFound -f $PropertyName)
Write-Verbose -Message ($LocalizedData.VerboseTargetPropertyWasFound -f $PropertyName)

return $true
}
Expand All @@ -264,6 +272,9 @@ function Test-TargetResource
.PARAMETER Filter
Required. Filter used to locate property to retrieve.

.PARAMETER Location
Optional. Location tag to use for property.

.PARAMETER PropertyName
Required. Name of the property to retrieve.
#>
Expand All @@ -283,20 +294,24 @@ function Get-ItemValue
[string]
$Filter,

[Parameter(Mandatory = $false)]
[string]
$Location,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
$PropertyName
)

# Retrieve the value of the specified property if present.
$value = Get-WebConfigurationProperty `
-PSPath $WebsitePath `
$value = Get-WebConfigurationProperty -PSPath $WebsitePath `
-Filter $Filter `
-Name $PropertyName
-Name $PropertyName `
-Location $Location

# Return the value of the property if located.
if ($value -is [Microsoft.IIs.PowerShell.Framework.ConfigurationAttribute])
{
if ($value -is [Microsoft.IIs.PowerShell.Framework.ConfigurationAttribute]) {
return $value.Value
}
return $value
Expand All @@ -307,13 +322,16 @@ function Get-ItemValue
Gets the current data type of the property.

.PARAMETER WebsitePath
Path to website location (IIS or WebAdministration format).
Required. Path to website location (IIS or WebAdministration format).

.PARAMETER Filter
Filter used to locate property to retrieve.
Required. Filter used to locate property to retrieve.

.PARAMETER Location
Optional. Location tag to use for property.

.PARAMETER PropertyName
Name of the property to retrieve.
Required. Name of the property to retrieve.
#>
function Get-ItemPropertyType
{
Expand All @@ -331,15 +349,21 @@ function Get-ItemPropertyType
[string]
$Filter,

[Parameter(Mandatory = $false)]
[string]
$Location,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
$PropertyName
)

$webConfiguration = Get-WebConfiguration -Filter $Filter -PsPath $WebsitePath
$webConfiguration = Get-WebConfiguration -Filter $Filter `
-PsPath $WebsitePath `
-Location $Location

$property = $webConfiguration.Schema.AttributeSchemas | Where-Object -FilterScript {$_.Name -eq $propertyName}
$property = $webConfiguration.Schema.AttributeSchemas | Where-Object -FilterScript { $_.Name -eq $propertyName }

return $property.ClrType.Name
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class MSFT_xWebConfigProperty : OMI_BaseResource
[Key, Description("Path to website location (IIS or WebAdministration format).")] String WebsitePath;
[Key, Description("Filter used to locate property to update.")] String Filter;
[Key, Description("Name of the property to update.")] String PropertyName;
[Key, Description("Location is used to update locked sections in the root config.")] String Location;
[Write, Description("Indicates if the property and value should be present or absent. Defaults to Present."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
[Write, Description("Value of the property to update.")] String Value;
};
1 change: 1 addition & 0 deletions Examples/Sample_xWebConfigProperty_Add.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Configuration Sample_xWebConfigProperty_Add
{
WebsitePath = 'IIS:\Sites\Default Web Site'
Filter = 'system.webServer/directoryBrowse'
Location = ''
PropertyName = 'enabled'
Value = 'false'
Ensure = 'Present'
Expand Down
1 change: 1 addition & 0 deletions Examples/Sample_xWebConfigProperty_Remove.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Configuration Sample_xWebConfigProperty_Remove
{
WebsitePath = 'IIS:\Sites\Default Web Site'
Filter = 'system.webServer/directoryBrowse'
Location = ''
PropertyName = 'enabled'
Ensure = 'Absent'
}
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ Ensures the value of an identified property in the web.config file.
* **WebsitePath**: Path to website location (IIS or WebAdministration format).
* **Filter**: Filter used to locate property to update.
* **PropertyName**: Name of the property to update.
* **Location**: Name of the location to update.
* **Value**: Value of the property to update.
* **Ensure**: Indicates if the property and value should be present or absent. Defaults to 'Present'. { *Present* | Absent }

Expand Down Expand Up @@ -317,12 +318,16 @@ This resource manages the IIS configuration section locking (overrideMode) to co

### Unreleased

<<<<<<< HEAD
* Added new reosurce xWebConfigProperty extening functionality provided by xWebConfigProperty to allow writing of locked sections in ApplicationHost.Config
=======
### 2.4.0.0

* Explicitly removed extra hidden files from release package

### 2.3.0.0

>>>>>>> 0b60a44eea60673f883544ecbf5cd294ecc750a8
* Update appveyor.yml to use the default template.
* Added default template file .gitattributes, and added default settings for
Visual Studio Code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ try
NodeName = 'localhost'
WebsitePath = "IIS:\Sites\$($websiteName)"
Filter = 'system.webServer/directoryBrowse'
Location = ''
PropertyName = 'enabled'
AddValue = $true
UpdateValue = $false
Expand Down
4 changes: 4 additions & 0 deletions Tests/Integration/MSFT_xWebConfigProperty.config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Configuration MSFT_xWebConfigProperty_Add
{
WebsitePath = $Node.WebsitePath
Filter = $Node.Filter
Location = $Node.Location
PropertyName = $Node.PropertyName
Value = $Node.AddValue
Ensure = 'Present'
Expand All @@ -25,6 +26,7 @@ Configuration MSFT_xWebConfigProperty_Update
{
WebsitePath = $Node.WebsitePath
Filter = $Node.Filter
Location = $Node.Location
PropertyName = $Node.PropertyName
Value = $Node.UpdateValue
Ensure = 'Present'
Expand All @@ -42,6 +44,7 @@ Configuration MSFT_xWebConfigProperty_Integer
{
WebsitePath = $Node.WebsitePath
Filter = $Node.IntegerFilter
Location = $Node.Location
PropertyName = $Node.IntergerPropertyName
Value = $Node.IntegerValue
Ensure = 'Present'
Expand All @@ -59,6 +62,7 @@ Configuration MSFT_xWebConfigProperty_Remove
{
WebsitePath = $Node.WebsitePath
Filter = $Node.Filter
Location = $Node.Location
PropertyName = $Node.PropertyName
Ensure = 'Absent'
}
Expand Down
Loading