Skip to content

Commit 5ba83db

Browse files
committed
Throws error message if using wrong parameters
1 parent 1558539 commit 5ba83db

File tree

3 files changed

+338
-164
lines changed

3 files changed

+338
-164
lines changed

Modules/ComputerManagementDsc/DSCResources/MSFT_SmbShare/MSFT_SmbShare.psm1

+91-13
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ function Set-TargetResource
245245
$Ensure = 'Present'
246246
)
247247

248+
Assert-AccessPermissionParameters @PSBoundParameters
249+
248250
<#
249251
Copy the $PSBoundParameters to a new hash table, so we have the
250252
original intact.
@@ -332,19 +334,6 @@ function Set-TargetResource
332334
}
333335

334336
New-SmbShare @smbShareParameters -ErrorAction 'Stop'
335-
336-
<#
337-
The group 'Everyone' is automatically given read access by
338-
the cmdlet New-SmbShare, if ReadAccess is set to @().
339-
340-
if ReadAccess was specified in the configuration, and if
341-
ReadAccess is set to @(), then this removes that access
342-
permission,
343-
#>
344-
if ($PSBoundParameters.ContainsKey('ReadAccess') -and -not $ReadAccess)
345-
{
346-
Remove-SmbShareAccessPermission -Name $Name -ReadAccess $ReadAccess
347-
}
348337
}
349338
}
350339
}
@@ -460,6 +449,8 @@ function Test-TargetResource
460449
$Ensure = 'Present'
461450
)
462451

452+
Assert-AccessPermissionParameters @PSBoundParameters
453+
463454
Write-Verbose -Message ($script:localizedData.TestTargetResourceMessage -f $Name)
464455

465456
$testTargetResourceResult = $false
@@ -777,3 +768,90 @@ function Add-SmbShareAccessPermission
777768
}
778769
}
779770
}
771+
772+
<#
773+
.SYNOPSIS
774+
Assert that not only empty collections are passed in the
775+
respectively access permission collections (FullAccess,
776+
ChangeAccess, ReadAccess, and NoAccess).
777+
778+
.PARAMETER Name
779+
The name of the SMB share to add access permission to.
780+
781+
.PARAMETER FullAccess
782+
A string collection of account names that should have full access
783+
permission. The accounts in this collection will be added to the
784+
SMB share.
785+
786+
.PARAMETER ChangeAccess
787+
A string collection of account names that should have change access
788+
permission. The accounts in this collection will be added to the
789+
SMB share.
790+
791+
.PARAMETER ReadAccess
792+
A string collection of account names that should have read access
793+
permission. The accounts in this collection will be added to the
794+
SMB share.
795+
796+
.PARAMETER NoAccess
797+
A string collection of account names that should be denied access
798+
to the SMB share. The accounts in this collection will be added to
799+
the SMB share.
800+
801+
.PARAMETER RemainingParameters
802+
Container for the rest of the potentially splatted parameters from
803+
the $PSBoundParameters object.
804+
805+
.NOTES
806+
The group 'Everyone' is automatically given read access by
807+
the cmdlet New-SmbShare if all access permission parameters
808+
(FullAccess, ChangeAccess, ReadAccess, NoAccess) is set to @().
809+
For that reason we are need either none of the parameters, or
810+
at least one to specify an account.
811+
812+
#>
813+
function Assert-AccessPermissionParameters
814+
{
815+
param
816+
(
817+
[Parameter()]
818+
[System.String[]]
819+
$FullAccess,
820+
821+
[Parameter()]
822+
[System.String[]]
823+
$ChangeAccess,
824+
825+
[Parameter()]
826+
[System.String[]]
827+
$ReadAccess,
828+
829+
[Parameter()]
830+
[System.String[]]
831+
$NoAccess,
832+
833+
[Parameter(ValueFromRemainingArguments)]
834+
[System.Collections.Generic.List`1[System.Object]]
835+
$RemainingParameters
836+
)
837+
838+
<#
839+
First check if ReadAccess is monitored (part of the configuration).
840+
If it is not monitored, then we don't need to worry if Everyone is
841+
added.
842+
#>
843+
if ($PSBoundParameters.ContainsKey('ReadAccess') -and -not $ReadAccess)
844+
{
845+
$fullAccessHasNoMembers = $PSBoundParameters.ContainsKey('FullAccess') -and -not $FullAccess
846+
$changeAccessHasNoMembers = $PSBoundParameters.ContainsKey('ChangeAccess') -and -not $ChangeAccess
847+
$noAccessHasNoMembers = $PSBoundParameters.ContainsKey('NoAccess') -and -not $NoAccess
848+
<#
849+
If ReadAccess should have no members, then we need at least one
850+
member in one of the other access permission collections.
851+
#>
852+
if ($fullAccessHasNoMembers -and $changeAccessHasNoMembers -and $noAccessHasNoMembers)
853+
{
854+
New-InvalidArgumentException -Message $script:localizedData.WrongAccessParameters -ArgumentName 'FullAccess, ChangeAccess, ReadAccess, NoAccess'
855+
}
856+
}
857+
}

Modules/ComputerManagementDsc/DSCResources/MSFT_SmbShare/en-US/MSFT_SmbShare.strings.psd1

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ ConvertFrom-StringData @'
1414
UnblockAccess = Revoking denied permission for account '{0}' on the SMB share with the name '{1}'.
1515
GrantAccess = Granting '{0}' permission for account '{1}' on the SMB share with the name '{2}'.
1616
DenyAccess = Denying permission for account '{0}' on the SMB share with the name '{1}'.
17+
WrongAccessParameters = Not allowed to have all access permission parameters set to empty collections. Must either remove the access permission parameters completely, or add at least one member to one of the access permission parameters.
1718
'@

0 commit comments

Comments
 (0)