Batch, CMD and Tasks.

Anything not relating to the X-Universe games (general tech talk, other games...) belongs here. Please read the rules before posting.

Moderator: Moderators for English X Forum

Post Reply
User avatar
felter
Posts: 6970
Joined: Sat, 9. Nov 02, 18:13
xr

Batch, CMD and Tasks.

Post by felter » Fri, 14. Jul 17, 22:17

This was done for something else, but it was never going to be seen by anyone and I thought hey maybe someone here would appreciate it or even find something helpful. Any questions feel free to ask them. It was copied over from a word document, so there my be a few formatting mistakes.

So I’m a lazy old git that likes shortcuts and that includes running programs, what I mean by this is that I have been playing a bit of Elite Dangerous of late but when I start the game it’s not just the program Elite Dangerous that I run, I actually have four programs that I use. Of course there is Elite Dangerous but I also use Voice Attack, Elite Dangerous Market Connector and a game recorder called Action!. Now I don’t want to have to click 4 buttons to start these four programs, so instead I use a batch file to start them all with a single click that runs all four of them in a single go. But there’s more, two of them require that I click on the yes button of the User Account Control, more button pressing. I’ve already said I’m too lazy for that, so I use the Task Scheduler to bypass the need to having to press all of those buttons. Finally I also want to have an icon in the start window to start all of these to keep the desktop tidy.

Phew considering I said I was a lazy old git, let me tell you this is a lot of work to do all of this. Some of it I already knew how to do it, others threw up some problems like you cannot link a .bat file from start, it has to be a .exe file, so I had to figure a way around that.

So I want to show you how I have achieved this, tell you of the problems I came up against, not just the start issue. I think some of you may like to know how to do it, or just how to do part of it like how to bypass the UAC for a single program while keeping it active for others as the safety feature it is meant to be.

So where to start, let’s start with the batch file, some of you may be asking what a batch file is, well it’s a computer file containing a list of instructions to be carried out in turn. In my case it’s being used to run four programs one after the other.

To do all of these things I’m just going to use software that you will probably already have on your computer. For the batch file all you need is notepad personally I use notepad++ but just plain notepad will do.

The first line that you have to enter for the creation of the batch file is:

Code: Select all

@echo off 
I’m not going to explain what the commands are doing, just telling you what to do I’ll also use some screenshots to show you what I have done and what you need to do.

Next we need to tell the file what it is going to do for us this it is to run a program, for this we use the line:

Code: Select all

start "EDLaunch" "M:\Games\EDLaunch\EDLaunch.exe"
I’ll go over some of it even though I said I wouldn’t. Start tells it what to do in this case start the program. The first use EDlaunch on the line above, while I have used this you could call it whatever you want, I just use what it is that is to be started, makes it easier to know what the line is being used to start. It’s the next part that is important as that is the address to what you want to be started. In my case this is the location of where I have Elite Dangerous installed, but it can be any program. To find where you have the program installed, you can use the link you use to start the program. If your link is in start, if you right click on that link and hover over more and click on open file location, then if you right click the link in the window that opens and click on properties, you can find the address to that program under the shortcut tab in the target box.

[ external image ]

You finish of the batch file with the command exit, so enter a new line and just type:

Code: Select all

Exit
So you should have this though you will have used the address to whatever you want to run.

[ external image ]

Now you have to save the file. To do this in notepad select the save as command below where you enter the File name, you need to change the save as file type to all files (*.*) and enter a filename and at the end of the filename add .bat, I’m just going to use test.bat this tells windows it’s a batch file.

[ external image ]

Save where you want to save the file, this could be your desktop but this is where the file will sit so I would pick somewhere else, I’m saving it in the folder where I am keeping these instructions. We are actually going to create a link to this file to launch it from, as we want to be able to change the icon for the file make it look better, and you can’t change the icon for a batch file. Not just that, I will be showing you how to add a link to the start menu and you can’t do that with a normal link. So anyway that is a batch file created, right now it is only a useless file as it only launches a single program, kind of a waste of time really but I’m going to add others to it. Right now let’s just leave it as is and create a link to it. To create a link is pretty easy all you have to do is right click on the file, go to send to and click on Desktop (create shortcut) and hey presto you have a link to the batch file on your desktop, which is fine if that’s where you want the shortcut but I don’t want it there though, I want it in start and you can’t put a link to a batch file in start, so to do this I’m going to use the CMD prompt to create a link.

