我想使用 powershell 修改打開網站的快捷方式的 TargetPath 我發現下面的腳本幾乎可以作業
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 }
uj5u.com熱心網友回復:
您是否嘗試過直接更改快捷方式物件的屬性?
試試這個,讓我知道:
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)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/471038.html
