• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 539
  • Last Modified:

Get a simple batchfile copied to a users PC and make it execute to copy a file from netwrk server to same users my documents folder!

My goal:

Copy a bat file to a network users machine using "psexec"

Once the file is there psexec must be configured to use the logged in users network drive mapping to cary out a bat file command which goes to a folder on the networks mapped drive, copies a file from this folder back to the current users "My Documents" folder.

This is an extremely hard one and should be worth 5000 points but i could only add 500!

I have been working on this for 2 days and still have yet to do it so I am open to any solutions.

Please include code for both the commands and switches to call psexec and also the bat file code.

A little explanation of how it all works would be nice too.

The issue is that i am running into is that when psexec connects to another PC it uses that PC's admin dos session and I cant get the doss bat file configured correctly to use the mapped drive of the logged in user..... just for your notes!
 
Thanks to anyone who could help!
0
HudsonMarine
Asked:
HudsonMarine
  • 10
  • 5
  • 4
1 Solution
 
igor-1965Commented:
Could you post the code you have worked out already? It might help to understand what you are doing and what you need to achieve.

Thanks
0
 
HudsonMarineAuthor Commented:
I created a batch file named update.bat which I can physically copy to all my users computer but for now i copied it to:
 
c:\test Folder on the destination PC
 
Here is the code for the bat file:
 
================
CD..
CD..
s:
S:
copy \ProdDB\CONDB\CONDB\HMMSA.mdb %USERPROFILE%\myDocu~1\TECHDB\
================
 
The object of this bat file is to copy a file from our public networkes mapped drive folder to the destinations userprofile\My Documents\TECHDB Folder
 
I have tested this while logged in as a user on the network and it copied the file just fine while i was physically at the destination PC
 
 
But this is the results of what i get when i execute the following psexec command from the remote PC:
 
psexec \\DESTINATION-PC C:\test\update.bat
 
=================================
RESULTS
=================================
PsExec v1.94 - Execute processes remotely
Copyright (C) 2001-2008 Mark Russinovich
Sysinternals - www.sysinternals.com
 
C:\WINDOWS\system32>The system cannot find the drive specified.
CD..The system cannot find the drive specified.
C:\WINDOWS>CD..
C:\>s:
C:\>S:
C:\>CLR
C:\>copy \ProdDB\CONDB\CONDB\HMMSA.mdb C:\Documents and Settings\username\myDoc
u~1\TECHDB\
The system cannot find the path specified.
c:\Docume~1\userna~1\update.bat exited on DESTINATION-PC with error code 1.
C:\>
 
----------------------------------------End Results
 
I also changed my bat file from this:

copy s:\ProdDB\CONDB\CONDB\HMMSA.mdb %USERPROFILE%\myDocu~1\TECHDB\

to use the UNC like this:

copy \\MYSERVER\ProdDB\CONDB\CONDB\HMMSA.mdb %USERPROFILE%\myDocu~1\TECHDB\

but it still says the same thing:

The system cannot find the path specified.


Thanks for anyone who can help.
0
 
igor-1965Commented:
update.bat is okay - problem resides elsewhere.

In PSEXEC you need to use option -c (the help is below):
  -c         Copy the specified program to the remote system for
              execution. If you omit this option the application
              must be in the system path on the remote system.

so you need to run command: psexec \\DESTINATION-PC -c C:\test\update.bat
In this case the batch will be passed to the target computer and then executed there. The batch is to be located in C:\test\ on YOUR computer, not the target one.

The main problem however is that when you use psexec \\DESTINATION-PC it runs on the target PC under YOUR account, not the user logged on there!

I would highly recommend you to use login script for the copying files that must be located under the logged user profile / shell folders. It will be a way easier to achive what you want with login script rather than with psexec.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
AmazingTechCommented:
I don't think it can be done. PSEXEC does not impersonate the logged on user. Nor can you access UNC paths. I've tried for years.

Try this:

PSEXEC \\DESTINATION-PC CMD
ECHO %username%

Is this your username or the logged on user's?

ECHO %USERPROFILE%

Is this your profile path or the logged on user's?


Why do you need to PSEXEC it? Why can't you just copy the file? Using logon script for the user or from your workstation.

copy \\MYSERVER\ProdDB\CONDB\CONDB\HMMSA.mdb "\\DESTINATION-PC\c$\documents and settings\username\myDocu~1\TECHDB\

If you really want to use PSEXEC in this manner and don't mind the clear text password. You will still need to resolve the %UserProfile% issue.
NET USE * \\MYSERVER\ProdDB /USER:domain\username password
FOR /F "tokens=1,2,3,4*" %%a IN ('net use ^| Find /I "\\MYSERVER\ProdDB"') DO SET DRIVELETTER=%%b
 
