1- # Monkey365 - the PowerShell Cloud Security Tool for Azure and Microsoft 365 (copyright 2022) by Juan Garrido
1+ # Monkey365 - the PowerShell Cloud Security Tool for Azure and Microsoft 365 (copyright 2022) by Juan Garrido
22#
33# Licensed under the Apache License, Version 2.0 (the "License");
44# you may not use this file except in compliance with the License.
@@ -47,38 +47,61 @@ function ConvertTo-Query{
4747 $finalquery = [System.Text.StringBuilder ]::new()
4848 Foreach ($query in @ ($InputObject )){
4949 If ($null -ne $query -and $null -ne $query.Psobject.Properties.Item (' filter' ) -and $null -ne $query.filter ){
50- # $filters = @()
5150 $filters = [System.Collections.Generic.List ` 1 [String ]]::new()
51+ # Get ConnectOperator
52+ $connectOperator = $query | Select-Object - ExpandProperty connectOperator - ErrorAction Ignore
5253 # Check if operator
5354 $operator = $query | Select-Object - ExpandProperty operator - ErrorAction Ignore
54- # Check if connect operator
55- $connectOperator = $query | Select-Object - ExpandProperty connectOperator - ErrorAction Ignore
56- foreach ($filter in $query.filter ){
55+ ForEach ($filter in $query.filter ){
5756 $newFilter = $filter | Resolve-Filter
5857 If ($newFilter ){
59- # $filters+=$newFilter
60- [void ]$filters.Add ($newFilter );
58+ # Check if connectOperator is present
59+ $connectOp = $filter | Select-Object - ExpandProperty connectOperator - ErrorAction Ignore
60+ If ($null -ne $connectOp -and $null -ne (Get-LogicalOperator $connectOp )){
61+ $q = (" -{0} {1}" -f $connectOp , $newFilter );
62+ [void ]$filters.Add ($q );
63+ }
64+ ElseIf ($filter.conditions.Count -gt 1 ){
65+ $q = (" ({0})" -f $newFilter );
66+ [void ]$filters.Add ($q );
67+ }
68+ Else {
69+ [void ]$filters.Add ($newFilter );
70+ }
6171 }
6272 }
63- If (@ ( $filters ) .Count -eq 1 -and $null -eq $operator ){
73+ If ($filters.Count -eq 1 ){
6474 $q = (@ ($filters ) -join ' ' )
6575 If ($null -ne $connectOperator -and $null -ne (Get-LogicalOperator $connectOperator )){
66- $q = (" -{0} ({1})" -f $connectOperator , $q )
76+ $q = (" -{0} ({1})" -f $connectOperator , $q )
6777 }
6878 If ($q.Length -gt 0 ){
69- # $finalquery = ("{0} {1}" -f $finalquery,$q)
7079 [void ]$finalquery.Append ($q );
7180 }
7281 }
73- Elseif ($null -ne $operator -and $null -ne (Get-LogicalOperator $operator )){
74- # $q = (@($filters).ForEach({"($_)"}) -join (' -{0} ' -f $operator))
75- $q = $filters -join (' -{0} ' -f $operator )
76- # Check if connect operator
77- If ($null -ne $connectOperator -and $null -ne (Get-LogicalOperator $connectOperator )){
78- $q = (" -{0} ({1})" -f $connectOperator , $q )
82+ ElseIf ($filters.Count -gt 1 ){
83+ $q = [System.String ]::Empty;
84+ If ($null -ne $connectOperator -and $null -ne (Get-LogicalOperator $connectOperator ) -and $null -ne $operator -and $null -ne (Get-LogicalOperator $operator )){
85+ $q = (" ({0})" -f (@ ($filters -join (' -{0} ' -f $operator ))))
86+ $q = (" -{0} {1}" -f $connectOperator , $q )
87+ }
88+ ElseIf ($null -ne $operator -and $null -ne (Get-LogicalOperator $operator )){
89+ If (@ ($InputObject ).Count -gt 1 ){
90+ $q = (" ({0})" -f (@ ($filters -join (' -{0} ' -f $operator ))))
91+ }
92+ Else {
93+ $q = (" {0}" -f (@ ($filters -join (' -{0} ' -f $operator ))))
94+ }
95+ }
96+ Else {
97+ If ($null -ne (Get-Variable - Name queryIsOpen - ErrorAction Ignore) -and $queryIsOpen ){
98+ $q = (" {0}" -f (@ ($filters ) -join ' ' ))
99+ }
100+ Else {
101+ $q = (" ({0})" -f (@ ($filters ) -join ' ' ))
102+ }
79103 }
80104 If ($q.Length -gt 0 ){
81- # $finalquery = ("{0} {1}" -f $finalquery,$q)
82105 [void ]$finalquery.Append ($q );
83106 }
84107 }
@@ -94,6 +117,9 @@ function ConvertTo-Query{
94117 If ($null -ne (Get-Variable - Name queryIsOpen - ErrorAction Ignore) -and $queryIsOpen ){
95118 # $finalquery = ("{0}}})" -f $finalquery,$q)
96119 [void ]$finalquery.Append (' })' );
120+ If ($atLeast ){
121+ [void ]$finalquery.Append ((' .Count -gt {0}' -f $atLeast ));
122+ }
97123 Remove-Variable - Name queryIsOpen - Scope Script - Force - ErrorAction Ignore
98124 }
99125 If ($finalquery.Length -gt 0 ){
0 commit comments