So how do you go about doing this? You need to click on your start button, and find the windows system folder, in that folder right click on the command prompt icon and go more->open file location, now you want to copy the command prompt shortcut that is there, right click on it and click on copy now. I’m working form a single folder it’s where I saved the batch file earlier, so I have now went there and used the paste command to make a copy of the command prompt link

[ external image ]

So now we have a shortcut to the command prompt, but all that does is open up the command prompt, we want it to open up and run our batch file instead. First of I would say rename your link to the command prompt to something more appropriate, as it is going to be used to open up the and run the batch file and not the command prompt for me my batch file is going to be used to run programs related to Elite Dangerous, so I’m going to call it Elite Launch. You can call it whatever you want, remember, the name can always be changed at a later date. So to make it run our batch file we need to change some of its properties. So once again we open the properties of the newly named command prompt shortcut, we do this by right clicking on it and selecting properties from the dropdown list. When the properties window is open the target box will be blue and contain the words %windir%\system32\cmd.exe, this is where the command prompt program is, we want to keep this line and we want to add some more information onto it, as what we want is for the command prompt to run and execute our batch program. To do this at the end we add:

Code: Select all

/C
This tells the command prompt to run our batch file once then close, so we also have to add the address of where our batch file is located, to do this open the folder where the batch file is located. Now there are several ways to get the location you can use properties, which I will come back to in a second. For now let us do it another way. Above where the files are located is an address bar, if you left click on it you will be shown the address to the folder, it should be in blue if you right click this you can then copy the address.

[ external image ]

You now enter this address after the /C you entered in the target box a short while ago.

Now in my target box I have

Code: Select all

%windir%\system32\cmd.exe /C E:\Documents\batch running
We also have to add the name of the batch file which I used test so to the end of the line I have to add:

Code: Select all

\test
This leaves me with:

Code: Select all

%windir%\system32\cmd.exe /C E:\Documents\batch running\test
[ external image ]

Now I have done this on purpose, as when I did this the first time it wouldn’t work as there is a mistake, it took me a little while to work it out, when I created a folder just like here I used two words and I included a space between those two words and due to this it will not work, a simple work around is to rename the folder and use an underscore in place of the space so now I have called the folder batch_running and edited my line so that it now reads:

Code: Select all

%windir%\system32\cmd.exe /C E:\Documents\batch_running\test
Now when I click on apply and ok to close the properties windows, and when I double click on the CMD shortcut I just created instead of launching the command prompt, it should launch the program which in my case is Elite Dangerous, mines does and I hope yours does too.

So now we have a CMD prompt shortcut that launches a program, but it still looks like the icon for the command prompt, which is not good. So let’s change the icon. You can find many icons on the internet just google free icons if you need one, I’m just going to use the same icon that is used for Elite Dangerous.

Now to do this once again open the properties window for the CMD prompt or should I say the our new batch file launcher, under the shortcut tab there is three buttons, we want the middle button labelled change icon, click on it there will just be a single icon in the select an icon from the list below box, we don’t want that as it is the command prompt icon we want to use our own icon. So if you click on the browse button you can head over to the folder where you have the icon you want to use. I’m heading to where the Elite Dangerous game is installed, as I’m going to use that icon, all I have to do is select the games .exe file and that will use that icon I want, you just have to select the one you want to use. Once you have your icon selected just click ok twice and that should have changed the CMD icon to your shiny new icon.

Next we want to put the shortcut that runs our batch file onto the start menu window, you just can’t drag it there as that won’t work, no all you need to do is once again right click on it and select pin to start. If this had been a normal shortcut that option would not have been available to us. As you can see.

[ external image ]

[ external image ]

So now we have an icon in the start menu that when we click on it, it will run our batch file which in turn will run our software that we wanted to run, but we are not finished yet.

