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: xWebVirtualDirectory renamed to WebVirtualDirectory #428

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
340 changes: 340 additions & 0 deletions DSCResources/MSFT_WebVirtualDirectory/MSFT_WebVirtualDirectory.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,340 @@
# Load the Helper Module
Import-Module -Name "$PSScriptRoot\..\Helper.psm1"

# Import Localization Strings
$localizedData = Get-LocalizedData `
-ResourceName 'MSFT_WebVirtualDirectory' `
-ResourcePath (Split-Path -Parent $Script:MyInvocation.MyCommand.Path)

<#
.SYNOPSIS
The Get-TargetResource cmdlet is used to fetch the status of the virtual
directory in the specific site under specific web application (if specified)
on the target machine.

.PARAMETER Site
Specifies the name of the site.

.PARAMETER Application
Specifies the name of the web application.

.PARAMETER Name
Specifies the name of the virtual directory.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$Site,

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

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

Assert-Module

$virtualDirectory = Get-WebVirtualDirectory @PSBoundParameters

if ($virtualDirectory.Count -eq 1)
{
$Ensure = 'Present'
Write-Verbose -Message ($LocalizedData.VerboseGetTargetPresent -f $Name)
}
else
{
$Ensure = 'Absent'
Write-Verbose -Message ($LocalizedData.VerboseGetTargetAbsent -f $Name)
}

return @{
Ensure = $Ensure
Name = $Name
Site = $Site
Application = $Application
PhysicalPath = $virtualDirectory.PhysicalPath
PhysicalPathAccessAccount = $virtualDirectory.userName
PhysicalPathAccessPass = $virtualDirectory.password
}
}

<#
.SYNOPSIS
The Set-TargetResource cmdlet is used to create, delete or configure a virtual
directory in the specific site under specific web application (if specified)
on the target machine.

.PARAMETER Ensure
Specifies whether the virtual directory should be present.

.PARAMETER Site
Specifies the name of the site.

.PARAMETER Application
Specifies the name of the web application.

.PARAMETER Name
Specifies the name of the virtual directory.

.PARAMETER PhysicalPath
Specifies physical folder location for virtual directory.

.PARAMETER PhysicalPathAccessAccount
Specifies username for access to the physical path if required.

.PARAMETER PhysicalPathAccessPass
Specifies password for access to the physical path if required.
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[Parameter()]
[ValidateSet('Present','Absent')]
[System.String]
$Ensure = 'Present',

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

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

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

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$PhysicalPath,

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

[Parameter()]
[System.String]
$PhysicalPathAccessPass
)

Assert-Module

$Location = "$Site\$Name"
if ($Application)
{
$Location = "$Site\$Application\$Name"
}

$virtualDirectory = Get-WebVirtualDirectory -Site $Site `
-Name $Name `
-Application $Application

if ($Ensure -eq 'Present')
{
# Creating virtual directory
if ($virtualDirectory.count -eq 0)
{
if ([bool]([System.Uri]$PhysicalPath).IsUnc)
{
# If physical path is provided using Unc syntax run New-WebVirtualDirectory with -Force flag
$virtualDirectory = New-WebVirtualDirectory -Site $Site `
-Application $Application `
-Name $Name `
-ErrorAction Stop `
-Force
}
else
{
# Run New-WebVirtualDirectory without -Force flag to verify that the path exists
$virtualDirectory = New-WebVirtualDirectory -Site $Site `
-Application $Application `
-Name $Name `
-PhysicalPath $PhysicalPath `
-ErrorAction Stop
}

Write-Verbose -Message ($LocalizedData.VerboseSetTargetCreateVirtualDirectory -f $Name)
}

# Update physical path if required
if ($PSBoundParameters.ContainsKey('PhysicalPath') -and `
$virtualDirectory.PhysicalPath -ne $PhysicalPath)
{
Set-ItemProperty -Path "IIS:\Sites\$Location" `
-Name physicalPath `
-Value $PhysicalPath `
-ErrorAction Stop
Write-Verbose -Message ($LocalizedData.VerboseSetTargetUpdatePhysicalPath -f $Name)
}

# Update physical path access username if required
if ($PSBoundParameters.ContainsKey('PhysicalPathAccessAccount') -and `
$virtualDirectory.userName -ne $PhysicalPathAccessAccount)
{
Set-ItemProperty -Path "IIS:\Sites\$Location" `
-Name userName `
-Value $PhysicalPathAccessAccount `
-ErrorAction Stop
Write-Verbose -Message ($LocalizedData.VerboseSetTargetUpdatePhysicalPathAccessAccount `
-f $Name)
}

# Update physical path access password if required
if ($PSBoundParameters.ContainsKey('PhysicalPathAccessPass') -and `
$virtualDirectory.password -ne $PhysicalPathAccessPass)
{
Set-ItemProperty -Path "IIS:\Sites\$Location" `
-Name password `
-Value $PhysicalPathAccessPass `
-ErrorAction Stop
Write-Verbose -Message ($LocalizedData.VerboseSetTargetUpdatePhysicalPathAccessPass `
-f $Name)
}
}
else
{
# Remove virtual directory
Remove-Item -Path "IIS:\Sites\$Location" -Recurse -Force -ErrorAction Stop
Write-Verbose -Message ($LocalizedData.VerboseSetTargetRemoveVirtualDirectory -f $Name)
}
}