copy /y %DRIVELETTER%\CONDB\CONDB\HMMSA.mdb %USERPROFILE%\myDocu~1\TECHDB\

Open in new window

0
 
HudsonMarineAuthor Commented:
I did as suggested and got this result:

==============================================
C:\>psexec \\DESTINATION-PC -c C:\test\update.bat

PsExec v1.94 - Execute processes remotely
Copyright (C) 2001-2008 Mark Russinovich
Sysinternals - www.sysinternals.com



C:\WINDOWS\system32>cd..
The system cannot find the drive specified.

C:\WINDOWS>cd..

C:\>s:

C:\>copy \ProdDB\CONDB\CONDB\HMMSA.mdb C:\Documents and Settings\username\myDoc
u~1\TECHDB\
The system cannot find the path specified.
update.bat exited on h-nj-11 with error code 1.

C:\>
==================================================

The one thing that has changed is that now instead of the %username% value in the dos file which maps to the Administrators default folders it now actually maps to the real username/MyDocuments" folder which is what I want!

So i see some progress here - any other ideas
0
 
HudsonMarineAuthor Commented:
from what I understand AmazingTech when psexec is executed on your machine and copies a file to the target machine and then executes it it executes it as the administrator of that targets PC's user account which means the dos file whichthe following:

1) The default dos window path is - c:\windows\system32\
2) Mapped drives do not exist and therefore must be mapped with the Dos file using the UNC

Am I missing anything here in my thinking and are my facts correct?

0
 
AmazingTechCommented:
Mapped drives do not exist. But existing loggedon users drive letters are not available to you nor are they available to use with net use.

My attached code use * and finds what drive letter was assigned. Add my code to your batch file.

You are you on the DESTINATION-PC within PSEXEC unless you use -s to use the SYSTEM account.
0
 
HudsonMarineAuthor Commented:
I made some corrections and additions:


From what I understand AmazingTech when psexec is executed on your machine and copies a file to the target machine and then executes it it executes it as the administrator of that targets PC's user account which means the dos file whichthe following:

1) The default dos window path or (when you open a dos prompt) is = c:\windows\system32\
2) The Default Administrators %userprofile% is =  c:\Documents and Settings\Admin\My Documents
3) Mapped drives do not exist and therefore must be mapped with the Dos file using the UNC

Am I missing anything here in my thinking and are my facts correct?
0
 
igor-1965Commented:
1) Not administrator - PSEXEC will run the command in the context of YOURS account.
2) In order to map drive You will have to include your AD account password in the batch otherwise you won't be able to map drives.

Plus, you still don't know where to copy files - you need to discover the username of the user who is logged on the target computer (if any).

As we said - login script is the solution for your task.
0
 
AmazingTechCommented:
The account you use to run PSEXEC must have the right to connect to the targeted PC over the network which will be Administrators (typically).
0
 
HudsonMarineAuthor Commented:
AmazingTech:

Concerning your response of :

The account you use to run PSEXEC must have the right to connect to the targeted PC over the network which will be Administrators (typically).

How is this possible I am logged on as a regular network user with no administrator rights and i have been able to successfully run psexec to not only copy a file to another network PC but also to execute it!

Can you please explain?

===============================================================

igor-1965:

Concerning your response of :

1) Not administrator - PSEXEC will run the command in the context of YOURS account.

[Can you please explain the variables you are talking about concerning "YOUR account" - does this include my mapped drives?]

2) In order to map drive You will have to include your AD account password in the batch otherwise you won't be able to map drives. [ok please tell me how to write this code]

Plus, you still don't know where to copy files - you need to discover the username of the user who is logged on the target computer (if any). [I know all my networks usernames]

As we said - login script is the solution for your task. [I am not interested in this route]
0
 
AmazingTechCommented:
Do a DIR \\DESTINATION-PC\C$ from your PC.

This is an administrative share if you can see this then your network account should be admin.

Open SecPol.msc
Security Settings - Local Policies - User Right Assignment

Access this computer from the network

These are the users or groups who can remotely connect to the workstation over the network. I suppose you don't need to be Admin to get in here but on a typical machine you will only find Administrators in here (I believe) it's been so long that a built a machine without our corporate build.

You need (or atleast years ago when I tested this) this right to use PSEXEC or connecting to anything on a workstation over the network.

0
 
AmazingTechCommented:
If you don't have admin rights you won't be able to get into a users profile either (typically).
0
 
HudsonMarineAuthor Commented:
ok I did as described and under:

Access this computer from the network

there is: EVERYONE