Right at the start I said I wanted to use a batch file to run four different programs, so far it only runs a single program. The other three on my list were Voice Command, Elite Dangerous Market Connector and the game recorder called Action!. The Elite Dangerous Market Connector is the easiest as this program just needs to be run the same way as Elite itself did, I just need to add the line:

Code: Select all

start "EDMarketConnector" "C:\Program Files (x86)\EDMarketConnector\EDMarketConnector.exe"
This is the name and location of the program. So now we have a batch file with two programs for it to start.

[ external image ]

Now the other two are a little bit different, as they require permission from the user account control to launch, and I want to bypass this and to do this I’m going to use the Task Scheduler. To open the Task Scheduler click on the start button and just type in task scheduler and click enter, the app will launch.

[ external image ]

The task Scheduler can look pretty intimidating but I’ll show you what you need to do. The next program I will do is voice attack. The first thing that needs to be done using the Task Scheduler, in the actions pane on the right hand side you want to left click on Create Task…

[ external image ]

This opens up the create task window, this is what we use to bypass the UAC meaning we won’t need to press yes to launch the program anymore. Now in the create task under the General tab we first want to give our task a name, as I’m doing this for voice attack that’s what I’m going to call it, but you can call it whatever you want, though I would name it after whatever program you are doing this for. Next you can give the task a description, this it is not essential and doesn’t do anything other than tell you what the task is being used for, you can skip doing this. The other thing to make sure that is done on this tab, is make sure that the tick box for Run with highest privileges is ticked, as this is what gives the software the needed rights to get passed the UAC, I also like to change the configure for: to windows 10 as that is what I’m using, but if the software is meant for say windows 7 then I would use that instead.

[ external image ]

Next we move onto the Actions tab. At the bottom of the window click on the New… button, now there are a couple of ways of doing this but I’m going to show how I do it, I know there is another maybe easier way to do it but I have had problems with that way, so I do it this way instead.

So for the new action we want to have Start a program as our Action, as that is what we are for doing. In the settings in the box under Program/script you want to enter:

Code: Select all

%windir%\System32\cmd.exe
Yes that is the same thing that was in the CMD prompt link we setup earlier. Now in the Add arguments (optional): we put in:

Code: Select all

/c start "Voice Attack" "K:\VoiceAttack\VoiceAttack.exe"
Once again this should look kind of familiar, as we used it earlier on. To go over it again, we are once more using the CMD prompt and this line tells it to run the program once, before closing the CMD prompt. In this demonstration it is voice attack and it’s the address to where that program is on the computer, so if you were using it for another program, you would have the name of that program and where it was kept on your computer. Leave the other box empty, I will come back to that for the next program.

[ external image ]

Click on ok and the actions tab should now look like this.

[ external image ]

That is it, that’s all there is too it, now just click on ok to create the task but we also need to tell the batch file about it, so back to the batch file we go. This time is a little bit different from the other two we set up previously, this time the line goes:

Code: Select all

start "VoiceAttack" C:\Windows\System32\schtasks.exe /run /tn "Voice Attack"
What we are doing is we are not linking to the program we want to run, instead we are linking to task scheduler (schtasks.exe), and we are telling it to run the voice attack which is the name I gave to the task I created. You would use the name of whatever the task you created.

Now I have one more to show you, and that’s how to setup the screen recorder Action!. This is a little bit different. The task scheduler is setup the same but it needs something else added, as when the software is run using the batch file, it will crash and this is because the command prompt itself has to be run from the directory that the program is in. So if you were to setup a program like this and it would not start or it would start then crash this is what you need to try first. This time when creating the task in the actions tab we click on the edit button, in the edit action window that opens there is the command Start in (optional): you enter the path to where the program is on your computer, for me for Action! It was:

Code: Select all

C:\Program Files (x86)\Mirillis\Action!\
[ external image ]

This tells the command line where to run the program from and it stops the program from crashing, this gave me a headache the first time when it happened to me, trying to figure this out but I got there in the end.

All we need to do now is go back to the batch file in notepad and add the start line for Action!. For me this is:

Code: Select all

start "Action" C:\Windows\System32\schtasks.exe /run /tn "Action"
This leaves me with a batch file that looks like this:

[ external image ]

