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

Add UNC Path support to WebVirtualDirectory #634

Merged
merged 11 commits into from
Jul 7, 2024
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()]
[pscredential]
$Credential
)

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

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

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

if ($WebApplication.Length -gt 0)
{
$ItemPath = "IIS:Sites\$Website\$WebApplication\$Name"
}
else
{
$ItemPath = "IIS:Sites\$Website\$Name"
}

$userName = (Get-ItemProperty $ItemPath -Name userName).Value
if ($userName -ne '')
{
#$password = (Get-ItemProperty $ItemPath -Name password).Value
$password = New-Object System.Security.SecureString # Blank Password
johlju marked this conversation as resolved.
Show resolved Hide resolved
$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 +83,7 @@ function Get-TargetResource
Website = $Website
WebApplication = $WebApplication
PhysicalPath = $PhysicalPath
Credential = $Credential
Ensure = $Ensure
}

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

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

[Parameter()]
[pscredential]
$Credential
)

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

if ($WebApplication.Length -gt 0)
{
$ItemPath = "IIS:Sites\$Website\$WebApplication\$Name"
}
else
{
$ItemPath = "IIS:Sites\$Website\$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 ([bool]([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 `
-Name physicalPath `
-Value $PhysicalPath
}

if ($Credential)
{
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 +246,11 @@ function Test-TargetResource

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

[Parameter()]
[pscredential]
$Credential
)

Assert-Module -ModuleName WebAdministration
Expand All @@ -210,8 +263,34 @@ 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 ($WebApplication.Length -gt 0)
{
$ItemPath = "IIS:Sites\$Website\$WebApplication\$Name"
}
else
{
$ItemPath = "IIS:Sites\$Website\$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.VerboseTestTargetFalse -f $PhysicalPath, $Name)
return $false
}
}
else
{
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,10 @@
# culture ="en-US"
ConvertFrom-StringData -StringData @'
VerboseGetTargetResource = Get-TargetResource has been run.
VerboseSetTargetPhysicalPath = Updating physical path for Web Virtual Directory '{0}'.
VerboseSetTargetPhysicalPath = Updating PhysicalPath and 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.
VerboseTestTargetFalse = Physical path '{0}' and Credential 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