Edição de propriedades do atalho (.lnk) com Powershell
-
20-08-2019 - |
Pergunta
Eu encontrei uma maneira desagradável VBS para fazer isso, mas eu estou procurando um procedimento Posh nativo para editar as propriedades de um arquivo .lnk. O objetivo é chegar a máquinas remotas, duplicar um atalho existente com a maioria das propriedades corretas, e editar um par deles.
Se ele seria apenas mais fácil de escrever um novo arquivo de atalho, que iria trabalhar muito.
Solução
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
Encontrado a versão VB do código aqui: http: // www.tutorialized.com/view/tutorial/Extract-the-target-file-from-a-shortcut-file-.lnk/18349
Outras dicas
A seguir estão as funções que eu uso para lidar com arquivos .lnk. Eles são versões modificadas das funções encontrada aqui como mencionado por @ Nathan Hartley. Eu melhorei Get-Shortcut
para wildcards punho como *
passando cordas para dir
para expandi-las em conjuntos de FileInfo objetos.
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()
}
}
Eu não acho que há uma maneira nativa.
Não é este DOS util:. Shortcut.exe
Você ainda precisa copiar o util ao sistema remoto, em seguida, possivelmente chamá-lo usando WMI para fazer as mudanças que você está procurando.
Eu estou pensando a maneira mais fácil será para substituir e / ou criar um novo arquivo.
Você tem acesso a estes sistemas através de um compartilhamento remoto?
Um curto além de resposta de @ JasonMArcher ..
Para ver as propriedades disponíveis você pode apenas $shortcut
correr atrás $shortcut = $shell.CreateShortcut($destination)
em um PS .
Isto irá imprimir todas as propriedades e seus valores atuais.