You can now save all of your hard work and close everything. Now I can launch all four programs with a single click of the icon in the start menu, and I don’t get bothered with the UAC any more either. I use this for other programs, I just used the ones above as it showed a few different issues I had to get around and how I did eventually get around them.
Florida Man Makes Announcement.
We live in a crazy world where winter heating has become a luxury item.

brucewarren
Posts: 9243
Joined: Wed, 26. Mar 08, 14:15
x3tc

Post by brucewarren » Fri, 14. Jul 17, 22:42

EDDN Felter?

Real pilots cart the goods around at random and hope for the best :P

User avatar
Antilogic
Posts: 7526
Joined: Wed, 6. Apr 05, 20:33
x3tc

Post by Antilogic » Fri, 14. Jul 17, 23:27

Ewwww batch :P

UniTrader
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 14571
Joined: Sun, 20. Nov 05, 22:45
x4

Post by UniTrader » Fri, 14. Jul 17, 23:35

Antilogic wrote:Ewwww batch :P
no bashing here please :D :P
if not stated otherwise everything i post is licensed under WTFPL

Ich mache keine S&M-Auftragsarbeiten, aber wenn es fragen gibt wie man etwas umsetzen kann helfe ich gerne weiter ;)

I wont do Script&Mod Request work, but if there are questions how to do something i will GLaDly help ;)

User avatar
Morkonan
Posts: 10113
Joined: Sun, 25. Sep 11, 04:33
x3tc

Post by Morkonan » Fri, 14. Jul 17, 23:41

Batch files make useful panic buttons, too. :)

Waay back in the dark ages, before broadband, a friend of mine worked at a facility that held regular flash inspections of its employee's computers.

This was back in the days of Civilization 1, maybe Civ 2. Both my friend and I loved to play those and we'd talk about the games fairly frequently. That was especially true since he basically played this game all day at his desk. :) I'd usually talk to him during the day and he'd give me the play-by-plays as time wore on.

But, he had a failsafe - A batch file he could click that would erase the game and every indication it had ever been installed, including erasing itself when it was done. IIRC, he used that batch file a couple of times in earnest...

These were the days when anyone who knew anything about "computer" could get a job. Security practices were usually nothing more than running a diagnostics program from a disk. To tell you how open this discipline was - I, myself, had offers from "teh gubbermint" to work on "stuffs." (I knew some people who knew some people.) One person who offered was heading up a new military project and asked me about joining. I told him I know a few things, some bits of languages, here and there, and could easily troubleshoot hardware issues and operating system problems, but all of those were things I considered to be "unskilled" and of little value to such a project. He told me I could have a desk by next week... I kid you not. I had several friends with skill levels similar to mine at that time that took such opportunities.

I guess what I'm trying to say is that what amounts to basic knowledge is interpreted differently, depending upon the knowledge of the interpreter. Here, these batch files are "basic", but they're an awesome display of inspiration born of a desire for convenience by a simple "user" willing to trudge through esoteric knowledge in order to accomplish a task. That's pretty similar to my friend, above, who just wanted to play a game at a boring job and also for the people that were looking to hire me, simply because they knew blowupstuffs or other things but had no idea of "computer." :)

Good work, felter, way to make the computer work for you! And, if you're ever interested in a job developing military weapons systems or certain esoteric computer practices, be sure to promptly submit your resume to the relevant government agencies and contractors. :)

User avatar
red assassin
Posts: 4613
Joined: Sun, 15. Feb 04, 15:11
x3

Post by red assassin » Sat, 15. Jul 17, 00:26

UniTrader wrote:
Antilogic wrote:Ewwww batch :P
no bashing here please :D :P
Ah, batch, the only time I ever find myself wishing I was writing bash instead. What is it with shell scripting languages and being utterly awful?
A still more glorious dawn awaits, not a sunrise, but a galaxy rise, a morning filled with 400 billion suns - the rising of the Milky Way

User avatar
jack775544
Posts: 1277
Joined: Tue, 13. Dec 11, 08:27
x4

Post by jack775544 » Sat, 15. Jul 17, 03:19

Have you ever had a look at Powershell?
It has been included with every version of Windows for a long while now and it is a lot nicer then batch scripting IMO (I prefer Powershell to Bash as well now)
1940s - Various "computers" are "programmed" using direct wiring and switches. Engineers do this in order to avoid the tabs vs spaces debate.

