Skip to content

Commit

Permalink
Add UNC Path support to WebVirtualDirectory (#634)
Browse files Browse the repository at this point in the history
- WebVirtualDirectory
  - Added Credential paramater
  - Fixed error when using UNC PhysicalPath (issue #94).
  • Loading branch information
RWebster-Noble authored Jul 7, 2024
1 parent db3d024 commit 617e801
Show file tree
Hide file tree
Showing 6 changed files with 372 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)
- Website
- Add Ensure to LogCustomFieldInformation. ([issue #571](https://github.com/dsccommunity/WebAdministrationDsc/issues/571))
- Added code to ensure certificate selected has longest time until expiration when multiple matching certificates are found ([issue #578](https://github.com/dsccommunity/WebAdministrationDsc/issues/578))
- WebVirtualDirectory
- Added Credential paramater

### Fixed

Expand All @@ -27,6 +29,8 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)
- WebSite
- Added code to ensure certificate has private key. ([issue #578](https://github.com/dsccommunity/WebAdministrationDsc/issues/578))
- Removed duplicated resource descriptions in README.md
- WebVirtualDirectory
- Fixed error when using UNC PhysicalPath. ([issue #94](https://github.com/dsccommunity/WebAdministrationDsc/issues/94))

## [4.1.0] - 2023-01-03

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ function Get-TargetResource

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

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

Assert-Module -ModuleName WebAdministration
Expand All @@ -46,11 +50,29 @@ function Get-TargetResource

$PhysicalPath = ''
$Ensure = 'Absent'
$Credential = $null

if ($virtualDirectory.Count -eq 1)
{
$PhysicalPath = $virtualDirectory.PhysicalPath
$Ensure = 'Present'

if ([System.String]::IsNullOrEmpty($WebApplication))
{
$itemPath = "IIS:Sites\$Website\$Name"
}
else
{
$itemPath = "IIS:Sites\$Website\$WebApplication\$Name"
}

$userName = (Get-ItemProperty $itemPath -Name UserName).Value
if (-not [System.String]::IsNullOrEmpty($userName))
{
$password = New-Object System.Security.SecureString # Blank Password
$secStringPassword = $password | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($userName, $secStringPassword)
}
}

Write-Verbose -Message ($script:localizedData.VerboseGetTargetResource)
Expand All @@ -60,6 +82,7 @@ function Get-TargetResource
Website = $Website
WebApplication = $WebApplication
PhysicalPath = $PhysicalPath
Credential = $Credential
Ensure = $Ensure
}

Expand Down Expand Up @@ -95,7 +118,11 @@ function Set-TargetResource

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

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

Assert-Module -ModuleName WebAdministration
Expand All @@ -115,34 +142,57 @@ function Set-TargetResource
$WebApplication = ''
}

if ([System.String]::IsNullOrEmpty($WebApplication))
{
$itemPath = "IIS:Sites\$Website\$Name"
}
else
{
$itemPath = "IIS:Sites\$Website\$WebApplication\$Name"
}

$virtualDirectory = Get-WebVirtualDirectory -Site $Website `
-Name $Name `
-Application $WebApplication
if ($virtualDirectory.count -eq 0)
{
Write-Verbose -Message ($script:localizedData.VerboseSetTargetCreateVirtualDirectory -f $Name)
New-WebVirtualDirectory -Site $Website `
-Application $WebApplication `
-Name $Name `
-PhysicalPath $PhysicalPath
}
else
{
Write-Verbose -Message ($script:localizedData.VerboseSetTargetPhysicalPath -f $Name)

if ($WebApplication.Length -gt 0)
if (([System.Uri] $PhysicalPath).IsUnc)
{
$ItemPath = "IIS:Sites\$Website\$WebApplication\$Name"
# If physical path is provided using Unc syntax run New-WebVirtualDirectory with -Force flag
New-WebVirtualDirectory -Site $Website `
-Application $WebApplication `
-Name $Name `
-PhysicalPath $PhysicalPath `
-ErrorAction Stop `
-Force
}
else
{
$ItemPath = "IIS:Sites\$Website\$Name"
# Run New-WebVirtualDirectory without -Force flag to verify that the path exists
New-WebVirtualDirectory -Site $Website `
-Application $WebApplication `
-Name $Name `
-PhysicalPath $PhysicalPath `
-ErrorAction Stop
}
}
else
{
Write-Verbose -Message ($script:localizedData.VerboseSetTargetPhysicalPath -f $Name)

Set-ItemProperty -Path $ItemPath `
Set-ItemProperty -Path $itemPath `
-Name physicalPath `
-Value $PhysicalPath
}

if ($Credential)
{
Write-Verbose -Message ($script:localizedData.VerboseSetTargetCredential -f $Name)

Set-ItemProperty $itemPath -Name UserName -Value $Credential.UserName
Set-ItemProperty $itemPath -Name Password -Value $Credential.GetNetworkCredential().Password
}
}

if ($Ensure -eq 'Absent')
Expand Down Expand Up @@ -197,7 +247,11 @@ function Test-TargetResource

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

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

Assert-Module -ModuleName WebAdministration
Expand All @@ -210,12 +264,38 @@ function Test-TargetResource
{
if ($virtualDirectory.PhysicalPath -eq $PhysicalPath)
{
Write-Verbose -Message ($script:localizedData.VerboseTestTargetTrue)
return $true
if (-not $Credential)
{
Write-Verbose -Message ($script:localizedData.VerboseTestTargetTrue)
return $true
}

if ([System.String]::IsNullOrEmpty($WebApplication))
{
$itemPath = "IIS:Sites\$Website\$Name"
}
else
{
$itemPath = "IIS:Sites\$Website\$WebApplication\$Name"
}

$userName = (Get-ItemProperty $itemPath -Name UserName).Value
$password = (Get-ItemProperty $itemPath -Name Password).Value

if (($Credential.UserName -eq $userName -and $Credential.GetNetworkCredential().Password -eq $password))
{
Write-Verbose -Message ($script:localizedData.VerboseTestTargetTrue)
return $true
}
else
{
Write-Verbose -Message ($script:localizedData.VerboseTestTargetCredentialFalse -f $PhysicalPath, $Name)
return $false
}
}
else
{
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalse -f $PhysicalPath, $Name)
Write-Verbose -Message ($script:localizedData.VerboseTestTargetPhysicalPathFalse -f $Credential.UserName, $Name)
return $false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ class DSC_WebVirtualDirectory : OMI_BaseResource
[Key, Description("Web application name for the virtual directory")] string WebApplication;
[Key, Description("Name of virtual directory")] string Name;
[Required, Description("Physical path for the virtual directory")] string PhysicalPath;
[Write, Description("Credential to use for accessing the virtual directory"), EmbeddedInstance("MSFT_Credential")] String Credential;
[Write, Description("Whether virtual directory should be present or absent"), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] string Ensure;
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# culture ="en-US"
ConvertFrom-StringData -StringData @'
VerboseGetTargetResource = Get-TargetResource has been run.
VerboseSetTargetPhysicalPath = Updating physical path for Web Virtual Directory '{0}'.
VerboseSetTargetPhysicalPath = Updating PhysicalPath for Web Virtual Directory '{0}'.
VerboseSetTargetCredential = Updating Credential for Web Virtual Directory '{0}'.
VerboseSetTargetCreateVirtualDirectory = Creating new Web Virtual Directory '{0}'.
VerboseSetTargetRemoveVirtualDirectory = Removing existing Virtual Directory '{0}'.
VerboseTestTargetFalse = Physical path '{0}' for Web Virtual Directory '{1}' is not in desired state.
VerboseTestTargetPhysicalPathFalse = Physical path '{0}' for Web Virtual Directory '{1}' is not in desired state.
VerboseTestTargetCredentialFalse = Credential {0} for Web Virtual Directory '{1}' is not in desired state.
VerboseTestTargetTrue = Web Virtual Directory is in desired state.
VerboseTestTargetAbsentTrue = Web Virtual Directory '{0}' should be Absent and is Absent.
'@
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<#
.SYNOPSIS
Create a new web virtual directory on the Default Web Site
.DESCRIPTION
This example shows how to use the WebVirtualDirectory DSC resource to create a new virtual
directory on the Default Web Site with a UNC path that requires credentials.
#>
configuration Sample_WebVirtualDirectory_NewVirtualDirectory_WithUncPath
{
param
(
# Target nodes to apply the configuration
[System.String[]]
$NodeName = 'localhost',

# Name of virtual directory to create
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[System.String]
$VirtualDirectoryName,

# Physical path of the virtual directory
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[System.String]
$PhysicalPath,

# Credential to use for the virtual directory
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[PSCredential]
$Credential
)

# Import the module that defines custom resources
Import-DscResource -Module PSDesiredStateConfiguration
Import-DscResource -Module WebAdministrationDsc

Node $NodeName
{
# Install the IIS role
WindowsFeature IIS
{
Ensure = 'Present'
Name = 'Web-Server'
}

# Start the default website
WebSite DefaultSite
{
Ensure = 'Present'
Name = 'Default Web Site'
State = 'Started'
PhysicalPath = 'C:\inetpub\wwwroot'
DependsOn = '[WindowsFeature]IIS'
}

# Copy the virtual directory content
File VirtualDirectoryContent
{
Ensure = 'Present'
DestinationPath = $PhysicalPath
Type = 'Directory'
DependsOn = '[WindowsFeature]IIS'
}

# Create the new virtual directory
WebVirtualDirectory NewVirtualDirectory
{
Ensure = 'Present'
Website = "Default Web Site"
WebApplication = ''
Name = $VirtualDirectoryName
PhysicalPath = $PhysicalPath
DependsOn = '[File]VirtualDirectoryContent'
Credential = $Credential
}
}
}
Loading

0 comments on commit 617e801

Please sign in to comment.