Solved

Script to login to remote share and run batch file

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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