User avatar
red assassin
Posts: 4613
Joined: Sun, 15. Feb 04, 15:11
x3

Post by red assassin » Sat, 15. Jul 17, 09:41

Powershell at least lives up to the name, which is a definite advantage over batch, but I still find it a really ugly language.
A still more glorious dawn awaits, not a sunrise, but a galaxy rise, a morning filled with 400 billion suns - the rising of the Milky Way

User avatar
felter
Posts: 6970
Joined: Sat, 9. Nov 02, 18:13
xr

Post by felter » Sat, 15. Jul 17, 16:53

You know sometimes it's better to go old school, and this is one of those times. It would not have been able to do what I did here using powershell. Let me explain, if you read what I did then you would have noticed the challenge was to start 4 programs with a single click from the start menu. Two of those programs Action! and Voice attack both require to be elevated, they need to be run in administrator mode and when you do that you get this:

Image

Which means you need to click on yes to run them. But the challenge remember was to start these four programs with a single click, so I had to do this without having the UAC box appearing. To do this I decided to use Task Scheduler, to give them permission to run without that little box appearing. Now here's the thing, to be able to do anything anywhere near this using the powershell you actually need to run an elevated Powershell, which means you get that little UAC box appearing. So right of the bat you use your single click to launch the powershell, but you then need another mouse click to click on that little yes button. As far as I can tell it is not possible to do this without running an elevated powershell, so it is not possible to do the challenge using the powershell. Unless of course someone here can show me how to.

Just because something is new(er) and shiny, does not necessarily equate to better.
Florida Man Makes Announcement.
We live in a crazy world where winter heating has become a luxury item.

User avatar
Antilogic
Posts: 7526
Joined: Wed, 6. Apr 05, 20:33
x3tc

Post by Antilogic » Sat, 15. Jul 17, 18:24

So, you circumvented security for "ease of use". ^^

And yeah newer does equal better here.

Also, it's 100% possible to duplicate all of the stuff in the OP with PowerShell. You could also batch the creation of the task scheduler.

However, I wouldn't, because circumventing the UAC control is more then a little unwise.

User avatar
felter
Posts: 6970
Joined: Sat, 9. Nov 02, 18:13
xr

Post by felter » Sat, 15. Jul 17, 18:53

Antilogic wrote:So, you circumvented security for "ease of use". ^^

And yeah newer does equal better here.

Also, it's 100% possible to duplicate all of the stuff in the OP with PowerShell. You could also batch the creation of the task scheduler.

However, I wouldn't, because circumventing the UAC control is more then a little unwise.
Circumnavigating it is fine, turning it off is not. And you failed as soon as you used a batch, because the idea was to use powershell to do away with both the batch file and the CMD. I never said powershell couldn't do it, I said it would take two mouse clicks to do it with powershell.

So once again, prove it show me how. Remember no old school, so no Batch and no CMD.
Florida Man Makes Announcement.
We live in a crazy world where winter heating has become a luxury item.

UniTrader
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 14571
Joined: Sun, 20. Nov 05, 22:45
x4

Post by UniTrader » Sat, 15. Jul 17, 19:05

felter wrote:I never said powershell couldn't do it, I said it would take two mouse clicks to do it with powershell.
if the Goal is doing it in one mouse Click then Poweshell can not do it, you just said it yourself :P
if not stated otherwise everything i post is licensed under WTFPL

Ich mache keine S&M-Auftragsarbeiten, aber wenn es fragen gibt wie man etwas umsetzen kann helfe ich gerne weiter ;)

I wont do Script&Mod Request work, but if there are questions how to do something i will GLaDly help ;)

User avatar
Antilogic
Posts: 7526
Joined: Wed, 6. Apr 05, 20:33
x3tc

Post by Antilogic » Sat, 15. Jul 17, 19:37

felter wrote: Circumnavigating it is fine
No. The end user should know if something is being launched with administrative rights. Working around it is just training people to continue to work around it and not be aware of when programs are running in an elevated fashion.
And you failed as soon as you used a batch
Sigh, I was just referring to the "SC" command in batch to skip the GUI interface if you wanted to do this in batch. If you want to do the equivalent in PS, then New-ScheduledTask & Start-ScheduledTask and it's family of cmdlet friends will take care of it. You need W8+, because it accesses parts of the OS outside of .net which don't exist in W7. "SC CREATE" (& friends) work in batch & PS for W7 but are a bit more fiddly.

User avatar
jack775544
Posts: 1277
Joined: Tue, 13. Dec 11, 08:27
x4

Post by jack775544 » Mon, 17. Jul 17, 02:16

felter wrote:So once again, prove it show me how. Remember no old school, so no Batch and no CMD.
Now I don't have all the programs you have installed so this method is going to be untested for your software but it will work in theory.

Assuming that all the tasks in the task scheduler are set up in the OP like you showed the following script would do it.

Code: Select all

Invoke-Item "M:\Games\EDLaunch\EDLaunch.exe"
Invoke-Item "C:\Program Files (x86)\EDMarketConnector\EDMarketConnector.exe"
Start-ScheduledTask -TaskName "Voice Attack"
Start-ScheduledTask -TaskName "Action"
Save that above code as SomeName.ps1. The only extra step you would have to do is change the Windows file association of ps1 files to be powershell instead of the default of notepad.

I tested a similar script that started a task with highest privileges and no admin prompt was shown.
felter wrote:Just because something is new(er) and shiny, does not necessarily equate to better.
The reason I like Powershell is not because I think it is a shiny new thing, but since I used to work with a lot of batch scripts in a prior job and found my life a lot easier when I switched to Powershell, especially for complex tasks. Batch has so many little 'gotchas' that it makes it a giant pain when something goes wrong.
1940s - Various "computers" are "programmed" using direct wiring and switches. Engineers do this in order to avoid the tabs vs spaces debate.

pjknibbs
Posts: 41359
Joined: Wed, 6. Nov 02, 20:31
x4

Post by pjknibbs » Mon, 17. Jul 17, 09:31

jack775544 wrote: Save that above code as SomeName.ps1. The only extra step you would have to do is change the Windows file association of ps1 files to be powershell instead of the default of notepad.
You would also need to change the Powershell default setting which means it won't run an unsigned script, since there's no way you can sign the script yourself AFAIK.

User avatar
Antilogic
Posts: 7526
Joined: Wed, 6. Apr 05, 20:33
x3tc

Post by Antilogic » Mon, 17. Jul 17, 10:00

pjknibbs wrote:
jack775544 wrote: Save that above code as SomeName.ps1. The only extra step you would have to do is change the Windows file association of ps1 files to be powershell instead of the default of notepad.
You would also need to change the Powershell default setting which means it won't run an unsigned script, since there's no way you can sign the script yourself AFAIK.
You can sign it, but it's not necessary for home use. Execution Policies in PowerShell are more about stopping users that don't know what they are doing then any real security - since if you launch .ps1 from Task Scheduler for example, then it runs even if the execution policy is set to restricted.

-RemoteSigned is best practice, but it's not the end of the world if Unrestricted for a home user.

User avatar
jack775544
Posts: 1277
Joined: Tue, 13. Dec 11, 08:27
x4

Post by jack775544 » Mon, 17. Jul 17, 11:57

pjknibbs wrote:You would also need to change the Powershell default setting which means it won't run an unsigned script, since there's no way you can sign the script yourself AFAIK.
I entirely forgot about that since doing that is one of the first things I do when setting up a new computer :D
1940s - Various "computers" are "programmed" using direct wiring and switches. Engineers do this in order to avoid the tabs vs spaces debate.

User avatar
Antilogic
Posts: 7526
Joined: Wed, 6. Apr 05, 20:33
x3tc

Post by Antilogic » Mon, 17. Jul 17, 12:09

jack775544 wrote:
pjknibbs wrote:You would also need to change the Powershell default setting which means it won't run an unsigned script, since there's no way you can sign the script yourself AFAIK.
I entirely forgot about that since doing that is one of the first things I do when setting up a new computer :D
Set-ExecutionPolicy guillotine

Post Reply

Return to “Off Topic English”