locked
Logical operators RRS feed

  • Question

  • Hello, 

    I am trying to filter Sharepoints sites but im kinda stucked on logical operators, i dont get results i need.

    get-sposite -IncludePersonalSite $true -limit All | 
    where-object { $_.url -like "*-my.sharepoint.com/personal/*" -or $_.Url -like "*sharepoint.com/sites/*" } 

    This way i get all sites I require .. Now i need some filtering. Need to exclude some sites from *sharepoint.com/sites* and all personal sites, except ones with specified owner.  But not sharepoint sites, even if the owner is in the exclusion list - so the exclusion filter for owners affect only personal sites

    $ExcludePersonalSites = "
    owner1@contoso.som
    owner2.contoso.com
    "
    $Filter = $ExcludePersonalSites.Split() | ? { $_ -ne "" }     
    
    $ExcludeSPSites = "
    https://mydomain.sharepoint.com/sites/SharepointSite1
    https://mydomain.sharepoint.com/sites/SharepointSite2
    "
    $Filter2 = $ExcludeSPSites.Split() | ? { $_ -ne "" }     
    

    So, my filter attempts were like this

    $sites | ?{($filter2 -notcontains $_.Url) -xor  ($_.url -like "*-my.sharepoint.com/personal/*" -and $Filter -notcontains $_.owner) } |sort url

    It still does include personal site with owner, who is in exclusion list ($filter).  I tried other operators -and, -or, some combinations.. I know this must be so easy task, I almost feel ashamed to ask you for the advice. 

    Thank you

    Thursday, April 9, 2020 1:41 PM

Answers

  • Try this instead:

    $sites = @(
        [pscustomobject]@{URL="https://mydomain.sharepoint.com/sites/SharepointSite1"; Owner="ralph@contoso.com"},
        [pscustomobject]@{URL="https://mydomain.sharepoint.com/sites/SharepointSite2"; Owner="ralph@contoso.com"},
        [pscustomobject]@{URL="https://mydomain.sharepoint.com/sites/SharepointSite3"; Owner="ralph@contoso.com"}
    
        [pscustomobject]@{URL="XXX-my.sharepoint.com/personal/melvin";Owner="melvin@contoso.com"},
        [pscustomobject]@{URL="XXX-my.sharepoint.com/personal/ralph"; Owner="ralph@contoso.com"},
        [pscustomobject]@{URL="XXX-my.sharepoint.com/personal/george";Owner="george@contoso.com"},
        [pscustomobject]@{URL="XXX-my.sharepoint.com/personal/melvin";Owner="calvin@contoso.com"},
        [pscustomobject]@{URL="XXX-my.sharepoint.com/personal/owner1";Owner="owner1@contoso.com"},
        [pscustomobject]@{URL="XXX-my.sharepoint.com/personal/owner2";Owner="owner2@contoso.com"}
    )
    
    
    $ExcludePersonalSites = "owner1@contoso.com",
        "owner2@contoso.com"
    $ExcludeSPSites = "https://mydomain.sharepoint.com/sites/SharepointSite1",
        "https://mydomain.sharepoint.com/sites/SharepointSite2"
    
    $sites | ForEach-Object {
        if ($_.url -match '^.+sharepoint\.com/sites/' -and $ExcludeSPSites -notcontains $_.url){
            $_
        }
        else{
            if ($_.url -match '^.+sharepoint\.com/personal/' -and $ExcludePersonalSites -contains $_.owner){
                $_
            }
        }
    } | sort url`
    
    

    All your 'personal' sites are never going to be in your "$filter2" so that condition is always going to be true and there's no need to evaluate the second condition.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by Nerváček Thursday, April 9, 2020 9:12 PM
    Thursday, April 9, 2020 8:05 PM

All replies

  • Try this instead:

    $sites = @(
        [pscustomobject]@{URL="https://mydomain.sharepoint.com/sites/SharepointSite1"; Owner="ralph@contoso.com"},
        [pscustomobject]@{URL="https://mydomain.sharepoint.com/sites/SharepointSite2"; Owner="ralph@contoso.com"},
        [pscustomobject]@{URL="https://mydomain.sharepoint.com/sites/SharepointSite3"; Owner="ralph@contoso.com"}
    
        [pscustomobject]@{URL="XXX-my.sharepoint.com/personal/melvin";Owner="melvin@contoso.com"},
        [pscustomobject]@{URL="XXX-my.sharepoint.com/personal/ralph"; Owner="ralph@contoso.com"},
        [pscustomobject]@{URL="XXX-my.sharepoint.com/personal/george";Owner="george@contoso.com"},
        [pscustomobject]@{URL="XXX-my.sharepoint.com/personal/melvin";Owner="calvin@contoso.com"},
        [pscustomobject]@{URL="XXX-my.sharepoint.com/personal/owner1";Owner="owner1@contoso.com"},
        [pscustomobject]@{URL="XXX-my.sharepoint.com/personal/owner2";Owner="owner2@contoso.com"}
    )
    
    
    $ExcludePersonalSites = "owner1@contoso.com",
        "owner2@contoso.com"
    $ExcludeSPSites = "https://mydomain.sharepoint.com/sites/SharepointSite1",
        "https://mydomain.sharepoint.com/sites/SharepointSite2"
    
    $sites | ForEach-Object {
        if ($_.url -match '^.+sharepoint\.com/sites/' -and $ExcludeSPSites -notcontains $_.url){
            $_
        }
        else{
            if ($_.url -match '^.+sharepoint\.com/personal/' -and $ExcludePersonalSites -contains $_.owner){
                $_
            }
        }
    } | sort url`
    
    

    All your 'personal' sites are never going to be in your "$filter2" so that condition is always going to be true and there's no need to evaluate the second condition.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by Nerváček Thursday, April 9, 2020 9:12 PM
    Thursday, April 9, 2020 8:05 PM
  • Thank you Rich, 

    it works (why didnt I think about IF/Else .. experience i guess).  You had just had a wrong operator (or i didnt express myself clear) 

    if ($_.url -match '^.+sharepoint\.com/personal/' -and $ExcludePersonalSites -contains $_.owner)

    .. should be -notcontains

    Then everyhing worked as expected. Thank you

    Thursday, April 9, 2020 9:15 PM
  • Oops! Sorry! That was me not paying attention, not you being unclear.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Thursday, April 9, 2020 9:19 PM