0

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

2
  • You have 4 x's in the url in Var1, and 5 in the one in Var2, so it's not going to match. Commented Oct 28, 2013 at 14:16
  • thats an example only Commented Oct 28, 2013 at 14:20

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

The code just ouputs the trimmed string. I changed the code to assign the result back to var2, to match your usage.
please check at my updated question with an screenshot, maybe I was not clear enough?
I updated the script to match your example, and it appears to do what you're asking for.

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.