Change target of shortcut created in Powershell

家住魔仙堡 提交于 2019-12-20 05:49:19

问题


I've got a script that installs a certain BP Program called iLink (Crosslink). The Script works fine and installs Java 6.21 and both Crosslink Components and also installs through DISM .Net 3.5 perfectly.

However, the only drawback is that the web version only works on a admin run internet explorer process. I've got it to create a shortcut on the users desktop but I want the script to change the target of it from: "C:\Program Files\Internet Explorer\iexplore.exe" to runas.exe /user:capeplc\a-bhargate /savecreds "C:\Program Files\Internet Explorer\iexplore.exe"

This is the script I've currently got I just want it to add it to the end or something to change the target of the shortcut:

# ... 
$TargetFile = "C:\Program Files\Internet Explorer\iexplore.exe"
$ShortcutFile = "$env:USERPROFILE\desktop\IEadmin.lnk"
$WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.Save()

The script outcome is fine, just need the shortcuts target changing to the one mentioned above in bold. Any help would be great!


回答1:


As gpunktschmitz points out in a comment on the question, you must:

  • make runas.exe the .TargetPath value

  • and assign the rest of the command line to the .Arguments property:

$WScriptShell = New-Object -ComObject 'WScript.Shell'

$ShortcutFile = "$env:USERPROFILE\desktop\IEadmin.lnk"

$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)

$Shortcut.TargetPath = 'runas.exe' 
$Shortcut.Arguments =
  "/user:capeplc\a-bhargate /savecreds `"C:\Program Files\Internet Explorer\iexplore.exe`""     #` (dummy comment to fix broken syntax highlighting)
$Shortcut.Save()

Note the need to `-escape the embedded double quotes.

Alternatively, with fixed strings such as the one at hand, you could use '...' (single quotes) as the outer quotes, in which case you needn't escape embedded double quotes.

Conversely, consider use of $env:ProgramFiles and specifying the username via a variable, in which case you do need "..." for the outer quoting to ensure that string expansion (interpolation) is performed.



来源:https://stackoverflow.com/questions/47741736/change-target-of-shortcut-created-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!