Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
843 views
in Technique[技术] by (71.8m points)

vb.net - WMI remote process to copy file

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.12c$
emotefile.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 = "targetAdministrator"
        .Password = "password"
    End With

    Dim ManagementScope As New System.Management.ManagementScope("\192.168.100.11
ootcimv2", 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.12c$
emotefile.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...