Copy files from a list of remote servers. How to give credentials?

Author Message
vv1984

  • Total Posts : 6
  • Scores: 0
  • Reward points : 0
  • Joined: 2/6/2012
  • Status: offline
Copy files from a list of remote servers. How to give credentials? Monday, February 06, 2012 9:35 PM (permalink)
0
[Helpful answer received] / [List Solutions Only]
Hi,
i did a script which retrieves files from remote servers. But these servers are not on my account domain.
 
Say, i run the scripts from a pc on domain "test.net" and my remote server is on domain "foo.net":
how do i give my script the credentials to access the foo.net domain from the start?
 
If i access the foo.net domain from Run ( mymachine.foo\\c$\mydir\mysubdir\ ) and then i run the script,
it works. But i have several domains, and i wish to insert credentials once for good.
 
Here's an idea of the code i'm using at the moment:
 
 
server_ip = "\\10.1.1.1\"
logfile_path = "C$\directory\file.log"
 
Set fso = CreateObject("Scripting.FileSystemObject")
 
dest_dir = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
dest_dir = dest_dir & "\logfiles_dir\"
 
fso.CopyFile server_ip & logfile_path , dest_dir & "bkp_file.log"
 
 
Any suggestion (even about my awful coding style) is absolutely welcome.
Thank you in advance
ciao
 
