Showing posts with label .bat file. Show all posts
Showing posts with label .bat file. Show all posts

Monday, January 6, 2014

What is w32tm.exe in Windows?

If you are always using new computers or if you frequently upgrade machines, you will not face the issue of setting time each and every time you start computer. In my case, I am now using my old computer and its battery is in bad state to keep up the time when I turn off system.

Since I am using it mainly for browsing internet will be always there even though it is low speed. So decided not to replace the batter instead sync the time time with internet servers when ever I turn on the computer.Since I am using Win 7 I am able to do this by below steps

  1. Right click on the digital time shown in task manager and select 'Adjust date / time'
  2. In the opened dialog select 'Internet Time' tab
  3. Click on 'Change Settings'
  4.  Tick the "Synchronize with an Internet time server'
But this does syncing in a scheduled fashion. People says it differently such as one week and we can change by editing the registry etc..Somebody says the setting is only to make sure the time service is running not to correct the time.

But what I need is to set the time after I succeeds in dialing for internet. I can force the sync by using the 'Update now' button in the dialog.But it is too time consuming. So what should I do?

As per my belief, programmer should first write code to solve his problems before he code for others. So started digging for snippets which does this. This time cleverly didn't google for C#. Started with PowerShell as my aim was to have a simple script file which I can double click after connecting to internet. I got some snippets which does more like hitting the time server and updating the time. These are not the command which invokes the 'Update now'. As Windows is following the philosophy of command first approach, I was sure that I can find the corresponding command and I got after some more google. Its nothing but W32tm.exe located at <system drive>:\Windows\System32

Running w32tm.exe

When I simply run the command in the PowerShell window I got the below error

The following error occurred: The service has not been started. (0x80070426)

This is a good finding. Its looking for a service. Is it looking for a windows service in local machine or the internet time server service? I had disabled all the unfamiliar services in my machine as its too slow. After going thorough the services list, I would see one service names "Windows Time" which seems related to the above error message. I cannot enable it permanently as the machine is slow. So wrote in code itself.

net start w32time
w32tm.exe /resync
net stop w32time

When I run this ends with Access is denied.

It is little difficult to run the script in admin privilege in PowerShell. Either we need to have one file to start the actual file or we need to manually open cmd window as admin them execute the required script. So thought of moving top old favorite .bat script. Yes it worked

@echo off
net start w32time
w32tm.exe /resync
net stop w32time
pause

One time I got one issue which tells, cannot do sync as the time difference is big. In those situations just use the /force switch along with /resync. Explore the windows documentation about w32tm if you are interested.

Happy scripting.

Tuesday, November 27, 2012

Run exe using NetworkService account

Sometime back we had a requirement of running an exe in the context of network user. Initially it looked very easy. But when I tried to impersonate the execution, I realized the difficulty. It is easy to impersonate as a standard user .But cannot impersonate as NetworkService. So what are the alternatives.

  1. Run a windows service and start the process/executable from that service.
  2. Schedule a task and specify target as our exe and use NetworkService to initiate execution. Delete the task after execution.

Run exe through windows service

This is a simple matter or creating a windows service and executing a exe when we start the service. Immediately after completion of exe stop the service.This can be summarized as

  1. Create a simple windows service
    1. This should accept an exe path through the OnStart() method.
    2. Execute the exe.
    3. Stop the service by calling Stop() method.
  2. Use the sc start command to start the service and pass path as param.

You may download the program from here.

Schedule task on behalf of NetworkService

This too simple. The steps are

  1. Create a scheduled task pointing to your application using ‘schtasks’ command.
    1. Use “NT AUTHORITY\NETWORK SERVICE" as user profile
    2. Give the run frequency as onlogon. This is to ensure that, it will never run before we trigger.
    3. eg: <drive>:\<folder>>schtasks /create /tn testser /sc onlogon /ru "NT AUTHORITY\NETWORK SERVICE" /tr d:\Temp\HelloWorld.exe (No way to reduce params)
  2. Run the task
    1. schtasks /run /tn "testser"
  3. Delete the task
    1. schtasks /delete /tn testser /f (/f is to delete forcefully.Else it will ask a confirmation)

Better wrap this into a single batch file which accepts the exe name and run the same in the context of ‘NetworkService’. ie you can use like RunAsNetworkService <file path>

NB: If you simply create the task using command, it will not run, it is in a portable system such as laptop and it is in battery during invocation. To resolve that, you need to create an xml file where you can specify to enable task running in batteries and point the same xml in the task creation command.Below links explains how to create task with xml file option.

http://stackoverflow.com/questions/9075564/change-settings-for-power-for-windows-scheduled-task
http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx

Sunday, July 4, 2010

For Loop in .Bat file

echo off
for %%v in (one two three) do (
echo %%v
)




This is simple usage of for loop in batch file.This can be extended to file reading and more.