Search code examples
powershellcontains

Powershell contains, check with variable not working


i'm trying for several hours, but don't get why this is not working...

  1. loading a CSV with multiple site entries:

https://tenant.sharepoint/sites/SiteA

https://tenant.sharepoint/sites/SiteB

https://tenant.sharepoint/sites/SiteC

https://tenant.sharepoint/sites/SiteD

  1. Get all SharePoint Sites

  2. Check if a SharePoint site is on CSV list

The Problem is, i always get "False" as result. What is the issue?

$exceptionSites = Import-Csv -Path "Document.csv"
$siteFilter = $exceptionSites.ExceptSite

$sites = Get-SPOSite -Limit ALL | select url
   foreach ($site in $sites) 
      {

          if ($siteFilter -contains $site)
              {

                  Write-Output "Site is on list"
              }
          else 
              {         
                  Write-Output "Site is not on list"
              }
    }   

when i change if ($siteFilter -contains "https://tenant.sharepoint/sites/SiteA") it's working....


Solution

  • Take a look at $Sites in your code. You'll see it still has the URL property.

    Try this:

    $sites = Get-SPOSite -Limit ALL | Select-Object -ExpandProperty url
    

    Or try this:

              if ($siteFilter -contains $site.url)
    

    But don't do both.