powershell 创建带有图标的desktop .url快捷方式

8hhllhi2  于 12个月前  发布在  Shell
关注(0)|答案(2)|浏览(153)

我试图创建一个简单的脚本,作为部署到多个网站的一部分,将创建一个桌面快捷方式到一个网址,并应用一个图标,但我遇到了一些错误。
以下是脚本:

# region Functions
Push-Location $PSScriptRoot

# Initial values
$Icon_start_path = ".\Icon.ico"
$Icon_end_folder = "C:\Program Files\Icons"
$Icon_end_path = "C:\Program Files\Icons\Icon.ico"
$Shortcut_end_path = "C:\Users\Public\Desktop\website.url"
$Target_path = "https://website.co.uk/"
$Icon_filename = "Icon.ico"

# If previous shortcut is there, delete it
Get-ChildItem $Shortcut_end_path -ErrorAction SilentlyContinue | Remove-Item -ErrorAction SilentlyContinue

# Check if icon file has been copied to $Icon_end_folder
if (Test-Path $Icon_end_folder) {
   
    Write-Host "Icons folder already exists."
    
}
else
{
  
    #Create directory if it doesn't exists
    New-item -Path $Icon_end_folder -ItemType Directory
    Write-Host "Icons folder created."
}

# Copy the icon file over
Copy-Item -Path $Icon_start_path -Destination $Icon_end_folder

# Create the shortcut
$shell = New-Object -ComObject WScript.Shell
$shortcut = $Shell.CreateShortcut($Shortcut_end_path)
$shortcut.TargetPath = $Target_path
$Shortcut.IconLocation = $Icon_end_path
$shortcut.Save()

字符串
当我在PowerShell中测试运行它时,我收到以下错误消息:

The shortcut pathname must end with .lnk or .url.
At line:2 char:1
+ $shortcut = $Shell.CreateShortcut($Shortcut_end_path)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
 
The property 'TargetPath' cannot be found on this object. Verify that the property exists and can be set.
At line:3 char:1
+ $shortcut.TargetPath = $Target_path
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
 
The property 'IconLocation' cannot be found on this object. Verify that the property exists and can be set.
At line:4 char:1
+ $Shortcut.IconLocation = "C:\Program Files\NHS_Icons\CSMS.ico"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
 
You cannot call a method on a null-valued expression.
At line:5 char:1
+ $shortcut.Save()
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull


尽管如此,它做的一切都是正确的-创建文件夹,复制.ico,在桌面上创建一个功能.url-但错误消息似乎暗示它找不到正确的网址来分配给快捷方式。之后的一切看起来都是说它不知道该怎么处理之后的命令,因为快捷方式还没有正确创建。
如果这些都可以忽略,那就好,但我最不想做的就是快捷方式中的.ico。有人能给我建议一个解决方案吗?

s3fp2yjn

s3fp2yjn1#

像这样的东西应该足够了。至少对我有用。️😉🤷🏼‍

$URLFileContent = @'
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,11
[InternetShortcut]
IDList=
URL=https://website.co.uk/
IconIndex=0
HotKey=0
IconFile=C:\Program Files\Icons\Icon.ico
'@

$NewItemSplat = @{
    Path     = 'C:\Users\Public\Desktop'
    Name     = 'website.url'
    ItemType = 'File'
    Value    = $URLFileContent
    Force    = $true
}
New-Item @NewItemSplat

字符串

kyks70gy

kyks70gy2#

考虑到错误消息,您的代码不太可能工作:

  • 第一条错误消息暗示$Shortcut_end_path的值实际上并没有以.url结尾
  • 后续错误可以解释为$shortcut-由于失败的(New-Object -ComObject WScript.Shell).CreateShortcut()调用-包含$nullSet-StrictMode -Version 2或更高版本。

如果你传递了一个以.url结尾的$Shortcut_end_path路径,为了创建一个 URL 快捷方式,(New-Object -ComObject WScript.Shell).CreateShortcut()返回的是一个WshUrlShortcut示例(而创建一个 * 常规 * 快捷方式(.lnk)返回一个WshShortcut示例)。
WshShortcut不同,WshUrlShortcut示例的唯一 * 可写属性是.TargetPath-奇怪的是,.IconLocation不存在
但是,与二进制的.lnk文件不同,**.url文件是INI格式的 * 纯文本文件 *,因此您有两个选项:

  • 您可以自己将 * 整个 * .url文件编写为纯文本,如Olaf's helpful answer所示。
  • 您可以在.url文件后面 * 追加 * 图标信息:**
# Be sure to use *full paths
$urlShortcutPath = "$HOME\Desktop\website.url"
$url = 'https://example.org'
$iconPath = 'C:\Windows\SysWOW64\OneDrive.ico'

# Create the shortcut
$urlShortcut = (New-Object -ComObject WScript.Shell).CreateShortcut($urlShortcutPath)
$urlShortcut.TargetPath = $url
$urlShortcut.Save()

# Append the icon information.
'IconIndex=0', "IconFile=$iconPath" |
  Add-Content -LiteralPath $urlShortcutPath

字符串
或者,如果你想要一个 * 完全 * 编程化的解决方案**,你可以使用一个普通的.lnk文件

  • .lnk文件快捷方式也可以在其.TargetLocation属性中接受URL,并且 do 具有.IconLocation属性
  • 唯一的警告是,你将无法编辑快捷方式的目标URL * 交互式 * 后的事实。
# Be sure to use *full paths
 # Note the use of *.lnk
 $shortcutPath = "$HOME\Desktop\website.lnk"
 $url = 'https://example.org'
 $iconPath = 'C:\Windows\SysWOW64\OneDrive.ico'

 # Create the shortcut
 $shortcut = (New-Object -ComObject WScript.Shell).CreateShortcut($shortcutPath)
 $shortcut.TargetPath = $url
 # Now you can use .IconLocation
 $shortcut.IconLocation = $iconPath
 $shortcut.Save()

相关问题