Remote script : create a file on local machine 
Author Message
 Remote script : create a file on local machine

Hi all,

I want to execute a script on remote machine, the script must create a file
on local machine.

Here a example (issue from the Windows Scripting doc), i modified the line
(fso.CreateTextFile...) to create the file on my local machine

// JScript.
RemoteTest.WSF
-------------------------------
<package>
<job>
<script language="JScript">
var oController = new ActiveXObject("WSHController");
var oProcess = oController.CreateScript("c:\\wsh5.6\\beenhere.wsf",
"remmachine");
oProcess.Execute();
while (oProcess.Status != 2) WScript.Sleep(100);
WScript.Echo("Fin");
</script>
</job>
</package>
-------------------------------
BeenHere.WSF
-------------------------------
<package>
<job>
<script language="JScript">
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fout = fso.CreateTextFile("\\\\localmachine\\C$\\beenhere.txt", true);
fout.WriteLine(new Date);
fout.Close();
</script>
</job>
</package>
-------------------------------

If i execute the script RemoteTest.WSF, he don't create the file
"beenhere.txt".
And if i copy the script BeenHere.WSF on the "remmachine" and execute it,
then it's OK.

Why do the WSHController not create the file "beenhere.txt" on my
localmachine ????
Thank's in advance for your help.

Yvan



Sat, 22 Oct 2005 00:44:51 GMT  
 Remote script : create a file on local machine

Quote:

> I want to execute a script on remote machine, the script must create a file
> on local machine.

> Here a example (issue from the Windows Scripting doc), i modified the line
> (fso.CreateTextFile...) to create the file on my local machine

> (snip code)

> If i execute the script RemoteTest.WSF, he don't create the file
> "beenhere.txt".
> And if i copy the script BeenHere.WSF on the "remmachine" and execute it,
> then it's OK.

> Why do the WSHController not create the file "beenhere.txt" on my
> localmachine ????

Hi

The process on the remote computer does not have access to any network
resources. You could pass credentials to the script on the remote computer, and
do an on-the-fly network drive map/unmap:

A Michael Harris quote:

"Change the calling script to prompt for username/password and pass them to the

remote script as commandline arguments. Change the remote script to map a
network drive on the fly using the credentials using an unused drive letter.
Before exiting the remote script, unmap the drive."

Below is a Michael Harris example of a script that is passed credentials via
named arguments and maps/unmaps a drive.

It should work, here is the feedback from one who has used it:

Subject: Re: problem using CopyFile method in remote script
Date: 20 Sep 2002 06:34:56 -0700

Newsgroups: microsoft.public.scripting.wsh

<quote>
I wound up using the on-the-fly map, but am using a dedicated domain
account for those credentials.
</quote>

'=================================================
' Calling syntax:
'   myscript.vbs /u:<username> /p:<password>
'
' Both named arguments are required...
'
' Error codes thrown:
'    100 - username and/or password missing
'    200 - ConnectDrive failed
'=================================================

sShare = "\\server\share"
sUsername = Null
sPassword = Null

Set Named = WScript.Arguments.Named
If Named.Exists("u") Then
  sUsername = Named.Item("u")
End If
If Named.Exists("p") Then
  sPassword = Named.Item("p")
End If

If IsNull(sUsername)
Or IsNull(sPassword) Then
  Err.Raise 100, WScript.ScriptName, "/u:<username> /p:<password> required"
  'WScript.Quit 100
End If

sDrive = ConnectDrive(sShare,sUsername,sPassword)

If IsNull(sDrive) Then
  Err.Raise 200, WScript.ScriptName, "ConnectDrive failed..."
  'WScript.Quit 200
End If
'
'
'...do your stuff...
'
'
'
DisconnectDrive sDrive

WScript.Quit 0

Function ConnectDrive(argShare,argUsername,argPassword)
  ConnectDrive = Null
  Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
  Dim net : Set net = CreateObject("WScript.network")
  Dim sDrive : sDrive = FreeDrive()
  On Error Resume Next
  net.MapNetworkDrive sDrive, sShare, False, argUsername, argPassword
  If Err Then Exit Function
  ConnectDrive = sDrive
End Function

Function DisconnectDrive(argDrive)
  Dim net : Set net = CreateObject("WScript.network")
  On Error Resume Next
  net.RemoveNetworkDrive argDrive, True
End Function

Function FreeDrive()
  Dim i
  Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
  For i = Asc("D") To Asc("Z")
    If Not fso.DriveExists(Chr(i)) Then
      FreeDrive = Chr(i) & ":"
      Exit Function
    End If
  Next
End Function

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter



Sat, 22 Oct 2005 01:51:40 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. WshController: Remote script runs only on local machine not on remote machine

2. Creating a new db on a local/remote machine using VB

3. Creating Text File on Client's Local Machine When Accessing Web Default Page

4. running external programs from ASP (on local or remote machines)

5. Enumerate local users on a remote machine

6. running external programs from ASP (on local or remote machines)

7. running external programs from ASP (on local or remote machines)

8. Execute remote script on a Remote XP Machine

9. Enumerating Services on Remote and local machines, other than for a domain

10. Create ODBC DSN on a remote machine

11. Creating Directories On Remote Machine

12. Using VB script to create a file on a remote server

 

 
Powered by phpBB® Forum Software