2013-10-28 83 views
0

我有一個使用絕對網址而不是相對網址構建的頂部導航欄,問題是,如果我從PROD到QA備份還原該網站集,則QA點中的網址到生產。powershell,子串。替換絕對網址與相關網址

所以,我正在嘗試創建一個腳本來替換絕對網址,以及相關的網址。

具體的問題是:。有了認識的Url Root和很多網址,我怎麼能從第二個中刪除第一個呢?

例如: 如果我在www.mydomain.com中有一個變量。 然後在第二個變量我已經www.mydomain.com/sites/site1 我希望它僅僅返回/網站/ 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

新的URL應該是:

/頁/客戶端invoices.aspx

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

+0

你有4層的在VAR1的URL,5在一個在VAR2,所以它不會匹配。 – mjolinor

+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

+0

不工作。 –

+0

1分鐘粘貼我的更新代碼 –

+0

該代碼只是輸出修剪的字符串。我更改了代碼以將結果返回給var2,以匹配您的使用情況。 – mjolinor