我需要遍历所有网站集并遍历所有子网站,并仅打印出具有特定模式的子网站:
/sites/clientcode/oppcode6digits
每个客户端网站集都有很多子网站,但我只需要URL最后是6位数的代码。
到目前为止我有这个但是没有工作:
$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()
}
答案 0 :(得分:1)
我将最后一行改为:
if( $web.Url –match $patrn)
{
Write-Host $web.Url
}
答案 1 :(得分:1)
您可以将我建议的正则表达式与您的代码修复一起使用:
foreach ($web in $SPSite.AllWebs) {
$patrn ='^https://mylocalurl/sites/(?:[^/]*/)*\d{6}$'
if( $web.Url –match $patrn) {
Write-Host $web.Url
}
}
正则表达式是
^https://mylocalurl/sites/(?:[^/]*/)*\d{6}$
<强>详情
^
- 行首https://mylocalurl/sites/
- 文字子字符串(?:[^/]*/)*
- 除了/
([^/]*
)之后出现了0 +个字符,其次是/
\d{6}
- 6位数$
- 行尾。