• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

vbscript - How to open .lnk file or better known as a shortcut

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

nfinity

Member
Joined
Jul 8, 2012
I simply have this

set x=createobject("wscript.shell")
x.run %comspec% /k C:\Program Files (x86)\Keysticks.net\Keysticks\KeySticks.lnk"

but gives me return code... invalid character, code: 800A0480

I copy pasted the path... I must be missing something else
 
Does your code have a missing quote in front of "C:\"? I don't see the first one.
 
Yeah, can't believe I missed that quote. But still having problems. Now it will launch cmd but doesn't recognize the command or the link.

This is my full code now

set x=createobject("wscript.shell")
x.run "%comspec% /k C:\Program Files (x86)\Keysticks.net\Keysticks\KeySticks.lnk"
wscript.sleep 2000
x.sendkeys"%{F4}"

I don't really know powershell but I would use it if you could do things like 'sendkeys' like in vbscript.
 
What does that expand to? Do:

Code:
MsgBox("%comspec% /k C:\Program Files (x86)\Keysticks.net\Keysticks\KeySticks.lnk")

I'm guessing you need to put double quotes around the name, which need to be escaped. You also may need to escape %comspec%, whatever that does.

Code:
x.run "%comspec% /k ""C:\Program Files (x86)\Keysticks.net\Keysticks\KeySticks.lnk"""
 
I can launch the shortcut in a bat file with this command

start "" /b "C:\Program Files (x86)\Keysticks.net\Keysticks\KeySticks.lnk"

so I know the path is valid. But batch doesn't have sendkey options so trying to do all of it in vbs.
 
It is likely an escaping issue. The space in the LNK path makes it think there are two files and it gets confused.
 
thideras, your awesome! that worked. Thank both of you for your help!
 
I do not know if you still are working on this project, but it happens to be what I used to do as part of our windows imaging - back from windows 2k and XP.

While I doubt this is useful to anyone, all this script really does is "wrap" SendKeys but gives you a way to pass a text file(s) as input, lets you have essentially variables in that text file. Deals with path resolution (like you needed) but also other stupid things like keeping the damn window focused.

There's also really bad attempt to use RC4 in there to let you have encrypted input. Just kinda ignore that - this was never intended for anything more than very basic obfuscation so that if you had say a Domain Admin password for auto-joining machines to the domain, it was at the very least not plain text if you somehow forgot to delete that file.

If anything you can laugh at how old school the structure is. Yea, I guess being taught "Structured Programming" on a TRS-80 in high school kinda shows.

Last note, this actually worked really well from NT to XP. For some reason win7 and later tends to drop keystrokes randomly when you blast input to them with sendkeys. Or it might just be fighting with the updates to the command windows. But it still worked 80% of the time and for me at least still was a life-saver when after we blasted a windows image out to blocks of 50 machines - this automated so much of the tedium of doing things like "first runs" on Office (to activate), web browsers, other stupid things like adding computer descriptions etc. This was important because we ran DeepFreeze (look up SteadyState too) which was a product that essentially gave your computer amnesia on reboot. So if you jacked it up, just reboot and it was like nothing happened. Unfortunately it also meant you would get all the annoying 1st run tutorials and other setup things every boot too unless you explicity did it before freezing.

Oh! just wanted to add. For those who do still mess with vbs, to get the script host back to the default of popping up windows after you run this (I set it to command host, but that's not default), run " cscript //H:WScript " at the command line.

Needless to say, Microsoft's official imaging solutions don't cover that.

Anyway, enough blabbing, more things for you to laugh at. Dump into a folder of it's own and run "runme.bat" Also it assumes Admin (i know, bad practice) but I do have some UAC wrappers somewhere for this - but I doubt anyone cares that much ;p

ADDED: To restore vbs back to the normal windows popup behavior, run "cscript //H:WScript" on the command line. Also if for some reason you do run this script in WScript mode you will go insane =D to escape Ctrl-C.... or at least try before the next windows popup.
 

Attachments

  • automationdemo.7z
    9.4 KB · Views: 78
Last edited:
Thanks xrror!

I just saw this now. I did fix my problem.... back in the middle of 2015 haha!

But anyway, when I have more free time I may play around with your script. Finding more uses for scripting everything.
 
Hope you have some fun with it. Also feel free to use any parts you want wherever and however - I don't care about attribution or credit. Because to be honest most of the script (like almost all scripts) are parts bolted together bits and examples I found on the net and in books - I tried to attribute where I found those parts in my comments.

- I only felt I had to explicitly state that because some people get really touchy about "their work" but I'm not one of them. I'm happy even is someone bothers to open the 7zip file to look, and laughs at how awful my "VB code skillz" are lol

Things I always wanted to improve in it:
Figure out why win7 and after randomly drops keystrokes - IF YOU FIX THIS, YOU WILL BE MY HERO (also, suddenly this script becomes useful to EVERYONE - even if just to punk people heheheh)
Actually get passing more than one argument to a shortcut working reliably. I either could get it to work with multiple arguments, or only one. But not both. argh.
Making the input text file format something like XML ... which yes I realize the madness of trying to write a text parser in vbs... which is parsed. Turtles all the way down lol

Things that make me weep:
fncPathMasseuse (function Path Masseuse) is a crime against humanity - both in how I wrote it and why it was needed to begin with
The fact that this is just old legacy vbscript... means you can pass this whole thing as a login script.
The runme.bat for the demo might not work anymore in win10, because I don't know of wordpad still lives =/
In the winXP days, I was tempted to make an input that solved a solitaire game.

Anyways I blab way too much. I'm probably going to package up what the last "production" folder I used of this and post it, because that includes all of the other scripts used to feed this one with input. Also there might be some fun/horrible parts/ideas for other people to laugh at.
 
Haha! thanks! appreciate it. I'll let you know if I find any fixes or interesting findings with the script on my windows 7 machines. Personally haven't been very impressed with Win10. Think all my machines will be staying on 7 for awhile.

But anyway I'll see if I can get some cool automation going with this script.
 
Back