Desktop shortcut to restart a windows service - windows

Is it possible to create a windows desktop shortcut that will restart a windows service?
I'd like a button to restart my apache service after I have made changes to the config file.

You can do this in a batch file, then make a shortcut to it.
Create a text file with the following content, but save it with the file extension .bat
net stop "Service Name"
net start "Service Name"
Once the file exists, you can create a shortcut to it, and even assign a keyboard shortcut too if deemed necessary.

You can accomplish this without a batch file using the following shortcut target:
C:\Windows\System32\cmd.exe /c "net stop "Service Name" & net start "Service Name""
In addition to the answer the following comment by Tibo is mandatory:
To make it run as Administrator (required for some services, maybe all?), in the shortcut's properties window, tab Shortcut, clic on the button "Advanced..." (at the bottom) then check "Run as administrator". It will open the User Account Control popup each time.

I'm using a system consisting of a simple CMD batch script and an LNK shortcut. The batch script contains the sc command, which acts as a Windows services controller. For starting or stopping a service it uses the same parameters as the net command:
sc <start|stop> <service>
So, e.g. for starting Apache web server service and MySQL database server service, the batch script named web_servers_start.cmd could look like the following:
sc start "Apache2.2"
sc start MySQL
The batch script must be launched elevated as an administrator. So I created a LNK shortcut which points to the batch script web_servers_start.cmd and checked "Run as administrator" in the file's Properties dialog under the "Advanced..." button on the Shortcut tab.
You can place the LNK shortcut on the desktop, start menu or wherever you prefer.
Note: One of differences between the sc and net commands is that sc sends a message (e.g. start) to a service and ends itself immediately while net waits until service operation is done. If you don't need to manipulate with operation status or error code, sc command is much faster.

Related

How to make the cmd line executable

Here's the situation, every time I need to do the step below after I boot my computer:
Open the cmd(admin)
copy this cmd into cmd:
C:\Windows\system32\netsh int ipv4 set glob defaultcurhoplimit=65
enter
For now, I hope to double click a file that can directly execute the command above without the step above.
Any method?
You can save it to .bat or .cmd file and run it on double-click.
Create a file with any name and extension .bat I.g. setLimit.bat. I assume you know how to create files.
Open file from above step in file editor, i.g. notepad.exe. Copy and paste line C:\Windows\system32\netsh int ipv4 set glob defaultcurhoplimit=65 into the file and press Ctrl+S to safe it. I hope it makes sense otherwise ask.
Double click on the file to execute the command.
If you need to run it under elevated privilege (i.e. admin) right click on file and choose Run as administrator option in menu.
You need a .bat file to execute the command and a shortcut to have it executed as administrator.
Creating the .bat file is trivial: use you favorite text editor (or notepad), type the command, optionnaly add a line PAUSE if you want to see eventual messages and save it to disk with a .bat extension (say c:\...\sethoplimit.bat)
Creating the shortcut is not much harder: right click on desktop, Create new..., Shortcut, and just select the newly created file
The magic comes now: right click on the newly created shortcut and click Properties, in Shortcut tab, click the Advanced button and select Execute as admin.
That's all. Each time you double click on the shortcut, Windows will start a console with elevated priviledges, eventually open the User Account Control confirmation dialog, and execute the batch file as administrator.
Here I showed you how to create the shortcut directly on the Desktop, but the same process can be used for a shortcut anywhere on a disk folder.

Running the cmd in windows command line as admin

Am opening windows cmd.exe as administrator and executing an installer(just call installer.exe) and it runs fine. But if I open the cmd.exe in normal mode ( not as administrator) but run the command as user administrator ( runas /profile /user:adminstrator installer.exe) am not able to execute the installer successfully.
The installer unpacks certain files in c:\users\ dir.
The error that I get is :
"Error running java -Dpython.console.encoding=UTF-8 -jar C:\users\<username>/tools/x.jar : Program ended with an error exit code. "
How can I solve this issue? Since am trying to automate executing this installer, opening the cmd.exe as admin is out of question. I would like to run the command as a normal user or if not possible, as an admin.
Am new to Windows. Any help is appreciated.
Thanks
You could create the .bat file with the line you want to run, then follow the procedures listed below to have it automatically run as an administrator.
Right click on the original file and click Create shortcut.
Right click on the shortcut and select the properties option.
Under the shortcut tab, click on the advanced button in the bottom right hand corner.
Check the box that says run as administrator
Click ok, and then ok.
Now when you double click on the batch file shortcut it will run as an administrator. You can delete the original if you wish to.

auto run a bat script in windows 7 at login

I have recently acquired a windows 7 laptop from my late grandmother.
I have been using it for work and other things. I decided to create a VM using VirtualBox And now I want to create a user on the (Windows 7 Host) machine so that when I log into that user it autoruns a .bat script to start the VM. To make it clear I only want to run it only if the user "VM" logs in and not my normal user and it would be super awesome if it would autostart in full screen. I have a shortcut on my desktop that executes the command:
"C:\Program Files\Oracle\VirtualBox\VirtualBox.exe" --comment "VM" --startvm "12dada4d- 9cfd-4aa7-8353-20b4e455b3fa"
but how do I make an autorun.bat when I log into the User "VM"?
To run the batch file when the VM user logs in:
Drag the shortcut--the one that's currently on your desktop--(or the batch file itself) to Start - All Programs - Startup. Now when you login as that user, it will launch the batch file.
Another way to do the same thing is to save the shortcut or the batch file in %AppData%\Microsoft\Windows\Start Menu\Programs\Startup\.
As far as getting it to run full screen, it depends a bit what you mean. You can have it launch maximized by editing your batch file like this:
start "" /max "C:\Program Files\Oracle\VirtualBox\VirtualBox.exe" --comment "VM" --startvm "12dada4d-9cfd-4aa7-8353-20b4e455b3fa"
But if VirtualBox has a truly full-screen mode (where it hides even the taskbar), you'll have to look for a command-line parameter on VirtualBox.exe. I'm not familiar with that product.
I hit this question looking for how to run batch scripts during user logon on a standalone windows server (workgroup not in domain). I found the answer in using group policy.
gpedit.msc
user configuration->administrative templates->system->logon->run these programs at user logon
add batch scripts.
you can add them using cmd /k mybatchfile.cmd if you want the command window to stay (on desktop) after batch script have finished.
gpupdate - to update the group policy.
Just enable parsing of the autoexec.bat in the registry, using these instructions.
:: works only on windows vista and earlier
Run REGEDT32.EXE.
Modify the following value within HKEY_CURRENT_USER:
Software\Microsoft\Windows NT\CurrentVersion\Winlogon\ParseAutoexec
1 = autoexec.bat is parsed
0 = autoexec.bat is not parsed

How to use runas command in windows 2008 r2 server?

I have written a batch file which will call another batch file and delete some files. For this I need to have admin rights. I tried following command...
runas /user:bala#nsc cmd
Enter the password for bala#nsc: xxxxxxx
Even though bala#nsc has admin rights command prompt is getting opened as a simple user rather than administrator.
I guess, I am missing something. Please help me.
Your results aren't what I expected, either. But I've got a few workarounds to suggest.
You could turn off User Account Control.
You could go to Start --> All Programs --> Accessories, then right-click on "Command Prompt" and choose "Run as Administrator."
You could right-click on your batch script and do the same.
You could create a shortcut to either cmd or your batch script on your Desktop, then modify the properties of that shortcut to run as Administrator.
You could add some code to your batch script to check for admin privileges and prompt for escalation if needed.
Turning off UAC would be my choice.

Batch: start application doesn't show a GUI

I try to launch a self written autoit application called "KeyShortcuts.exe" using a batch called "launchMacros.bat". This applications provides keyboard shortcuts for various things and includes a GUI which shows me the available shortcuts.
launchMacros.bat:
start "MyMacros" "M:\applications\AutoIt\KeyShortcuts.exe"
The application does start and I'm able to use every shortcut but I'm not able to see the GUI.
If I start the application direct (double click on KeyShortcuts.exe) everythings works fine.
I also tryed starting the application using runas:
runas /user:REQUIREDUSERNAME /savecred "M:\applications\AutoIt\KeyShortcuts.exe"
Same problem here. Even right click -> "Run as administrator" doesnt worked.
Any suggestions?
If your batch file is in a different directory than KeyShortcuts.exe, you may need to specify the starting directory using the /D parameter for START.
Like this:
START "MyMacros" /D "M:\applications\AutoIt" "M:\applications\AutoIt\KeyShortcuts.exe"
Every batch file launched from Windows GUI create a new console window, run the batch file, then close. If you need this to be different, there's several ways:
Create a shortcut to CMD /K YOURBATCHFILE.BAT
Add a pause to your BAT file
Here's a demonstration of method 1:
New > Shortcut
Type the location of the item: C:\Windows\System32\CMD.EXE
Type the name for this shortcut: InsertYourNameHere
Click Finish
Right click on your Shortcut and go properties
Change Target to: C:\Windows\System32\CMD.EXE /K "InsertYourBatchFileName.BAT"
Click OK
Done, now you have a shortcut that opens a new console window and leaves it open whilst ir runs your batch file.

Resources