Domanda

Ho trovato un brutto modo VBS per farlo, ma sto cercando una procedura PoSh nativa per modificare le proprietà di un file .LNK. L'obiettivo è raggiungere i computer remoti, duplicare un collegamento esistente con la maggior parte delle proprietà corrette e modificarne un paio.

Se sarebbe solo più semplice scrivere un nuovo file di collegamento, anche questo funzionerebbe.

È stato utile?

Soluzione

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

Ho trovato la versione VB del codice qui: http: // www.tutorialized.com/view/tutorial/Extract-the-target-file-from-a-shortcut-file-.lnk/18349

Altri suggerimenti

Di seguito sono riportate le funzioni che utilizzo per gestire i file .lnk. Sono versioni modificate delle funzioni trovate qui come menzionato da @Nathan Hartley. Ho migliorato Get-Shortcut per gestire i caratteri jolly come * passando le stringhe a dir per espanderle in set di oggetti 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()
  }
}

Non penso che ci sia un modo nativo.

Esiste questo programma di utilità DOS: Shortcut.exe .

Devi ancora copiare l'utilità sul sistema remoto, quindi eventualmente chiamarlo utilizzando WMI per apportare le modifiche che stai cercando.

Sto pensando che il modo più semplice sarà quello di sovrascrivere e / o creare un nuovo file.

Hai accesso a questi sistemi tramite una condivisione remota?

Una breve aggiunta alla risposta di @ JasonMArcher ..

Per vedere le proprietà disponibili puoi semplicemente eseguire $shortcut dopo $shortcut = $shell.CreateShortcut($destination) in un PS . Ciò stamperà tutte le proprietà e i loro valori correnti.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top