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

Enhancing module #20

Merged
merged 42 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
a37f46d
Fixing parameter passing to New-SmbShare
Jan 19, 2018
c5b4e26
Added removal of parameters that are null or emtpy
Jan 19, 2018
8fdd58d
Update to MSFT_xSmbShare.psm1
Jun 21, 2018
9099075
Update MSFT_xSmbShare.psm1
Jun 21, 2018
9ad782f
Created unit tests file
Jun 28, 2018
305d045
Adding unit tests
Jul 9, 2018
cab8d43
Moving unit test to another folder
Jul 11, 2018
95c312e
Deleting unit tests file
Jul 11, 2018
d7d906a
Replaced tabs with 4 spaces
Jul 11, 2018
b290ddc
Updated curly brace locations
Jul 11, 2018
671e917
Updating unit tests
twerthi Jul 26, 2018
450e577
Updated FolderEnumerationMode
twerthi Jul 26, 2018
7dd1725
Fixing GetResource test
twerthi Aug 1, 2018
baac760
Removing NoAccess so it won't get bound
twerthi Aug 1, 2018
e5a247a
Removing module name reference
twerthi Aug 1, 2018
a9acf50
Moved resource call, mock statements
twerthi Aug 2, 2018
4a1132f
Changed scoping for assert
twerthi Aug 3, 2018
aff0af2
Fixing test
twerthi Aug 3, 2018
683681c
Fixing test target
twerthi Aug 3, 2018
10ee2a1
Adding method to alter parameters
twerthi Aug 3, 2018
b30cfc3
Using method for Set resource
twerthi Aug 3, 2018
5abf4cc
Context were backwards
twerthi Aug 6, 2018
0ad8062
Fixed Test resource parameters
twerthi Aug 6, 2018
7a9ef13
Fixed typo
twerthi Aug 6, 2018
fa21dc4
Testing parameters
twerthi Aug 6, 2018
ac038f5
Mocked Get-TargetResource
twerthi Aug 17, 2018
8721898
Mock of Get-TargetResource was incorrect
twerthi Aug 17, 2018
00819b5
Because I'm dumb
twerthi Aug 17, 2018
633dc5b
Forced Ensure to present
twerthi Aug 17, 2018
0bf1b60
Moved variables into InScope block
twerthi Aug 23, 2018
bd09615
Attempting to provide more output
twerthi Aug 23, 2018
f269cf0
Changed parameter name of method
twerthi Oct 16, 2018
159fb66
Fixed tests
twerthi Oct 17, 2018
eb5eeea
Merge branch 'dev' into dev
Oct 17, 2018
54793ec
Added verbose flag
twerthi Oct 26, 2018
617d4e7
Updated code with requested changes
twerthi Jan 28, 2019
606135a
Updating upper case to lowercase on foreach
twerthi Feb 27, 2019
028f628
Updating verbose message
twerthi Mar 4, 2019
cc92feb
Syntax error in write-verbose call
twerthi Mar 4, 2019
824c4e5
updated readme.md cosmetic
gaelcolas Mar 27, 2019
5c7f1a1
Added changelog entry
gaelcolas Mar 27, 2019
fc7ac99
variable capitalization and cosmetic
gaelcolas Mar 27, 2019
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: 117 additions & 31 deletions DscResources/MSFT_xSmbShare/MSFT_xSmbShare.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function Get-TargetResource
if ($smbShare -ne $null)
{
$smbShareAccess = Get-SmbShareAccess -Name $Name
$smbShareAccess | % {
$smbShareAccess | ForEach-Object {
$access = $_;
if ($access.AccessRight -eq 'Change' -and $access.AccessControlType -eq 'Allow')
{
Expand Down Expand Up @@ -94,6 +94,47 @@ function Set-AccessPermission
}
}

Function Set-BoundParameters
{
# Define parameters
Param
(
$boundparameters
)

# Check for null access before passing to New-SmbShare
if (($boundparameters.ContainsKey("ChangeAccess")) -and ([string]::IsNullOrEmpty($boundparameters["ChangeAccess"])))
{
Write-Verbose "Parameter ChangeAccess is null or empty, removing from collection."
# Remove the parameter
$boundparameters.Remove("ChangeAccess")
}

if (($boundparameters.ContainsKey("ReadAccess")) -and ([string]::IsNullOrEmpty($boundparameters["ReadAccess"])))
{
Write-Verbose "Paramater ReadAccess is null or empty, removing from collection."
# Remove the parameter
$boundparameters.Remove("ReadAccess")
}

if (($boundparameters.ContainsKey("FullAccess")) -and ([string]::IsNullOrEmpty($boundparameters["FullAccess"])))
{
Write-Verbose "Parameter FullAccess is null or empty, removing from collection."
# Remove the parameter
$boundparameters.Remove("FullAccess")
}

if (($boundparameters.ContainsKey("NoAccess")) -and ([string]::IsNullOrEmpty($boundparameters["NoAccess"])))
{
Write-Verbose "Parameter NoAccess is null or empty, removing from collection."
# Remove the parameter
$boundparameters.Remove("NoAccess")
}

# Return the parameter collection
return $boundparameters
}

function Remove-AccessPermission
{
[CmdletBinding()]
Expand All @@ -114,7 +155,8 @@ function Remove-AccessPermission
if ($AccessPermission -eq "Change" -or $AccessPermission -eq "Read" -or $AccessPermission -eq "Full")
{
Revoke-SmbShareAccess -Name $Name -AccountName $UserName -Force
}

}
else
{
UnBlock-SmbShareAccess -Name $Name -AccountName $userName -Force
Expand Down Expand Up @@ -179,7 +221,12 @@ function Set-TargetResource
{
$psboundparameters.Remove("Ensure")
Write-Verbose "Creating share $Name to ensure it is Present"
New-SmbShare @psboundparameters

# Alter bound parameters
$newShareParameters = Set-BoundParameters -boundparameters $psBoundParameters

# Pass the parameter collection to New-SmbShare
New-SmbShare @newShareParameters
}
else
{
Expand Down Expand Up @@ -212,53 +259,69 @@ function Set-TargetResource

# Use *SmbShareAccess cmdlets to change access
$smbshareAccessValues = Get-SmbShareAccess -Name $Name

# Remove Change permissions
$smbshareAccessValues | Where-Object {$_.AccessControlType -eq 'Allow' -and $_.AccessRight -eq 'Change'} `
| ForEach-Object {
Remove-AccessPermission -ShareName $Name -UserName $_.AccountName -AccessPermission Change
}

if ($ChangeAccess -ne $null)
{
# Blow off whatever is in there and replace it with this list
$smbshareAccessValues | ? {$_.AccessControlType -eq 'Allow' -and $_.AccessRight -eq 'Change'} `
| % {
Remove-AccessPermission -ShareName $Name -UserName $_.AccountName -AccessPermission Change
}

$changeAccessValue | % {
# Add change permissions
$changeAccessValue | ForEach-Object {
Set-AccessPermission -ShareName $Name -AccessPermission "Change" -Username $_
}
}

$smbshareAccessValues = Get-SmbShareAccess -Name $Name

# Remove read access
$smbshareAccessValues | Where-Object {$_.AccessControlType -eq 'Allow' -and $_.AccessRight -eq 'Read'} `
| ForEach-Object {
Remove-AccessPermission -ShareName $Name -UserName $_.AccountName -AccessPermission Read
}

if ($ReadAccess -ne $null)
{
# Blow off whatever is in there and replace it with this list
$smbshareAccessValues | ? {$_.AccessControlType -eq 'Allow' -and $_.AccessRight -eq 'Read'} `
| % {
Remove-AccessPermission -ShareName $Name -UserName $_.AccountName -AccessPermission Read
}

$readAccessValue | % {
# Add read access
$readAccessValue | ForEach-Object {
Set-AccessPermission -ShareName $Name -AccessPermission "Read" -Username $_
}
}


$smbshareAccessValues = Get-SmbShareAccess -Name $Name

# Remove full access
$smbshareAccessValues | Where-Object {$_.AccessControlType -eq 'Allow' -and $_.AccessRight -eq 'Full'} `
| ForEach-Object {
Remove-AccessPermission -ShareName $Name -UserName $_.AccountName -AccessPermission Full
}


if ($FullAccess -ne $null)
{
# Blow off whatever is in there and replace it with this list
$smbshareAccessValues | ? {$_.AccessControlType -eq 'Allow' -and $_.AccessRight -eq 'Full'} `
| % {
Remove-AccessPermission -ShareName $Name -UserName $_.AccountName -AccessPermission Full
}

$fullAccessValue | % {
# Add full access
$fullAccessValue | ForEach-Object {
Set-AccessPermission -ShareName $Name -AccessPermission "Full" -Username $_
}
}

$smbshareAccessValues = Get-SmbShareAccess -Name $Name

# Remove explicit deny
$smbshareAccessValues | Where-Object {$_.AccessControlType -eq 'Deny'} `
| ForEach-Object {
Remove-AccessPermission -ShareName $Name -UserName $_.AccountName -AccessPermission No
}


if ($NoAccess -ne $null)
{
# Blow off whatever is in there and replace it with this list
$smbshareAccessValues | ? {$_.AccessControlType -eq 'Deny'} `
| % {
Remove-AccessPermission -ShareName $Name -UserName $_.AccountName -AccessPermission No
}
$noAccessValue | % {
# Add explicit deny
$noAccessValue | ForEach-Object {
Set-AccessPermission -ShareName $Name -AccessPermission "No" -Username $_
}
}
Expand Down Expand Up @@ -314,8 +377,13 @@ function Test-TargetResource
[System.String]
$Ensure = 'Present'
)

# Alter the bound parameters, removing anything that is null or emtpy
$alteredBoundParameters = Set-BoundParameters -boundparameters $PSBoundParameters

$testResult = $false;
$share = Get-TargetResource -Name $Name -Path $Path -ErrorAction SilentlyContinue -ErrorVariable ev
$differences = @()
if ($Ensure -ne "Absent")
{
if ($share.Ensure -eq "Absent")
Expand All @@ -325,9 +393,27 @@ function Test-TargetResource
elseif ($share.Ensure -eq "Present")
{
$Params = 'Name', 'Path', 'Description', 'ChangeAccess', 'ConcurrentUserLimit', 'EncryptData', 'FolderEnumerationMode', 'FullAccess', 'NoAccess', 'ReadAccess', 'Ensure'
if ($PSBoundParameters.Keys.Where({$_ -in $Params}) | ForEach-Object {Compare-Object -ReferenceObject $PSBoundParameters.$_ -DifferenceObject $share.$_})

# Get all matching parameters from alteredBoundParameters that are in Params
$matchingParameters = $alteredBoundParameters.Keys.Where({($_ -in $Params)})

if ($null -ne $matchingParameters)
{
$testResult = $false
foreach ($matchingParameter in $matchingParameters)
{
$differences += Compare-Object -ReferenceObject $alteredBoundParameters[$matchingParameter] -DifferenceObject $share.$matchingParameter #; $differences
}

# Check to see if there is anything in $differences
if (($null -ne $differences) -and ($differences.Length -gt 0))
{
$differences | ForEach-Object {Write-Verbose -Message "$_"}
$testResult = $false
}
else
{
$testResult = $true
}
}
else
{
Expand Down
Loading