用于从文本文件创建网站快捷方式的 PowerShell 脚本

发布于 2025-01-16 09:22:06 字数 73 浏览 0 评论 0原文

我正在尝试创建一个 PowerShell 脚本,以从桌面上的文本文件在桌面上创建网站快捷方式。文本文本文件包含 URL 和网站名称。

I'm trying to create a PowerShell script to create website shortcuts on the desktop from a text file located on the desktop. Text text file has the URL and website names.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鼻尖触碰 2025-01-23 09:22:06

我制作了一个有点灵活的脚本。以下脚本显示了如何使用它的一些示例。最后一个示例显示了它如何处理 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 the 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
next-line will generate a 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

I made a script that is a bit flexible. The following script shows some examples of how to use it. The last example shows how it works with a text file with URLs and names in the same line. That would already work in most cases to get at least some links generated. The script may be customized to fit your text file, but you must show examples.

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 the 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
next-line will generate a 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
        }
    }

The following test files will be generated in the current folder to better understand what my script does:
another link that I will make a file for.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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文