Solved

Script to login to remote share and run batch file

Posted on 2014-09-11
5
509 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
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…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

863 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

27 Experts available now in Live!

Get 1:1 Help Now