UNDER SECURITY SETTINGS - so I guess that means everyone on my network has admin rights to access any computer on the network?

Is this correct?

I also guess that I can change my path to just copy the file it retrieves from the server over to a root folder like C:\Temp

0
 
igor-1965Commented:
1) PSEXEC executes commands / batch on the target account in the same context as you launch it on the computer. Presumably, it is your AD account context. You could explicitly provide the credentials to launch PSEXEC or use -s switch to run it in the system context on the target computer. There are no any mapped drives when batch is running on the target computer.

2) net use u: \\MYSERVER\ProdDB /USER:<yourdomain>\username> <user_password>

3) %USERPROFILE% that you use in the batch is a variable that will be equal to your username when you run batch on the target computer in your own context. You cannot run batch in the context on the account of the logged user. You even don't know the username of the user who is currently logged on.

4) Could you explain what are the reasons why you don't want to use login scripts (even if it is the ideal solution for your problem)?
0
 
HudsonMarineAuthor Commented:
Actually i found this under the tab "explain this"

Access this computer from the network

This user right determines which users and groups are allowed to connect to the computer over the network. Terminal Services are not affected by this user right.

Default:
On workstations and servers:
      Administrators
      Backup Operators
      Power Users
      Users
      Everyone

On domain controllers:
      Administrators
      Authenticated Users
      Everyone
0
 
HudsonMarineAuthor Commented:
How bout this:

is there a way via a psexec and a bat file to automatically put in place a command into the task scheduler of the current users session to initiate the dos file therefore using the target users mapped drives to perform the copy function in the Bat file.

I want to be able to perform this function while my users are logged in. or even if they are not!

maybe i could put the file in another location other than a server that is mapped  - any ideas?




0
 
HudsonMarineAuthor Commented:
AmazingTech:I don't think it can be done. PSEXEC does not impersonate the logged on user. Nor can you access UNC paths. I've tried for years.

Try this:

PSEXEC \\DESTINATION-PC CMD
ECHO %username%

Is this your username or the logged on user's?

ECHO %USERPROFILE%

Is this your profile path or the logged on user's?


Why do you need to PSEXEC it? Why can't you just copy the file? Using logon script for the user or from your workstation.

copy \\MYSERVER\ProdDB\CONDB\CONDB\HMMSA.mdb "\\DESTINATION-PC\c$\documents and settings\username\myDocu~1\TECHDB\

If you really want to use PSEXEC in this manner and don't mind the clear text password. You will still need to resolve the %UserProfile% issue.



 NET USE * \\MYSERVER\ProdDB /USER:domain\username password
FOR /F "tokens=1,2,3,4*" %%a IN ('net use ^| Find /I "\\MYSERVER\ProdDB"') DO SET DRIVELETTER=%%b
 
copy /y %DRIVELETTER%\CONDB\CONDB\HMMSA.mdb %USERPROFILE%\myDocu~1\TECHDB\



I TRIED THIS SCRIPT AND THIS WAS THE RESULT:


C:\>psexec \\TARGET-PC -c C:\test\updatex.bat

PsExec v1.94 - Execute processes remotely
Copyright (C) 2001-2008 Mark Russinovich
Sysinternals - www.sysinternals.com



C:\WINDOWS\system32>NET USE * \\MYSERVER\shared~1 /USERNAME:DOMAIN\PASSWORD
The option /USERNAME:DOMAIN\PASSWORD is unknown.

The syntax of this command is:


NET USE

C:\WINDOWS\system32>[devicename | *] [\\computername\sharename[\volume] [passwor
d | *]]
FOR        [/USER:[domainname\]username]
 /F        [/USER:[dotted domain name\]username]
 "tokens=1,2,3,4*"        [/USER:[username@dotted domain name]
 %a IN         [/SMARTCARD]
('net use | Find /I "\\hmmsnj01\shared~1\ProdDB"') DO         [/SAVECRED]
SET        [[/DELETE] | [/PERSISTENT:{YES | NO}]]
 DRIVELETTER=%b

NET USE {devicename | *} [password | *] /HOME

NET USE [/PERSISTENT:{YES | NO}]


More help is available by typing NET HELPMSG 3506.


C:\WINDOWS\system32>copy /y \CONDB\CONDB\HMMSA.mdb c:\TECHDB\
The system cannot find the path specified.
updatex.bat exited on h-nj-11 with error code 1.

C:\>
0
 
HudsonMarineAuthor Commented:
ok nevermind i did as you suggested and i used this:

copy "s:\ProdDB\CONDB\CONDB\HMMSA.mdb" "\\users-PC\c$\TECHDB\"

Thanks For EVERYONES HELP!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now