Luis Valencia
Luis Valencia

Reputation: 34028

powershell, substring. replace absolute urls with relative urls

I have a top navigation bar which was built using absolute urls, instead of relative urls, the problem is, if I do backup restore of that site collection from PROD to QA, then the urls in QA point to production.

So, I am trying to create a script to replace absolute urls, with relative Urls.

The specific question would be:. With the know Url Root, and a lot of urls, how can I remove the first from the second?

Example: If I have www.mydomain.com in a variable. And then in a second variable I have www.mydomain.com/sites/site1 I want it to return only /sites/site1

This is what I got so far

 $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
                #    }
                #}
            }
        }
    }
}

On the screenshot below the old url is:

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

The new Url should be:

/Pages/clientinvoices.aspx

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

Upvotes: 0

Views: 1398

Answers (1)

mjolinor
mjolinor

Reputation: 68321

Something like this?

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

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

$var2

/Pages/clientinvoices.aspx

Upvotes: 2

Related Questions