我们正在更改5个网站共享网站上的网址,因此他们只响应HTTPS和FQDN请求。 我们的很多用户在我们的收藏夹中都有“内部”链接,当我们更改网址时,这些链接会中断。
我需要一种方法来打开每个“收藏夹”,看看是否有任何链接指向旧网址,然后将其更改为新网址。
这是我正在尝试做的事情(没有出现错误,但链接未被修改):
# 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 $_ }
答案 0 :(得分:1)
您可以使用COM来操作快捷方式文件。使用CreateShortcut
method的Shell
class来读取快捷方式文件:
$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();
答案 1 :(得分: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()
答案 2 :(得分: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()
}
我添加了代码来帮助您设置主页。
答案 3 :(得分:0)
第一个脚本的问题是Set-Content Cmdlet需要-Path。这是我写的一个有效的脚本。您可以根据需要进行调整。这是相当冗长但嘿我正在学习。我调用了脚本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 ""
}