2

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

我需要一种方法来打开每个“收藏夹”并查看是否有任何链接指向旧 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 $_ }
4

4 回答 4

1

您可以使用 COM 来操作快捷方式文件。使用CreateShortcut方法读取快捷方式文件:Shell

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

然后您可以读取和修改TargetPath属性,并使用以下Save方法将更改提交到快捷方式文件:

$oldTargetPath = $shortcut.TargetPath;
Write-Host "Shortcut currently points to $oldTargetPath";
$shortcut.TargetPath = $newTargetPath;
$shortcut.Save();
于 2013-05-03T20:26:51.107 回答
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()
于 2013-05-08T15:32:50.797 回答
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()
}

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

于 2013-05-05T19:05:14.497 回答
0

第一个脚本的问题在于 Set-Content Cmdlet 需要 -Path。这是我写的一个有效的脚本。您可以根据需要进行调整。它相当冗长,但嘿,我正在学习。我调用了脚本 FindURLs.ps1 注意:第一个版本更改了内容,但没有更改 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 ""
}
于 2017-02-17T20:56:35.223 回答