<#
.SYNOPSIS
The Test-TargetResource cmdlet is used to validate if the virtual directory
is in a desired state. If the state is not correct it will return $false.
If the state is correct it will return $true

.PARAMETER Ensure
Specifies whether the virtual directory should be present.

.PARAMETER Site
Specifies the name of the site.

.PARAMETER Application
Specifies the name of the web application.

.PARAMETER Name
Specifies the name of the virtual directory.

.PARAMETER PhysicalPath
Specifies physical folder location for virtual directory.

.PARAMETER PhysicalPathAccessAccount
Specifies username for access to the physical path if required.

.PARAMETER PhysicalPathAccessPass
Specifies password for access to the physical path if required.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter()]
[ValidateSet('Present','Absent')]
[System.String]
$Ensure = 'Present',

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

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

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

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$PhysicalPath,

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

[Parameter()]
[System.String]
$PhysicalPathAccessPass
)

Assert-Module

$inDesiredState = $true

$virtualDirectory = Get-WebVirtualDirectory -Site $Site `
-Application $Application `
-Name $Name
# Check Ensure
if (($Ensure -eq 'Present' -and $virtualDirectory.Count -eq 0) -or `
($Ensure -eq 'Absent' -and $virtualDirectory.Count -eq 1))
{
$inDesiredState = $false
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseEnsure `
-f $Name)
}

# Only check properties if the virtual directory exists
if ($Ensure -eq 'Present' -and $virtualDirectory.Count -eq 1)
{
# Check physical path
if ($PSBoundParameters.ContainsKey('PhysicalPath') -and `
$virtualDirectory.PhysicalPath -ne $PhysicalPath)
{
$inDesiredState = $false
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalsePhysicalPath `
-f $Name)
}

# Check physical path access username if required
if ($PSBoundParameters.ContainsKey('PhysicalPathAccessAccount') -and `
$virtualDirectory.userName -ne $PhysicalPathAccessAccount)
{
$InDesiredState = $false
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalsePhysicalPathAccessAccount `
-f $Name)
}

# Check physical path access password if required
if ($PSBoundParameters.ContainsKey('PhysicalPathAccessPass') -and `
$virtualDirectory.password -ne $PhysicalPathAccessPass)
{
$InDesiredState = $false
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalsePhysicalPathAccessPass `
-f $Name)
}
}

if ($inDesiredState -eq $true)
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetTrueResult)
}
else
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseResult)
}

return $inDesiredState
}

Export-ModuleMember -Function *-TargetResource
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[ClassVersion("1.0.0.0"), FriendlyName("WebVirtualDirectory")]
class MSFT_WebVirtualDirectory : OMI_BaseResource
{
[Key, Description("Name of website with which Web Application is associated")] String Site;
[Key, Description("Web application name for the virtual directory")] String Application;
[Key, Description("Name of virtual directory")] String Name;
[Write, Description("Physical path for the virtual directory")] String PhysicalPath;
[Write, Description("Specifies the username for physical path access in the virtual directory.")] String PhysicalPathAccessAccount;
[Write, Description("Specifies the password for physical path access in the virtual directory.")] String PhysicalPathAccessPass;
[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
@@ -0,0 +1,16 @@
ConvertFrom-StringData @'
ErrorPhysicalPathNotSpecifiedOrEmpty = The PhysicalPath parameter must be provided for a virtual directory "{0}" to be created.
VerboseGetTargetAbsent = No virtual directories exists with the name "{0}".
VerboseGetTargetPresent = Virtual directory with the name "{0}" exists.
VerboseSetTargetCreateVirtualDirectory = Successfully created virtual directory "{0}".
VerboseSetTargetRemoveVirtualDirectory = Successfully removed virtual directory "{0}".
VerboseSetTargetUpdatePhysicalPath = Successfully updated PhysicalPath for the virtual directory "{0}".
VerboseSetTargetUpdatePhysicalPathAccessPass = Successfully updated password for physical path access in the virtual directory "{0}".
VerboseSetTargetUpdatePhysicalPathAccessAccount = Successfully updated username for physical path access in the virtual directory "{0}".
VerboseTestTargetFalseEnsure = Ensure state for the virtual directory "{0}" does not match the desired state.
VerboseTestTargetFalsePhysicalPath = Physical Path for the virtual directory "{0}" does not match the desired state.
VerboseTestTargetFalsePhysicalPathAccessPass = Password for physical path access in the virtual directory "{0}" does not match the desired state.
VerboseTestTargetFalsePhysicalPathAccessAccount = Username for physical path access in the virtual directory "{0}" does not match the desired state.
VerboseTestTargetFalseResult = The target resource is not in the desired state.
VerboseTestTargetTrueResult = The target resource is already in the desired state. No action is required.
'@
Loading