powershell,substring。用相对网址替换绝对网址

时间:2013-10-28 13:51:08

标签: powershell

我有一个使用绝对网址而不是相对网址构建的顶部导航栏,问题是,如果我将该网站集的备份从PROD备份到QA,那么QA中的网址指向生产。

所以,我正在尝试使用相对Urls创建一个替换绝对URL的脚本。

具体问题是:知道Url Root和很多网址后,如何从第二个删除第一个?

实施例: 如果我在变量中有www.mydomain.com。 然后在第二个变量中我有www.mydomain.com/sites/site1 我希望它只返回/ sites / site1

这是我到目前为止所得到的

 $var1 = "https://xxxx.com"
$var2 = "https://xxxxx.com/sites/10024960/"
$rootSiteCollection = Get-SPSite $siteCollectionUrl
if ($rootSiteCollection -ne $null)
{
     if($Site.RootWeb.AllProperties["WebTemplate"] -eq "Client")
     {
        $rootWeb = $rootSiteCollection.RootWeb
        $navigationNodes = $rootWeb.Navigation.TopNavigationBar 
        if ($navigationNodes -ne $null)
        {
            foreach($navNode in $navigationNodes)
            {
                write-host 'Old Url: '  $navNode.Url

                $regex = "^$([regex]::Escape($var1))(.+)"
                $var2 -replace $regex,'$1'

                write-host 'New Url: '  $var2

                #if($navNode.Children.Count -ge 0)
                #{
                #    foreach($navNodeChild in $navNode.Children)
                #    {
                #        write-host 'Old Url'
                #        write-host $navNode.Url
                #    }
                #}
            }
        }
    }
}

在下面的屏幕截图中,旧网址为:

https://xxx.com/Pages/clientinvoices.aspx

新的网址应为:

/Pages/clientinvoices.aspx

http://screencast.com/t/73Uof4je

1 个答案:

答案 0 :(得分:2)

这样的东西?

$$var1 = 'https://example.com'
$var2 = 'https://example.com/Pages/clientinvoices.aspx'

$regex = "$([regex]::Escape($var1))(.+)"
$var2 = $var2 -replace $regex,'$1'

$var2

/Pages/clientinvoices.aspx

相关问题