How to stop a service delete files and restart service in a script

WeirdFishes
WeirdFishes used Ask the Experts™
on
Gents,

I would like some help in creating a Script that can stop a service> delete some files> restart the service.

*I would like to run it from my machine (client, winodws 7)
*The service i want stop and start and files to delete are on a MS 2008 Box
*I would like to use our support account that is part of the Domain admins group
*I have run the content of the script manually on the server in cmd and works fine but doesn't work as a script using runas on my machine.

So far i have

net stop "Service"
del "files path to delete"
net start "Service"

I played around with runas /u: /netonly but it looks like i'm just not getting it right

Thanks for the help in advanced.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You could use netsvc.exe to accomplish the start/stop tasks.
For deleting files use an UNC path.

The user running the script needs permission on the remote machine.
TechnoChatWintel Administrator / Cloud Computing

Commented:
Try PS Service from MS.
http://technet.microsoft.com/en-us/sysinternals/bb897542.aspx

Thanks
Saugata
@echo off
psservice \\192.168.44.13 -u username -p password stop spooler
del "files path to delete"
psservice \\192.168.44.13 -u username -p password start spooler

Open in new window

TechnoChatWintel Administrator / Cloud Computing

Commented:
Note: Here using spooler as an example

Thanks
Saugata

Author

Commented:
thanks for the response.

When i run the script i get the error msg "Unable to connect to \\192.168.
Multiple connections to a server or shared resource by the same user, using more
 than one user name, are not allowed. Disconnect all previous connections to the
 server or shared resource and try again*

Any ideas

PS: i'm using psservice

@echo off
psservice \\192.168. -u username -p password stop spooler
del "files path to delete"
psservice \\192.168. -u username -p password start spooler


You haven't got the full IP address for the machine you are trying operate on.  An IP address requires 4 sets of numbers; in the code you show you only have two sets of numbers.

Your example:
psservice \\192.168. -u username -p password stop spooler

Example given by TechnoChat:
psservice \\192.168.44.13 -u username -p password stop spooler

You need to use the full IP address or, if the computer is on the same network, you can use the computer name.  If you don't know the computer name, but have access to it typing:

echo %COMPUTERNAME%

at the dos prompt will supply the computer name.  The psservice command would then become:

psservice \\TheComputerName -u username -p password stop spooler


I suggest you use the computer name, because I doubt the IP address will remain the same.
You understand that you have to replace "username" and "password" in the psservice command with a username and password that will log on to the computer whose name you have used?  And you have to replace "spooler" with the name of the particular service you want to stop/start?
TechnoChatWintel Administrator / Cloud Computing

Commented:
Thanks LeeeRussel, for helping the user ..

@WeirdFishes, hope now your problem will solve.

Thanks
Saugata

Author

Commented:
Thanks LeeRussell,

but yes i have the full ip address in the script, i'm a bit security concious so i only revelead 2 octets so yes i didn't add the last 2 octetes in the post (my fault should have mentioned) IP address will be fine (never heard of someone that gives a server a dynamic ip address). i doubt it would give you that error message any when the machine doesn't can not be found let alone talk about user access.

back to error msg
*I ran the sript and made sure that that particular user was not logged on by deleting all sessions on the server (ip adress in the script) in Task Manager > Users
*It's a file server so i have a few mapped drives to it
*the del ""path to files"still works as it prompts for confirmation

Cheers

Commented:
Try this: on a command prompt and type net use.
If you had a resource showing there, you have to delete it using the command
net use /delete \\servername\sharename

net use * /delete   --will delete any shared resources

Other info here http://support.microsoft.com/kb/938120

Author

Commented:
Gents,

I ran psservice from cmd (on my machine) and it works fine, for some reason it's not working from a script. Also it's not working with the file&print servers ip address but thats not important.

net use * /delete
psservice \\computername -u username -p password stop spooler

the purpose of the script is to stop the spooler service delete the job queue and restart the spooler service.

Cheers

Author

Commented:
the error msg i'm getting is

Unable to connect to \\computername:
Logon failure: unknown user name or bad password.
Press any key to continue . . .

i added a pause statement at the end to see any error messages.
TechnoChatWintel Administrator / Cloud Computing

Commented:
Try the attached script and modify only following lines..

::Set username bellow(username or domain\username)
set usname=
::Set password bellow
set passwd=
::Set system Name bellow(\\ip or \\hostname)
set sysnam=
::Set service Name bellow
set srname=

Thanks
Saugata
@echo off
setlocal
%~d0
cd "%~dp0"
::Set username bellow(username or domain\username)
set usname=
::Set password bellow
set passwd=
::Set system Name bellow(\\ip or \\hostname)
set sysnam=
::Set service Name bellow
set srname=
::Main
psservice %sysnam% -u %usname% -p %passwd% stop %srname%
net use * /delete /y
psservice %sysnam% -u %usname% -p %passwd%start %srname%
exit

Open in new window

Author

Commented:
Hi TechnoChat,

Unfortunately it didn't work as well even after adding the space in %passwd%start on line 16. Any other ideas?

Wintel Administrator / Cloud Computing
Commented:
Can you tell me one thing that are you able to execute the following command from the system where you are going to scheduled the script.

psservice \\IP or HOSTNAME -u username -p password stop spooler


Also you can not execute net use * /delete /y from your system to delete shares from the server. You have to user psexec to do the same.

Use this.
psexec \\IP or HOSTNAME -u username -p password net use * /delete /y

PsEXEc
http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

Also tell me that, both of the system are in the same domain?

Thanks
Saugata

Author

Commented:
Hi TechnoChat,

I can start and stop the services now but can't delete the spooler files in between stoping then starting the service. in the batch i have tried

these line are just to delete the spooler

psexec \\computername -u domain\user cmd /c del /s /q "C:\Windows\System32\spool\PRINTERS\*.*"

above doesn't work

psexec \\computername -u domain\user net use C:\Windows\System32\spool\PRINTERS\* /delete /y

above doesn't work


TechnoChatWintel Administrator / Cloud Computing

Commented:
Hi,
Use attached one, and it's working fine from here..

Check the attached screen shot.. :)

Thanks
Saugata
@echo off
setlocal
%~d0
cd "%~dp0"
::Set username bellow(username or domain\username)
set usname=
::Set password bellow
set passwd=
::Set system Name bellow(\\ip or \\hostname)
set sysnam=
::Set service Name bellow
set srname=
::Main
psservice %sysnam% -u %usname% -p %passwd% stop %srname%
psexec %sysnam% -u %usname% -p %passwd% cmd /c del /s /q /f "C:\Windows\System32\spool\PRINTERS\*.*"
psservice %sysnam% -u %usname% -p %passwd% start %srname%
exit

Open in new window

scri.GIF

Author

Commented:
solved issue myself

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial