【问题标题】:PowerShell script to create website shortcuts from text file从文本文件创建网站快捷方式的 PowerShell 脚本
【发布时间】:2022-10-17 04:19:17
【问题描述】:

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

【问题讨论】:

  • 嘿,欢迎来到 SO!看起来您单击提交按钮太快而忘记添加遇到问题的代码示例。您知道吗,您可以通过点击下方的编辑链接来改进您的帖子。您还可以查看help center 部分,特别是How to Ask 一个好问题。祝你今天过得愉快!
  • 请提供足够的代码,以便其他人可以更好地理解或重现该问题。

标签: powershell shortcut-file


【解决方案1】:

我做了一个有点灵活的脚本。以下脚本显示了一些如何使用它的示例。最后一个示例显示了它如何处理在同一行中具有 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

【讨论】:

    猜你喜欢
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 2021-03-21
    相关资源
    最近更新 更多