Solved

Script to login to remote share and run batch file

Posted on 2014-09-11
5
511 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
ID: 40318098
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
ID: 40318123
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
ID: 40318140
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
ID: 40318144
Yes.  Just use psexec in accordance with the task scheduler or script.
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 250 total points
ID: 40318325
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Do Psexec queries install files on remote computers 6 62
Recursively Delete Files 5 80
simplest php form 3 78
how to run this deletion request 4 21
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

776 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