Solved

Script to login to remote share and run batch file

Posted on 2014-09-11
5
501 Views
Last Modified: 2014-09-25
I need help writing a script that will login to a shared folder on a server in another domain (it requires username and password authentication - then the shared folder is available). Let's say I'm on COMPUTER1.mydomain.com trying to login to a share on SERVER1.thatdomain.com for instance, when I do this I have to authenticate with username and password. I put in my credentials and authenticate with no problem, then I execute a batch file to run on SERVER1.thatdomain.com.

I do not want the script to execute locally. So will I need to specify the server and login within the script to have it execute on SERVER1.thatdomain.com?

The idea is to restart a service on the server named SERVER1.thatdomain.com, but I have to athenticate first from COMPUTER1.mydomain.com.

Can I do this with a batch or powershell? What is best? Thank You!
0
Comment
Question by:Noah_Williams35
5 Comments
 
LVL 16

Assisted Solution

by:choward16980
choward16980 earned 250 total points
Comment Utility
I use PSEXEC:
http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

Then, you can easily run the following commands remotely:

net stop "service name"
net start "service name"

Open in new window



This is the command you would run on Computer1.mydomain.com:

PSEXEC \\server1.thatdomain.com -u %username% -p %pwd% -c net stop "service name"

PSEXEC \\server1.thatdomain.com -u %username% -p %pwd% -c net start "service name"

Open in new window

0
 
LVL 35

Expert Comment

by:Kimputer
Comment Utility
This exposes quite the security risk. It's better to leave a specified file on that share, and have a running program on the server, or scheduled task detect this file, and then run a specific command.
0
 

Author Comment

by:Noah_Williams35
Comment Utility
Can we use Task Scheduler on COMPUTER1.mydomain.com to kick off a on demand task when needed to execute a script that does what we need done on SERVER1.thatdomain.com?

I want to take the best approach given security also...
0
 
LVL 16

Expert Comment

by:choward16980
Comment Utility
Yes.  Just use psexec in accordance with the task scheduler or script.
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 250 total points
Comment Utility
If you just want to restart a service on there you can use sc.exe too btw from your end.

net use \\server1.thatdomain.com\ipc$ /user:username password
sc \\server1.thatdomain.com stop "service name"
sc \\server1.thatdomain.com start "service name"

Another approach is to have a scheduled task on the remote server which watches, say, every 5 minutes, for a flag file in a directory.  Drop a specific file there from your end and the service restarts and drops the status into another file. e.g.

On remote box, batch file scheduled periodically:

@echo off
if exist d:\shareddir\restart-flag.txt (
  echo %Date% %time%
  net stop service name
  net start service name
  echo %Date% %time%
) > d:\shareddir\restart-status.txt

On your end:

@echo off
net use \\server1.thatdomain.com\ipc$ /user:username password
echo Restart %date% %time% > \\server1.otherdomain.com\shareddir\restart-flag.txt

then the username only needs the rights to write a file into that share, nothing more.

Steve
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now