L'édition de raccourci (.lnk) propriétés avec Powershell

j'ai trouvé un moyen VBS méchant pour faire cela, mais je suis à la recherche d'une procédure natif chic pour éditer les propriétés d'un .LNK fichier. Le but est d'atteindre les machines distantes, de dupliquer un raccourci existant avec la plupart des propriétés correctes, et d'en éditer quelques-unes.

s'il était plus facile d'écrire un nouveau fichier de raccourci, cela fonctionnerait aussi.

22
demandé sur Doug Chase 2009-01-27 21:15:55

3 réponses

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

trouvé la version VB du code ici: http://www.tutorialized.com/view/tutorial/Extract-the-target-file-from-a-shortcut-file-.lnk/18349

30
répondu JasonMArcher 2009-02-10 21:59:28

voici les fonctions que j'utilise pour traiter .les fichiers lnk. Ils sont des versions modifiées des fonctions ici comme mentionné par @ Nathan Hartley. Je me suis amélioré Get-Shortcut pour gérer les caractères génériques comme * en passant des cordes à dir pour les étendre en ensembles D'objets 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()
  }
}
15
répondu Tim Lewis 2014-02-23 11:24:54

Je ne pense pas qu'il y ait une façon autochtone.

Il y a ce DOS util: Raccourci.exe.

vous avez encore besoin de copier l'util dans le système distant, puis peut-être l'appeler en utilisant WMI pour faire les changements que vous recherchez.

je pense que la façon la plus facile sera d'écraser et/ou de créer un nouveau fichier.

avez-vous accès à ces systèmes via un partage à distance?

3
répondu Marco Shaw 2009-01-28 03:20:41