WMI remote process to copy file

2020-02-01 06:08发布

Long story short, my application needs to copy a file to a remote target where UNC connections TO the target might not be possible. However UNC connections FROM the target and BACK to the server will always be possible. So the plan was to use WMI to start a remote command shell (cmd) and use the copy command to grab the file. But this doesn't work. The following command works fine when executed manually from the command line of the target:

copy \\192.168.100.12\c$\remotefile.txt c:\localfile.txt

But when I try this same command as part of the InputParameters("CommandLine") it does not work, and produces no error. Note that I can use WMI to connect to the target and remote execution works just fine as I can start calc.exe etc. Here is the code that doesn't work:

Dim ConnectionOptions As New System.Management.ConnectionOptions
    With ConnectionOptions
        .Username = "target\Administrator"
        .Password = "password"
    End With

    Dim ManagementScope As New System.Management.ManagementScope("\\192.168.100.11\root\cimv2", ConnectionOptions)
    Try
        ManagementScope.Connect()
        MsgBox("connected")
        Dim ManagementPath As New System.Management.ManagementPath("Win32_Process")
        Dim ManagementOptions As New System.Management.ObjectGetOptions
        Dim ManagementClass As New System.Management.ManagementClass(ManagementScope, ManagementPath, ManagementOptions)
        Dim InputParameters As System.Management.ManagementBaseObject = ManagementClass.GetMethodParameters("Create")
        InputParameters("CommandLine") = "cmd /c copy \\192.168.100.12\c$\remotefile.txt c:\localfile.txt"
        Dim OutputParameters As System.Management.ManagementBaseObject = ManagementClass.InvokeMethod("Create", InputParameters, Nothing)
        MsgBox("done")
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

Any ideas why this isn't working? Or does anyone have a better way of doing what I'm trying to do?

标签: vb.net wmi
2条回答
Juvenile、少年°
2楼-- · 2020-02-01 06:49

For security reasons, most methods of programatically connecting to a remote machine and telling it to copy a file to itself from another machine are blocked. One thing that finally worked for me is FTP. Using the above code I can do something like this:

InputParameters("CommandLine") = "ftp -s:c:\ftpscript.txt"

Which causes the ftp commandline utility to run on the remote machine, using c:\ftpscript.txt to get a list of commands from. Since there is no way to copy the ftp script file to the target (again, no UNC connection), I can first do:

InputParameters("CommandLine") = "cmd /c echo myFTPCommands > c:\ftpscript.txt"

And this works :)

UPDATE: Never thought to use XCOPY and it works perfectly:

InputParameters("CommandLine") = "cmd /c echo F | xcopy remotefile localfile"

UPDATE: XCOPY worked yesterday, now it doesn't. NOTHING has changed, so I am at a complete loss for explanation.

查看更多
3楼-- · 2020-02-01 07:05

Frank you should actually give yourself credit since the method you created is likely the first ever to get around WMI limitations of remote file copy! I did 3 weeks of searching for info/workaround and yours is the only one that works! If I had any points I would vote for your solution...

I created a fully working VBS & WMI script based on your method:

 InputParameters("CommandLine") = "cmd /c echo myFTPCommands > c:\ftpscript.txt"

where you replace myFTPCommands as needed with whatever script you want to go into the file c:\ftpscript.bat (or .vbs, .ps1, or whatever you like). If you couldn't fit enough text in the one-line script, then append with the same method using >>. Now, you can use XCOPY, PSEXEC, COPY, or anything else to run the script you just created on the remote host's file system.

Here's my fully fleshed out VBScript using your method. Thanks again. :)

HTH, Lizz

查看更多
登录 后发表回答