|
| 1 | +function Convert-QuarantinePermissionsValue { |
| 2 | + [CmdletBinding(DefaultParameterSetName = 'DecimalValue')] |
| 3 | + param ( |
| 4 | + [Parameter(Mandatory, Position = 0, ParameterSetName = "StringValue")] |
| 5 | + [ValidateNotNullOrEmpty()] |
| 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 |
| 24 | + ) |
| 25 | + |
| 26 | + #Converts string value with EndUserQuarantinePermissions received from Get-QuarantinePolicy |
| 27 | + if (($PSCmdlet.ParameterSetName) -eq "StringValue") { |
| 28 | + try { |
| 29 | + # Remove square brackets and split into lines |
| 30 | + $InputObject = $InputObject.Trim('[', ']') |
| 31 | + $hashtable = @{} |
| 32 | + $InputObject -split "`n" | ForEach-Object { |
| 33 | + $key, $value = $_ -split ":\s*" |
| 34 | + $hashtable[$key.Trim()] = [System.Convert]::ToBoolean($value.Trim()) |
| 35 | + } |
| 36 | + return $hashtable |
| 37 | + } |
| 38 | + catch { |
| 39 | + throw "Convert-QuarantinePermissionsValue: Failed to convert string to hashtable." |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 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") { |
| 45 | + try { |
| 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) |
| 65 | + } |
| 66 | + catch { |
| 67 | + $ErrorMessage = Get-NormalizedError -Message $_.Exception.Message |
| 68 | + throw "Convert-QuarantinePermissionsValue: Failed to convert QuarantinePermissions to QuarantinePermissionsValue. Error: $ErrorMessage" |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments