DosTips.com

A Forum all about DOS Batch
It is currently 01 Jul 2016 05:33

All times are UTC-06:00




Post new topic  Reply to topic  [ 15 posts ] 
Author Message
PostPosted: 16 Apr 2012 02:51 
Offline

Joined: 08 Dec 2011 12:14
Posts: 61
HI

In Windows 7, there is a registry key/entry that allows to run a DOS command with options when I reboot the system (this command will have to be performed only on the first reboot)?

THANKS

BYE


Top
   
PostPosted: 16 Apr 2012 04:39 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5702
google for "runonce"


Top
   
PostPosted: 16 Apr 2012 10:40 
Offline

Joined: 08 Dec 2011 12:14
Posts: 61
There is a problem:

The runonce key is performed in the system starting phase and not during the system shutdown phase. Am I wrong?

THANKS

BYE


Top
   
PostPosted: 16 Apr 2012 10:46 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3099
You seem to be contradicting yourself. Do you want it to run on startup or shutdown?


Top
   
PostPosted: 16 Apr 2012 11:12 
Offline

Joined: 08 Dec 2011 12:14
Posts: 61
During the shutdown.

THANKS

BYE


Top
   
PostPosted: 16 Apr 2012 11:49 
Offline

Joined: 28 Jun 2010 03:46
Posts: 250
- start -> run -> gpedit.msc
- Under computer configuration -> windows settings -> scripts -> right click on shutdown -> select properties -> add the name of the program you want to run (.cmd, .bat, .vbs...). Just try it.
I heared once that there might be a time limit on this - you better check this.

Saso


Top
   
PostPosted: 16 Apr 2012 12:07 
Offline

Joined: 08 Dec 2011 12:14
Posts: 61
Instead, if I have Windows 7 Home Premium, how do I solve this problem?

THANKS

BYE


Top
   
PostPosted: 16 Apr 2012 16:42 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5702
.


Top
   
PostPosted: 18 Apr 2012 21:03 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
this is the registry keys for runonce jobs:
Code:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce


u can add jobs using batch like that:
Code:
@echo off
cls
set regkey1=HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
set regkey2=HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
:: = = = = = = =
REG add "%regkey1%" /v "job_name" /t "job_type" /d "job_command" /f >nul

Replace the "job_name" with the title of the job u going to add.
Replace the "job_type" with the type of the registry key use "REG_SZ" it will work.
Replace the "job_command with the program u want to launch and u can add special switches to it like "c:\test\example.exe /s"

if running windows 7 u will need permission to modify Registry.


Top
   
PostPosted: 19 Apr 2012 00:56 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5702
@abc0502 The fellow wasn't clear - he wants to run a task when he *shuts down* the PC.


@op

One batch solution is to run the task in a batch file and then use shutdown.exe after it, so that running the batch file performs your tasks and then your machine shuts down.


Top
   
PostPosted: 19 Apr 2012 01:43 
Offline

Joined: 08 Dec 2011 12:14
Posts: 61
abc0502 wrote:
this is the registry keys for runonce jobs:
Code:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce


u can add jobs using batch like that:
Code:
@echo off
cls
set regkey1=HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
set regkey2=HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
:: = = = = = = =
REG add "%regkey1%" /v "job_name" /t "job_type" /d "job_command" /f >nul

Replace the "job_name" with the title of the job u going to add.
Replace the "job_type" with the type of the registry key use "REG_SZ" it will work.
Replace the "job_command with the program u want to launch and u can add special switches to it like "c:\test\example.exe /s"

if running windows 7 u will need permission to modify Registry.


There is a problem:

The runonce key is performed in the system starting phase and not during the system shutdown phase. Am I wrong?

THANKS

BYE


Top
   
PostPosted: 19 Apr 2012 02:15 
Offline

Joined: 03 Apr 2010 20:27
Posts: 109
Location: Wellington, New Zealand
Correct, runonce is for when the system is starting.

Maybe we could rename shutdown.exe to something else and put another exe in its place. This exe could store the argument that was intended for the shutdown command (-s, -r etc.), then runs a batch file we want to run on shutdown and then runs the original shutdown.exe (the one we renamed) with the argument we collected earlier....?

I know, I know, it's a bit iffy when messing with system files... Just a thought.

Hmm.. this is an afterthought... if an application uses a system call to shut the computer down, the solution I suggested may not work as Windows may not use shutdown.exe to shut the machine down when a system call is used to initiate a shutdown..... Can anyone confirm this?

Thanks
Phillid


Top
   
PostPosted: 19 Apr 2012 02:48 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5702
When Windows shuts itself down I'd think it uses an API function and not shutdown.exe

A test would be to rename shutdown.exe and see if Windows can be shut down.


The OP ignored my suggestion, so maybe it wasn't clear enough.
You can have a batch file like this to run a program before the machine shuts down.

@echo off
start "" /w "c:\program files\my application.exe"
shutdown.exe -s


Top
   
PostPosted: 19 Apr 2012 02:54 
Offline

Joined: 03 Apr 2010 20:27
Posts: 109
Location: Wellington, New Zealand
Yeah that's what I thought.. :/ Time to decompile Windows, edit it and recompile ;)

The only problem with that batch file is that it won't run as a result of a shutdown... I think that's what he wants. Your batch shuts down the computer itself... Maybe he needs to clarify.

Anyway, time for me to have a coffee, enjoy the rest of the evening and get some sleep.

Thanks
Phillid


Top
   
PostPosted: 19 Apr 2012 03:18 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
sorry i didn't notice shutdown,
as miskox said use the group policy,
This should add registry keys to the group policy to run when shuting down the pc:
i tested it to add the registry keys but didn't test to start that keys:
Code:
@echo off
cls

:: This is the main key and must be exist once to add jobs,
:: "regkey_main=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0"

:: This key is for job #1:
:: "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0\0"
::This key is for job #3:
:: "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0\1"
:: If Add 3rd Job the key should look like: "notice the last digits"
:: "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0\2"

:: ===== Change This section Only with your settings =====
set "regkey=%temp%\reg.reg"

set "scriptname1=Test1"
set "parameters1=D:\example1.exe /s"

set "scriptname2=Test2"
set "parameters2=D:\example2.exe /s"

:: you can add more jobs by duplicating the last set of commands and consider the last number in each regkey address "0\0" for job 1 and "0\1" for job 2  etc,...
:: the %regkey_main appear once and don't repaeat it
:: =============== End Section ================
:: This is The Reg.Reg Key that will add the tasks executed at the shutdown ::
IF EXIST "%regkey%" ( DEL /F /Q "%regkey%
) Else ( goto next )
:next
echo: Windows Registry Editor Version 5.00 >>%regkey%
echo:
echo: ^[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0^] >>%regkey%
echo: "GPO-ID"="LocalGPO" >>%regkey%
echo: "SOM-ID"="Local" >>%regkey%
echo: "FileSysPath"="C:\\Windows\\System32\\GroupPolicy\\Machine" >>%regkey%
echo: "DisplayName"="Local Group Policy" >>%regkey%
echo: "GPOName"="Local Group Policy" >>%regkey%
echo: "PSScriptOrder"=dword:00000001 >>%regkey%
echo:
echo: ^[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0\0^] >>%regkey%
echo: "Script"="scriptname1" >>%regkey%
echo: "Parameters"="parameters1" >>%regkey%
echo: "IsPowershell"=dword:00000000 >>%regkey%
echo: "ExecTime"=hex^(b^):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00 >>%regkey%
echo:
:: The next commands is for a second job u can delete it if u have only one job and add one like it at the end if u have more jobs just notice the last digit of the reg-key
:: >"0\1"<   this mean 2nd job and if add 3rd job it will be like that   >"0\2"<   at the end of the same key name and the only change will be in   >"script"<   and   >"parameters"<.
echo: ^[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0\1^] >>%regkey%
echo: "Script"="scriptname2" >>%regkey%
echo: "Parameters"="parameters2" >>%regkey%
echo: "IsPowershell"=dword:00000000 >>%regkey%
echo: "ExecTime"=hex^(b^):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00 >>%regkey%

Reg Import "%regkey%" >nul


just change the section pointed to in the batch and run it with administrator rights i tested in win7


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 15 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Bing [Bot], dbenham, Google [Bot], Yahoo [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited