Edición de propiedades de acceso directo (.lnk) con Powershell
-
20-08-2019 - |
Pregunta
He encontrado una forma desagradable de VBS de hacer esto, pero estoy buscando un procedimiento nativo de PoSh para editar las propiedades de un archivo .LNK. El objetivo es llegar a máquinas remotas, duplicar un acceso directo existente con la mayoría de las propiedades correctas y editar un par de ellas.
Si fuera más fácil escribir un nuevo archivo de acceso directo, eso también funcionaría.
Solución
Copy-Item $sourcepath $destination ## Get the lnk we want to use as a template
$shell = New-Object -COM WScript.Shell
$shortcut = $shell.CreateShortcut($destination) ## Open the lnk
$shortcut.TargetPath = "C:\path\to\new\exe.exe" ## Make changes
$shortcut.Description = "Our new link" ## This is the "Comment" field
$shortcut.Save() ## Save
Encontré la versión VB del código aquí: http: // www.tutorialized.com/view/tutorial/Extract-the-target-file-from-a-shortcut-file-.lnk/18349
Otros consejos
A continuación se muestran las funciones que uso para tratar con archivos .lnk. Son versiones modificadas de las funciones que se encuentran aquí según lo mencionado por @Nathan Hartley. He mejorado Get-Shortcut
para manejar comodines como *
pasando cadenas a dir
para expandirlas en conjuntos de objetos FileInfo.
function Get-Shortcut {
param(
$path = $null
)
$obj = New-Object -ComObject WScript.Shell
if ($path -eq $null) {
$pathUser = [System.Environment]::GetFolderPath('StartMenu')
$pathCommon = $obj.SpecialFolders.Item('AllUsersStartMenu')
$path = dir $pathUser, $pathCommon -Filter *.lnk -Recurse
}
if ($path -is [string]) {
$path = dir $path -Filter *.lnk
}
$path | ForEach-Object {
if ($_ -is [string]) {
$_ = dir $_ -Filter *.lnk
}
if ($_) {
$link = $obj.CreateShortcut($_.FullName)
$info = @{}
$info.Hotkey = $link.Hotkey
$info.TargetPath = $link.TargetPath
$info.LinkPath = $link.FullName
$info.Arguments = $link.Arguments
$info.Target = try {Split-Path $info.TargetPath -Leaf } catch { 'n/a'}
$info.Link = try { Split-Path $info.LinkPath -Leaf } catch { 'n/a'}
$info.WindowStyle = $link.WindowStyle
$info.IconLocation = $link.IconLocation
New-Object PSObject -Property $info
}
}
}
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()
}
}
No creo que haya una forma nativa.
Existe esta utilidad de DOS: Shortcut.exe .
Aún necesita copiar la utilidad en el sistema remoto, luego posiblemente llamarla usando WMI para hacer los cambios que está buscando.
Creo que la forma más fácil será sobrescribir y / o crear un nuevo archivo.
¿Tiene acceso a estos sistemas a través de un recurso compartido remoto?
Una breve adición a la respuesta de @ JasonMArcher ...
Para ver las propiedades disponibles, puede ejecutar $shortcut
después de $shortcut = $shell.CreateShortcut($destination)
en un PS .
Esto imprimirá todas las propiedades y sus valores actuales.