我做了一个有点灵活的脚本。以下脚本显示了一些如何使用它的示例。最后一个示例显示了它如何处理在同一行中具有 URL 和名称的文本文件。在大多数情况下,这已经可以至少生成一些链接。可以自定义脚本以适合您的文本文件,但您需要显示示例。
Function Remove-InvalidFileNameChars {
param(
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String]$Name
)
$invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
$re = "[{0}]" -f [RegEx]::Escape($invalidChars)
return ($Name -replace $re,"_")
} # Source: https://stackoverflow.com/questions/23066783/how-to-strip-illegal-characters-before-trying-to-save-FilePaths
function New-Link {
param(
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[Alias("Address","LINK")]
[String]$URL,
[Parameter(Mandatory=$true,
Position=1,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateSet("URL", "HTML")]
[String]$Type = "URL",
[Parameter(Mandatory=$false,
Position=2,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({if ([System.IO.FileInfo]$_) {$true} else{throw "Exception: The parent directory was not found."}})]
[Alias("Name","Path")]
[System.IO.FileInfo]$FilePath
)
if (-not $FilePath) { $FilePath = (Remove-InvalidFileNameChars $URL) + "." + $Type }
if ($FilePath -notlike "*.$Type") {$FilePath = [string]$FilePath + "." + $Type}
$FilePath = ([string]$FilePath).trim() # Remove leading or trailing spaces
switch ( $Type ) {
"URL" {
# Windows only - it's windows default format for URLs
"[InternetShortcut]`r`nURL=$URL`r`n" | Out-File $FilePath
}
"HTML" {
# Compatible to multiple OS/Browsers but not with smartphones when not accessed via browser and webserver
# - Idea from https://www.ctrl.blog/entry/internet-shortcut-files.html - More ideas for alternative types are there like
# - You might see the local file within browsers addressbar as long as the target site does not answer
"<!doctype html>",
"<script>",
"window.location.replace('$URL')",
"</script>",
"" | Out-File $FilePath
}
}
}
#Examples for Web
New-Link -URL stackoverflow.com -Type url
New-Link -URL https://stackoverflow.com -Type url
New-Link -URL stackoverflow.com -Type HTML
New-Link -URL https://stackoverflow.com -Type HTML
New-Link -URL https://stackoverflow.com/questions/71593413/powershell-script-to-create-website-shortcuts-from-text-file -Type HTML
New-Link -URL stackoverflow.com -Type HTML -FilePath "Where Developers Learn, Share, & Build ..."
#Other Examples
New-Link -URL ldap://yourDomainController -Type URL
New-Link -URL tel:00123456789 -Type URL -Name CallMe
New-Link -URL mailto:noreply@DSmoothPanda -Type URL -Name MailMe.url
New-Link -URL FTP:yourWebServer -Type URL -Name UploadFilesToMe
## Example of generating URL files for links with names in same line
# ExampleContent for "links.txt"
"
https://website.com/what?you%20want page with more data
another link that i will make a file for https://www.google.com/search?q=StackOverflow
this line will not generate a link as it doesn't have something that can be detected
the next line will generate only the first link and everything else will be the name
my holiday pictures are http://myholidaypicturepage.com here http://onedrive.com
nextline will generate filename from url
https://stackoverflow.com/questions/71593413/powershell-script-to-create-website-shortcuts-from-text-file
" | Out-File .links.txt
# Generation of URL files
foreach ($line in (Get-Content .links.txt | where {$_ -match "(https?:|mailto:|ftp:|tel:)"})) {
$URL = @($line -split " " | where {$_ -match "^https?:"})[0]
$FilePath = ($line -replace [Regex]::Escape($url),"").trim()
"`r`n`tGenerating Link with following parameters"
" Filename: $FilePath"
" URL: $URL"
if ($FilePath) {
New-Link -URL $URL -Type URL -FilePath $(Remove-InvalidFileNameChars $FilePath)
} else {
New-Link -URL $URL -Type URL
}
}
将在当前文件夹中生成以下测试文件,以更好地了解我的脚本的作用:
我将为.URL制作文件的另一个链接
CallMe.URL
https___stackoverflow.com.HTML
https___stackoverflow.com.url
https___stackoverflow.com_questions_71593413_powershell-script-to-create-website-shortcuts-from-text-file.HTML
https___stackoverflow.com_questions_71593413_powershell-script-to-create-website-shortcuts-from-text-file.URL
ldap___yourDomainController.URL
links.txt
MailMe.url
my holiday pictures are here http___onedrive.com.URL
page with more data.URL
stackoverflow.com.HTML
stackoverflow.com.url
UploadFilesToMe.URL
Where Developers Learn, Share, & Build ....HTML