Solved

Script to login to remote share and run batch file

Posted on 2014-09-11
5
520 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 36

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 Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to count occurrences of each item in an array.
Progress

690 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