2013-05-03 39 views
2

我们正在更改5个网站共享网站上的网址,以便它们仅响应HTTPS和FQDN请求。 很多我们的用户在我们的收藏夹中有“内部”链接,当我们更改URL时,这些链接将会中断。我需要更改某些“收藏夹”链接中的网址

我需要一种方法来打开每个'收藏',看看是否有任何链接指向旧的URL,然后将其更改为新的。

这就是我想要做的(没有错误出现,但链接不会被修改):

# Set folder variable 
    $folder = $env:HOMEPATH + '\' + 'favorites'            #' 

    # Backup the folder 
    Copy-Item $folder $env:homepath\favorites1 -recurse 

    # set the stringtofind variables 
    $stringToFind1 = "http://wss1/" 
$stringToFind2 = "http://iwds/" 
$stringToFind3 = "http://webshare/" 
$stringToFind4 = "http://mysites/" 
$stringToFind5 = "http://eforms/" 

    # Set the stringtoplace variables 
$stringToPlace1 = "https://wss1.FQDN.com" 
$stringToPlace2 = "https://iwds.FQDN.com" 
$stringToPlace3 = "https://webshare.FQDN.com" 
$stringToPlace4 = "https://mysites.FQDN.com" 
$stringToPlace5 = "https://eforms.FQDN.com" 

    # Execute the content changes 
gci $folder\*.url -recurse | foreach-object { (Get-Content $_) | ForEach-Object { $_ -replace $stringToFind1, $stringToPlace1 } | Set-Content $_ } 
gci $folder\*.url -recurse | foreach-object { (Get-Content $_) | ForEach-Object { $_ -replace $stringToFind2, $stringToPlace2 } | Set-Content $_ } 
gci $folder\*.url -recurse | foreach-object { (Get-Content $_) | ForEach-Object { $_ -replace $stringToFind3, $stringToPlace3 } | Set-Content $_ } 
gci $folder\*.url -recurse | foreach-object { (Get-Content $_) | ForEach-Object { $_ -replace $stringToFind4, $stringToPlace4 } | Set-Content $_ } 
gci $folder\*.url -recurse | foreach-object { (Get-Content $_) | ForEach-Object { $_ -replace $stringToFind5, $stringToPlace5 } | Set-Content $_ } 

回答

1

您可以使用COM操作快捷方式文件。使用Shell classCreateShortcut method读取快捷方式文件:

$shell = New-Object -ComObject 'WScript.Shell'; 
$shortcut = $shell.CreateShortcut($shortcutPath); 

然后你就可以读取和修改TargetPath property,并使用提交更改快捷方式文件的Save method

$oldTargetPath = $shortcut.TargetPath; 
Write-Host "Shortcut currently points to $oldTargetPath"; 
$shortcut.TargetPath = $newTargetPath; 
$shortcut.Save(); 
+0

非常方便;只是为了明确说明:这种方法可以读取/更新/创建常规快捷方式文件('* .lnk')以及URL快捷方式文件('* .url')(收藏夹)。 – mklement0 2017-02-17 21:28:43

0

我可能会通过复制已经创建的链接的文件夹并将当前的链接删除,以便将来不会有任何麻烦。

但是,下面是如何使用Powershell使用您的方法来完成此操作。下面的代码使用正则表达式来完成这项工作。它应该满足你的要求。

$favourites_path = [System.Environment]::GetFolderPath('Favorites') 

Copy-Item $favourites_path "$favourites_path`1" -Recurse -Force 

$favourites = Get-ChildItem($favourites_path) -Recurse -Filter *.url 

$reg_path = 'HKCU:\Software\Microsoft\Internet Explorer\Main\' 

$start_page = (Get-Itemproperty -Path $reg_path).'Start Page' 

foreach ($favourite in $favourites) { 

    $shortcut = (New-Object -ComObject 'WScript.Shell').CreateShortCut($favourite.FullName) 

    $new_path = switch -Regex ($shortcut.TargetPath) { 

     ('http://wss1/(.*)') { "https://wss1.FQDN.com/$($Matches[1])"; break } 
     ('http://iwds/(.*)') { "https://iwds.FQDN.com/$($Matches[1])"; break } 
     ('http://webshare/(.*)') { "https://webshare.FQDN.com/$($Matches[1])"; break } 
     ('http://mysites/(.*)') { "https://mysites.FQDN.com/$($Matches[1])"; break } 
     ('http://eforms/(.*)') { "https://eforms.FQDN.com/$($Matches[1])"; break } 
     default { $_ } 
    } 

    $shortcut.TargetPath = $new_path 

    # Set homepage 
    if ($shortcut.TargetPath -eq $start_page) { Set-ItemProperty -Path $reg_path -Name 'Start Page' -Value $new_path } 

    $shortcut.Save() 
} 

我已经添加了代码来帮助您设置主页。

+0

经过进一步测试,我发现其余的链接丢失。也就是说,基本网址右侧的任何内容都将被删除。 I.E. http://eforms/default.aspx现在是https://eforms.fqdn.com。我需要其余的网址保持不变。 – user2348167 2013-05-07 18:09:30

1

的作品最终代码:

$favourites_path = [System.Environment]::GetFolderPath('Favorites') 
Copy-Item $favourites_path "$favourites_path`1" -Recurse 
$favourites = Get-ChildItem $favourites_path -Recurse -Filter *.url 
foreach ($favourite in $favourites) { 

$shortcut = (New-Object -ComObject 'WScript.Shell').CreateShortCut($favourite.FullName) 
$newpath=switch -Wildcard ($shortcut.TargetPath) 
{ 
    'http://wss1/*'   { $_ -replace 'http://wss1', 'https://wss1.vmc.com'} 
    'http://iwds/*'   { $_ -replace 'http://iwds', 'https://iwds.vmc.com'} 
    'http://webshare/*'  { $_ -replace 'http://webshare', 'https://webshare.vmc.com'} 
    'http://mysites/*'  { $_ -replace 'http://mysites', 'https://mysites.vmc.com'} 
    'http://eforms/*'   { $_ -replace 'http://eforms', 'https://eforms.vmc.com'} 
    default     { $_ } 
} 

$shortcut.TargetPath=$newpath 
$shortcut.Save() 
+0

为了帮助未来的读者,请自我接受这个答案,表明这是一个有效的解决方案。 – mklement0 2017-02-17 21:26:19

0

问题与第一个脚本是,-Path需要的Set-Content命令。这是我写的一个脚本,可以工作。您可以根据需要进行调整。这是相当详细的,但嘿,我正在学习。我打电话给脚本FindURLs.ps1 注意:第一个版本改变了CONTENT,但不是URL argh ..

param (
    [string]$string = "//hpenterprise", 
    [string]$stringtoreplace = "//hpe", 
    [string]$Path = "*.url", 
    [switch]$Recurse = $false, 
    [switch]$update = $false, 
    [switch]$help = $false 
) 
$myargs = @{ 
    Path = $Path 
    Recurse = $Recurse 
} 
#echo "$string $stringtoreplace $Path $Recurse $update" 
#echo $myargs 
#$help=$true; 
if ($help) { 
    echo " 
FindURLs Finds and Optionally Replaces URLs by default 
string = $string The string to find. 
stringtoreplace = $stringtoreplace The string to replace the found string 
Path = $Path  Path to the urls 
Recurse = $Recurse Recurse option. i.e. start in this directory and all recursively find URLs in sub-directories 
Update = $update Update the string. This option must be used to actually update the URLs. 

i.e. 
FindURLs.ps1     will start in the current directory and check the URLs for $string and show what the new string will look like. 
FindURLs.ps1 -Recurse   will find the strings and show new strings recursively. 
FindURLs.ps1 -Recurse -Update will find the strings and update them recursively. 

You can also override string, stringtoreplace and path. 
For example -Path <new_path> -string //hpebrokenlinks -stringtoreplace //hpenewlinks 
You may need to use quotes around <new_path> if the path contains any spaces. 
Note: PowerShell is Case Insensitive unlike other shells. 
" 
    return; 
} 

Get-Childitem @myargs | ForEach-Object { 
    $urlpath = $_.FullName 
    echo "Examining $urlpath" 
    $shortcut = (New-Object -ComObject 'WScript.Shell').CreateShortCut($urlpath) 
    if ($shortcut.TargetPath -Match $string) { 
     echo ("Changing " + $shortcut.TargetPath) 
     $shortcut.TargetPath = ForEach-Object { $shortcut.TargetPath -replace $string, $stringtoreplace } 
     echo ("To   " + $shortcut.TargetPath) 
     if ($update) { 
      #Set-Content -Path "$path" "$new_content" Does not Work! Even though the code says it works. Content changes but the link is the same? 
      #Had to change to the shortcut method. 

      $shortcut.Save(); 
      echo ("Updated To " + $shortcut.TargetPath) 
     } else { 
      echo "-Update Option not set. Test Only No Change to URL" 
     } 
    } else { 
     echo ("No Change " + $shortcut.TargetPath) 
    } 
    echo "" 
} 
相关问题