#1
    59cobalt

    • Total Posts : 969
    • Scores: 91
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:Copy files from a list of remote servers. How to give credentials? Monday, February 06, 2012 9:57 PM (permalink)
    5
    Using the MapNetworkDrive() method of the WshNetwork object will allow you to provide credentials. And instead of messing around with Left() and Len() simply use the GetParentFolderName() method:
    dest_dir = fso.GetParentFolderName(WScript.ScriptFullName)

     
    #2
      vv1984

      • Total Posts : 6
      • Scores: 0
      • Reward points : 0
      • Joined: 2/6/2012
      • Status: offline
      Re:Copy files from a list of remote servers. How to give credentials? Monday, February 06, 2012 10:24 PM (permalink)
      0
      59cobalt

      Using the MapNetworkDrive() method of the WshNetwork object will allow you to provide credentials. And instead of messing around with Left() and Len() simply use the GetParentFolderName() method:
      dest_dir = fso.GetParentFolderName(WScript.ScriptFullName)


       
      I'm not sure the paths i'm trying to reach are shared folders. Will it work anyway?
       
      Another question: this method maps the remote path to a logical drive.
      Say, path "\\server\public" to E:
      So, to access directory "\\server\public\subdir" i'll have to insert the path "E:\subdir\" then?
       
      Thank you very much for your help.
       
       
      #3
        59cobalt

        • Total Posts : 969
        • Scores: 91
        • Reward points : 0
        • Joined: 7/17/2011
        • Status: offline
        Re:Copy files from a list of remote servers. How to give credentials? Monday, February 06, 2012 11:14 PM (permalink)
        0
        vv1984
        I'm not sure the paths i'm trying to reach are shared folders. Will it work anyway?
        "\\server\C$\path\to\subfolder" will work.
        vv1984
        Another question: this method maps the remote path to a logical drive.
        Say, path "\\server\public" to E:
        So, to access directory "\\server\public\subdir" i'll have to insert the path "E:\subdir\" then?
        Yes.
         
        #4
          59cobalt

          • Total Posts : 969
          • Scores: 91
          • Reward points : 0
          • Joined: 7/17/2011
          • Status: offline
          Re:Copy files from a list of remote servers. How to give credentials? Monday, February 06, 2012 11:21 PM (permalink)
          0
          [deleted]
           
          #5
            vv1984

            • Total Posts : 6
            • Scores: 0
            • Reward points : 0
            • Joined: 2/6/2012
            • Status: offline
            Re:Copy files from a list of remote servers. How to give credentials? Monday, February 06, 2012 11:22 PM (permalink)
            0
            59cobalt


            vv1984
            I'm not sure the paths i'm trying to reach are shared folders. Will it work anyway?
            "\\server\C$\path\to\subfolder" will work.
            vv1984
            Another question: this method maps the remote path to a logical drive.
            Say, path "\\server\public" to E:
            So, to access directory "\\server\public\subdir" i'll have to insert the path "E:\subdir\" then?
            Yes.

             
            You mean that these are two different situations?
            At the moment, if i try to reach "\\server\C$\path\to\subfolder" with a script (before inserting credentials anywhere), I get something like "path not found" error.
             
            Will the logical drive "trick" solve this problem? Or will it solve that only if i am dealing with shared folders?
            As you may notice i'm a little bit confused about that... 
             
            #6
              59cobalt

              • Total Posts : 969
              • Scores: 91
              • Reward points : 0
              • Joined: 7/17/2011
              • Status: offline
              Re:Copy files from a list of remote servers. How to give credentials? Monday, February 06, 2012 11:28 PM (permalink)
              5
              Of course you need to replace "path\to\subfolder" with the actual path. Applying the concept to the information from your OP you'd use "\\10.1.1.1\C$\directory" instead of "\\server\C$\path\to\subfolder". If using "\\10.1.1.1\C$\directory" still gives you a "path not found" error, then either the folder "C:\directory" doesn't exist on the server, or administrative shares are disabed.
               
              #7
                vv1984

                • Total Posts : 6
                • Scores: 0
                • Reward points : 0
                • Joined: 2/6/2012
                • Status: offline
                Re:Copy files from a list of remote servers. How to give credentials? Tuesday, February 07, 2012 8:39 AM (permalink)
                0
                59cobalt


                Of course you need to replace "path\to\subfolder" with the actual path. Applying the concept to the information from your OP you'd use "\\10.1.1.1\C$\directory" instead of "\\server\C$\path\to\subfolder". If using "\\10.1.1.1\C$\directory" still gives you a "path not found" error, then either the folder "C:\directory" doesn't exist on the server, or administrative shares are disabed.


                Maybe i didn't understand the primary concept...
                Why can't I directly insert inside a script my credentials for a certain domain, without dealing with logical drives?

                I mean, i don't want to map \\remoteserver\c$\directory\subdir to \\mylocalmachine\c$\
                I just want to copy a file from \\remoteserver\c$\directory\subdir with no need to manually insert my credentials anywhere before running the script... (at the moment i have to manually access to remoteserver's domain from Run and authenticate. And then the script has full acess to the remote path)

                Hope i've explained my doubts.
                Thank you very much for help and patience 
                <message edited by vv1984 on Tuesday, February 07, 2012 8:41 AM>
                 
                #8
                  59cobalt

                  • Total Posts : 969
                  • Scores: 91
                  • Reward points : 0
                  • Joined: 7/17/2011
                  • Status: offline
                  Re:Copy files from a list of remote servers. How to give credentials? Tuesday, February 07, 2012 10:17 AM (permalink)
                  5
                  [This post was marked as helpful]
                  Perhaps you can manage to find a way to provide credentials via CopyFile() that I was unable to see. Unless you do, you're stuck with mapping a network drive like the rest of us, since MapNetworkDrive() does allow to specify credentials.

                  If you're concerned about storing credentials in the script: you could prompt for the credentials at runtime.
                  <message edited by 59cobalt on Tuesday, February 07, 2012 10:20 AM>
                   
                  #9
                    vv1984

                    • Total Posts : 6
                    • Scores: 0
                    • Reward points : 0
                    • Joined: 2/6/2012
                    • Status: offline
                    Re:Copy files from a list of remote servers. How to give credentials? Tuesday, February 07, 2012 9:08 PM (permalink)
                    0
                    Ok, i've got it (at last)
                     
                    But for some reason it seems i don't have rights enough to do so...
                     
                    remote_ip = "127.0.0.1"
                    Const OverwriteExisting = True 
                    Set fso = CreateObject("Scripting.FileSystemObject") 
                    Set objNetwork = Wscript.CreateObject("WScript.Network") 
                    current_dir = fso.GetParentFolderName(WScript.ScriptFullName)
                    objNetwork.MapNetworkDrive "Y:", "\\" & remote_ip & "\C$\test" , , "vv1984", "password" 
                    fso.CopyFile current_dir & "\testvv.txt" , "Y:\", OverwriteExisting
                    objNetwork.RemoveNetworkDrive "Y:" 

                     
                    I get "Access denied error" on MapNetworkDrive method. User vv1984 is Administrator, and has full access to C:\test folder.
                    I can't figure out what's missing..
                     
                     
                     
                     
                     
                    #10
                      59cobalt

                      • Total Posts : 969
                      • Scores: 91
                      • Reward points : 0
                      • Joined: 7/17/2011
                      • Status: offline
                      Re:Copy files from a list of remote servers. How to give credentials? Wednesday, February 08, 2012 1:24 AM (permalink)
                      0
                      127.0.0.1 is localhost. Mapping a local shared folder to a network drive doesn't work (you have to use subst to map local folders to drive letters). It's also quite pointless, since you could just skip the step and copy from local to local right away.

                      Does the problem persist when you try the same with a remote host?
                       
                      #11
                        vv1984

                        • Total Posts : 6
                        • Scores: 0
                        • Reward points : 0
                        • Joined: 2/6/2012
                        • Status: offline
                        Re:Copy files from a list of remote servers. How to give credentials? Wednesday, February 08, 2012 4:51 AM (permalink)
                        0
                        59cobalt

                        127.0.0.1 is localhost. Mapping a local shared folder to a network drive doesn't work (you have to use subst to map local folders to drive letters). It's also quite pointless, since you could just skip the step and copy from local to local right away.

                        Does the problem persist when you try the same with a remote host?


                        Sorry, 127.0.0.1 was just a "test" ip.
                        I tried it on my home network, using 192.168.1.2 as local machine and 192.168.1.3 as remote machine.

                        I think i've finally found where the matter is:
                        it's not possible to map a "normal" remote folder on a local drive.
                        It must be a shared folder. Is it right?

                        I finally managed to map \\192.168.1.3\test on Z: logical drive
                        but there's no way to map \\192.168.1.3\C$\test if test folder isn't a shared one.

                        So, in the end, there's no way to authenticate on various servers (on different domains) and retrieve files from them through a vbscript: you must first manually access to each single domain.

                        Another question (little bit off-topic i'm afraid..), is there any real difference between a domain and a workgroup? They seem pretty the same.

                        Thank you again

                        <message edited by vv1984 on Wednesday, February 08, 2012 4:58 AM>
                         
                        #12
                          59cobalt

                          • Total Posts : 969
                          • Scores: 91
                          • Reward points : 0
                          • Joined: 7/17/2011
                          • Status: offline
                          Re:Copy files from a list of remote servers. How to give credentials? Wednesday, February 08, 2012 7:50 AM (permalink)
                          5
                          [This post was marked as helpful]
                          Okay, crash course Windows shares:

                          Yes, you'll always need a shared folder to access files on a remote system via SMB (the protocol used for that kind of access):
                          net.MapNetworkDrive "Z:", "\\SERVER\SHARE"
                          However, you don't necessarily have to share the particular folder (although that's recommended in most cases), but can also map subfolders of the shared folder:
                          net.MapNetworkDrive "Z:", "\\SERVER\SHARE\SOME\SUB\FOLDER"
                          On top of that Windows systems by default share the root directories of all local drives, so you can do stuff like this:
                          net.MapNetworkDrive "Z:", "\\SERVER\C$"
                          These are the so-called "administrative shares". And of course mapping subfolders also works for administrative shares:
                          net.MapNetworkDrive "Z:", "\\192.168.1.3\C$\test"
                          As long as there's a folder "C:\test" on the computer with the IP address 192.168.1.3, that is.
                          Access to administrative shares is restricted to members of the local administrators group on the remote system, though, so you must provide the credentials of an admin account on 192.168.1.3:
                          net.MapNetworkDrive "Z:", "\\192.168.1.3\C$\test", , "adminuser", "password"
                          In a workgroup you must either maintain identical usernames and passwords on all systems (in that case Windows should automatically use the credentials of your current user for mapping the share), or you have to explicitly provide the credentials of the remote user. In a domain, the "Domain Admins" group is by default a member of the local administrators group on every domain member (the group is automatically added when you join a computer to a domain). The centralized user administration in a domain saves you the headache of maintaining local users and passwords on all systems.
                          If the two computers are members of different domains, you must provide the username including the domain name:
                          net.MapNetworkDrive "Z:", "\\192.168.1.3\C$\test", , "EXAMPLE\adminuser", "password"
                          or
                          net.MapNetworkDrive "Z:", "\\192.168.1.3\C$\test", , "adminuser@example.org", "password"

                          If you have the correct username and password, but access still fails, there are several reasons that may cause this:
                          • The Windows firewall is blocking access: add an exception.
                          • Someone had disabled the administrative shares: check with "net share" and re-enable the shares by changing the respective registry value and starting the Server service.
                          • Incorrect permissions to the share: best practice is to grant full access to Everyone, because unlike filesystem permissions share permissions affect only the share itself, not its subfolders. Plus, filesystem permissions are far more fine-grained anyway.
                          • Incorrect permissions on a folder: check and correct the permissions on the folder.

                          There are also other ways to access remote files (like WMI), but I wouldn't recommend them, since they're more complicated and you'd still need to provide credentials.
                           
                          #13

                            Online Bookmarks Sharing: Share/Bookmark

                            Jump to:

                            Current active users

                            There are 0 members and 1 guests.

                            Icon Legend and Permission

                            • New Messages
                            • No New Messages
                            • Hot Topic w/ New Messages
                            • Hot Topic w/o New Messages
                            • Locked w/ New Messages
                            • Locked w/o New Messages
                            • Read Message
                            • Post New Thread
                            • Reply to message
                            • Post New Poll
                            • Submit Vote
                            • Post reward post
                            • Delete my own posts
                            • Delete my own threads
                            • Rate post

                            2000-2012 ASPPlayground.NET Forum Version 3.9