Highlighted
Absent Member.
Absent Member.
7213 views

vbscript to connect to remote machine

Jump to solution
Hi All,

Can anyone pls provide script on how to connect to remote machine (with different credentials) and run batch file/s using vbscript?
0 Likes
1 Solution

Accepted Solutions
Highlighted
Knowledge Partner
Knowledge Partner
Ramesh, have you tried mapping a drive to the remote machine and executing the file that way:


' Map a remote drive and execute a file on remote drive
Dim xFound
xFound = False
' Verify whether the X: drive is already mapped
Set objNet = Wscript.CreateObject ("Wscript.Network")
Set colDrives = objNet.EnumNetworkDrives
For i = 0 To colDrives.count-1 Step 2
If colDrives.Item(i) = "X:" Then
xFound = True
End If
Next
' Map the X: Drive if not exist
If xFound = False Then
objNet.MapNetworkDrive "X:", "\\10.123.123.12\c$"

' Execute remote file
Set oShell=CreateObject("wscript.shell")
oShell.run "cmd /k cscript c:\myfile.vbs" & "Exit"
Set oShell = Nothing
End If
' tidy up
Set objNet = Nothing


You can specify userID & password on the objNet.MapNetworkDrive command if required.


Mark Smith.

View solution in original post

6 Replies
Highlighted
Knowledge Partner
Knowledge Partner
Ramesh, have you tried mapping a drive to the remote machine and executing the file that way:


' Map a remote drive and execute a file on remote drive
Dim xFound
xFound = False
' Verify whether the X: drive is already mapped
Set objNet = Wscript.CreateObject ("Wscript.Network")
Set colDrives = objNet.EnumNetworkDrives
For i = 0 To colDrives.count-1 Step 2
If colDrives.Item(i) = "X:" Then
xFound = True
End If
Next
' Map the X: Drive if not exist
If xFound = False Then
objNet.MapNetworkDrive "X:", "\\10.123.123.12\c$"

' Execute remote file
Set oShell=CreateObject("wscript.shell")
oShell.run "cmd /k cscript c:\myfile.vbs" & "Exit"
Set oShell = Nothing
End If
' tidy up
Set objNet = Nothing


You can specify userID & password on the objNet.MapNetworkDrive command if required.


Mark Smith.

View solution in original post

Highlighted
Absent Member.
Absent Member.
Hi Mark Smith,

Thanks very much for giving reply.

I have not tried anything. I will take your script as a model script and will implement according to my requirement. If i face any problems will contact you.

Thanks in Advance.
Ramesh
0 Likes
Highlighted
Absent Member.
Absent Member.
Hi Mark Smith,

As per my understanding your script run the batch files on my local machine, not on remote machine if am not wrong. But i need to run the batch files on remote machine. Means i need to
1.connect to remote machine
2.access batch file in remote machine
3.execute it on remote machine
4.get log of execution.

Pls correct if am wrong. I would appreciate your comments.
0 Likes
Highlighted
Knowledge Partner
Knowledge Partner
It maps to whichever machine IP address you state in the MapNetworkDrive statement (maps to C: using c$ change to whichever drive is required).


Mark Smith.
0 Likes
Highlighted
Absent Member.
Absent Member.
I havent tested it, but I am fairly certain the shell being run in that script will still be on the local machine, not the remote one. This link might be of help though.

http://benchristian.wordpress.com/2006/01/11/opening-a-local-command-prompt-for-a-remote-machine/
Highlighted
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.