1
1
function Convert-QuarantinePermissionsValue {
2
+ [CmdletBinding (DefaultParameterSetName = ' DecimalValue' )]
2
3
param (
3
- [Parameter (Mandatory = $true , Position = 0 )]
4
+ [Parameter (Mandatory , Position = 0 , ParameterSetName = " StringValue " )]
4
5
[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
10
24
)
11
25
12
26
# Converts string value with EndUserQuarantinePermissions received from Get-QuarantinePolicy
13
- if ($InputObject -is [ String ] ) {
27
+ if (( $PSCmdlet .ParameterSetName ) -eq " StringValue " ) {
14
28
try {
15
29
# Remove square brackets and split into lines
16
30
$InputObject = $InputObject.Trim (' [' , ' ]' )
@@ -22,28 +36,36 @@ function Convert-QuarantinePermissionsValue {
22
36
return $hashtable
23
37
}
24
38
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."
27
40
}
28
41
}
29
42
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 " ) {
32
45
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 )
43
65
}
44
66
catch {
45
67
$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 "
47
69
}
48
70
}
49
71
}
0 commit comments