Skip to content

Commit ea12858

Browse files
committed
Modified to calculate decimalValue from binary
all values are now parameters and use parameterSetName
1 parent bf388a7 commit ea12858

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed
Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
function Convert-QuarantinePermissionsValue {
2+
[CmdletBinding(DefaultParameterSetName = 'DecimalValue')]
23
param (
3-
[Parameter(Mandatory = $true, Position=0)]
4+
[Parameter(Mandatory, Position = 0, ParameterSetName = "StringValue")]
45
[ValidateNotNullOrEmpty()]
5-
[ValidateScript(
6-
{$_ -is [String] ? $true : $_ -is [Hashtable] ? $true : $false},
7-
ErrorMessage = "Input must be a string or hashtable."
8-
)]
9-
$InputObject
6+
[string]$InputObject,
7+
8+
[Parameter(Position = 0, ParameterSetName = "DecimalValue")]
9+
[int]$PermissionToViewHeader = 0,
10+
[Parameter(Position = 1, ParameterSetName = "DecimalValue")]
11+
[int]$PermissionToDownload = 0,
12+
[Parameter(Mandatory, Position = 2, ParameterSetName = "DecimalValue")]
13+
[int]$PermissionToAllowSender,
14+
[Parameter(Mandatory, Position = 3, ParameterSetName = "DecimalValue")]
15+
[int]$PermissionToBlockSender,
16+
[Parameter(Mandatory, Position = 4, ParameterSetName = "DecimalValue")]
17+
[int]$PermissionToRequestRelease,
18+
[Parameter(Mandatory, Position = 5, ParameterSetName = "DecimalValue")]
19+
[int]$PermissionToRelease,
20+
[Parameter(Mandatory, Position = 6, ParameterSetName = "DecimalValue")]
21+
[int]$PermissionToPreview,
22+
[Parameter(Mandatory, Position = 7, ParameterSetName = "DecimalValue")]
23+
[int]$PermissionToDelete
1024
)
1125

1226
#Converts string value with EndUserQuarantinePermissions received from Get-QuarantinePolicy
13-
if ($InputObject -is [String]) {
27+
if (($PSCmdlet.ParameterSetName) -eq "StringValue") {
1428
try {
1529
# Remove square brackets and split into lines
1630
$InputObject = $InputObject.Trim('[', ']')
@@ -22,28 +36,36 @@ function Convert-QuarantinePermissionsValue {
2236
return $hashtable
2337
}
2438
catch {
25-
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
26-
throw "Convert-QuarantinePermissionsValue: Failed to convert string to hashtable. Error: $ErrorMessage"
39+
throw "Convert-QuarantinePermissionsValue: Failed to convert string to hashtable."
2740
}
2841
}
2942

30-
#Converts hashtable with selected end user quarantine permissions to decimal value used by EndUserQuarantinePermissionsValue property in New-QuarantinePolicy and Set-QuarantinePolicy
31-
elseif ($InputObject -is [Hashtable]) {
43+
#Converts selected end user quarantine permissions to decimal value used by EndUserQuarantinePermissionsValue property in New-QuarantinePolicy and Set-QuarantinePolicy
44+
elseif (($PSCmdlet.ParameterSetName) -eq "DecimalValue") {
3245
try {
33-
$EndUserQuarantinePermissionsValue = 0
34-
$EndUserQuarantinePermissionsValue += ([int]$InputObject.PermissionToViewHeader ?? 0 ) * 128
35-
$EndUserQuarantinePermissionsValue += ([int]$InputObject.PermissionToDownload ?? 0 ) * 64
36-
$EndUserQuarantinePermissionsValue += [int]$InputObject.PermissionToAllowSender * 32
37-
$EndUserQuarantinePermissionsValue += [int]$InputObject.PermissionToBlockSender * 16
38-
$EndUserQuarantinePermissionsValue += [int]$InputObject.PermissionToRequestRelease * 8
39-
$EndUserQuarantinePermissionsValue += [int]$InputObject.PermissionToRelease * 4
40-
$EndUserQuarantinePermissionsValue += [int]$InputObject.PermissionToPreview * 2
41-
$EndUserQuarantinePermissionsValue += [int]$InputObject.PermissionToDelete * 1
42-
return $EndUserQuarantinePermissionsValue
46+
# both PermissionToRequestRelease and PermissionToRelease cannot be set to true at the same time
47+
if($PermissionToRequestRelease -eq 1 -and $PermissionToRelease -eq 1) {
48+
throw "PermissionToRequestRelease and PermissionToRelease cannot both be set to true."
49+
}
50+
51+
# Convert each permission to a binary string
52+
$BinaryValue = [string]@(
53+
$PermissionToViewHeader,
54+
$PermissionToDownload,
55+
$PermissionToAllowSender,
56+
$PermissionToBlockSender,
57+
$PermissionToRequestRelease,
58+
$PermissionToRelease,
59+
$PermissionToPreview,
60+
$PermissionToDelete
61+
) -replace '\s',''
62+
63+
# Convert the binary string to an Decimal value
64+
return [convert]::ToInt32($BinaryValue,2)
4365
}
4466
catch {
4567
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
46-
throw "Convert-QuarantinePermissionsValue: Failed to convert hashtable to QuarantinePermissionsValue. Error: $ErrorMessage"
68+
throw "Convert-QuarantinePermissionsValue: Failed to convert QuarantinePermissions to QuarantinePermissionsValue. Error: $ErrorMessage"
4769
}
4870
}
4971
}

0 commit comments

Comments
 (0)