
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