1

I need to iterate through all site collections and through all subsites, and print out only the subsites with a certain pattern:

/sites/clientcode/oppcode6digits

Each client site collection has many subsites, but I only need the ones which URL is at the end 6-digit code.

I have this so far but not working:

$SPWebApp = Get-SPWebApplication "https://mylocalurl.com"

foreach ($SPSite in $SPWebApp.Sites) {
    if ($SPSite -ne $null -and $SPSite.Url -notmatch "billing" -and $SPSite.Url -notmatch "administrativedocuments" -and $SPSite.Url -notmatch "documentation" -and $SPSite.Url -notmatch "help" -and $SPSite.Url -notmatch "marketing" -and $SPSite.Url -and $SPSite.Url -notmatch "search" -and $SPSite.Url -ne $rootDMS  ) {
        foreach ($web in $SPSite.AllWebs) {
            $regex = ‘\b[0-9]{6}\b’  
            $patrn = "https://mylocalurl/sites/*/$regex"

            Write-Host $web.Url | select-string -Pattern $patrn
        }
    }
    $SPSite.Dispose()
}
8
  • Try "https://mylocalurl/sites/.*/\d{6}`$" Commented May 9, 2018 at 8:38
  • still doesnt work Commented May 9, 2018 at 8:41
  • Well, the pattern matches https://mylocalurl/sites/abc/123456, so I believe there is a problem not related to regex. Maybe there are 6 digits right after sites? Then try "https://mylocalurl/sites/(?:[^/]*/)*\d{6}$" Commented May 9, 2018 at 8:49
  • this is the result, it should match the ones with the arrow screencast.com/t/FqDnNuG2m3nm Commented May 9, 2018 at 8:59
  • See regex101.com/r/oIYWDO/1, the pattern I shared last seems to do what you want. Commented May 9, 2018 at 9:01

2 Answers 2

1

You may use the regex I suggested together with your code fix:

foreach ($web in $SPSite.AllWebs) {
    $patrn ='^https://mylocalurl/sites/(?:[^/]*/)*\d{6}$'
    if( $web.Url –match  $patrn) {
        Write-Host $web.Url
    }
}

The regex is

^https://mylocalurl/sites/(?:[^/]*/)*\d{6}$

See the regex demo online.

Details

  • ^ - start of line
  • https://mylocalurl/sites/ - a literal substring
  • (?:[^/]*/)* - 0+ occurrences of 0+ chars other than / ([^/]*) followed with /
  • \d{6} - 6 digits
  • $ - end of the line.
Sign up to request clarification or add additional context in comments.

Comments

1

I changed the last line to:

  if( $web.Url –match  $patrn)
                {
                    Write-Host $web.Url
                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.