如何将“捷径”目标路径设置为网站?

发布于 2025-01-27 00:37:21 字数 929 浏览 1 评论 0原文

我想使用powershell修改快捷方式的目标路径以打开网站 我发现以下几乎有效的脚本

function Set-Shortcut {
  param(
  [Parameter(ValueFromPipelineByPropertyName=$true)]
  $LinkPath,
  $Hotkey,
  $IconLocation,
  $Arguments,
  $TargetPath
  )
  begin {
    $shell = New-Object -ComObject WScript.Shell
  }

  process {
    $link = $shell.CreateShortcut($LinkPath)

    $PSCmdlet.MyInvocation.BoundParameters.GetEnumerator() |
      Where-Object { $_.key -ne 'LinkPath' } |
      ForEach-Object { $link.$($_.key) = $_.value }
    $link.Save()
  }
}

将默认

Set-Shortcut -LinkPath "C:\Users\user\Desktop\test.lnk" -TargetPath powershell start process "www.youtube.com"

为附加默认路径,如果您不定义一个默认路径:

"C:\Users\micha\Desktop\powershell start process "www.youtube.com""

如何摆脱该默认文件路径?

奖金: 如果有人打破了这条代码,我会很感激:

ForEach-Object { $link.$($_.key) = $_.value }

I want to use powershell to modify the TargetPath of a shortcut to open a website
I found the below script that almost works

function Set-Shortcut {
  param(
  [Parameter(ValueFromPipelineByPropertyName=$true)]
  $LinkPath,
  $Hotkey,
  $IconLocation,
  $Arguments,
  $TargetPath
  )
  begin {
    $shell = New-Object -ComObject WScript.Shell
  }

  process {
    $link = $shell.CreateShortcut($LinkPath)

    $PSCmdlet.MyInvocation.BoundParameters.GetEnumerator() |
      Where-Object { $_.key -ne 'LinkPath' } |
      ForEach-Object { $link.$($_.key) = $_.value }
    $link.Save()
  }
}

However

Set-Shortcut -LinkPath "C:\Users\user\Desktop\test.lnk" -TargetPath powershell start process "www.youtube.com"

Will default out to attaching a default path if you do not define one to look like:

"C:\Users\micha\Desktop\powershell start process "www.youtube.com""

how do I get rid of that default file path?

BONUS:
I'd be appreciative if someone broke down this line of code:

ForEach-Object { $link.$($_.key) = $_.value }

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

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

发布评论

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

评论(1

猛虎独行 2025-02-03 00:37:21

您是否尝试过直接更改快捷对象的属性?

尝试一下,让我知道:

function Set-Shortcut {

[CmdletBinding()]
param (
    [Parameter(Mandatory, Position = 0, ValueFromPipeline)]
    [System.String]$FilePath,

    [Parameter(Mandatory, Position = 1)]
    [System.String]$TargetPath
)

Begin {
    $shell = new-object -ComObject WScript.Shell
}

Process {
    try {
        $file = Get-ChildItem -Path $FilePath -ErrorAction Stop

        $shortcut = $shell.CreateShortcut($file.FullName)
        $shortcut.TargetPath = $TargetPath
        $shortcut.Save()    
    }
    catch {
        throw $PSItem
    }
}

End {
    while ($result -ne -1) {
        $result = [System.Runtime.InteropServices.Marshal]::ReleaseComObject($shell)
    }
}

have you tried to change the properties of the shortcut object directly?

Try this and let me know:

function Set-Shortcut {

[CmdletBinding()]
param (
    [Parameter(Mandatory, Position = 0, ValueFromPipeline)]
    [System.String]$FilePath,

    [Parameter(Mandatory, Position = 1)]
    [System.String]$TargetPath
)

Begin {
    $shell = new-object -ComObject WScript.Shell
}

Process {
    try {
        $file = Get-ChildItem -Path $FilePath -ErrorAction Stop

        $shortcut = $shell.CreateShortcut($file.FullName)
        $shortcut.TargetPath = $TargetPath
        $shortcut.Save()    
    }
    catch {
        throw $PSItem
    }
}

End {
    while ($result -ne -1) {
        $result = [System.Runtime.InteropServices.Marshal]::ReleaseComObject($shell)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文