WMI 远程进程复制文件 [英] WMI remote process to copy file

查看:43
本文介绍了WMI 远程进程复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长话短说,我的应用程序需要将文件复制到远程目标,而该目标可能无法通过 UNC 连接.然而,来自目标和返回到服务器的 UNC 连接始终是可能的.所以计划是使用 WMI 启动远程命令外壳 (cmd) 并使用复制命令来获取文件.但这不起作用.从目标的命令行手动执行以下命令时可以正常工作:

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

但是当我在 InputParameters("CommandLine") 中尝试这个相同的命令时,它不起作用,并且不会产生任何错误.请注意,我可以使用 WMI 连接到目标,并且远程执行工作正常,因为我可以启动 calc.exe 等.这是不起作用的代码:

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?

推荐答案

坦率地说,您实际上应该给自己点赞,因为您创建的方法可能是有史以来第一个绕过 WMI 远程文件复制限制的方法!我花了 3 周时间搜索信息/解决方法,而你的是唯一有效的方法!如果我有任何观点,我会投票支持您的解决方案...

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...

我创建了一个完全可用的 VBS &基于您的方法的 WMI 脚本:

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

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

根据需要将 myFTPCommands 替换为要放入文件 c:\ftpscript.bat(或 .vbs、.ps1 或任何您喜欢的文件)中的任何脚本.如果在单行脚本中无法容纳足够多的文本,请使用 >> 附加相同的方法.现在,您可以使用 XCOPY、PSEXEC、COPY 或其他任何方式来运行您刚刚在远程主机的文件系统上创建的脚本.

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.

这里是 我使用你的方法完全充实的 VBScript.再次感谢.:)

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

HTH,丽兹

这篇关于WMI 远程进程复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