Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

wsh - Change target of shortcut created in Powershell

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 FilesInternet Exploreriexplore.exe" to runas.exe /user:capeplca-bhargate /savecreds "C:Program FilesInternet Exploreriexplore.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 FilesInternet Exploreriexplore.exe"
$ShortcutFile = "$env:USERPROFILEdesktopIEadmin.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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

$ShortcutFile = "$env:USERPROFILEdesktopIEadmin.lnk"

$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)

$Shortcut.TargetPath = 'runas.exe' 
$Shortcut.Arguments =
  "/user:capeplca-bhargate /savecreds `"C:Program FilesInternet Exploreriexplore.exe`""     #` (dummy comment to fix broken syntax highlighting)
$Shortcut.Save()

See the WshShortcut documentation for an overview of all available properties.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...