WMI远程进程复制文件
长话短说,我的应用程序需要将文件复制到远程目标,而远程目标可能无法与目标建立 UNC 连接。然而,从目标到服务器的 UNC 连接始终是可能的。因此计划是使用 WMI 启动远程命令 shell (cmd) 并使用复制命令来抓取文件。但这行不通。从目标的命令行手动执行时,以下命令工作正常:
copy \\192.168.100.12\c$\remotefile.txt c:\localfile.txt
但是当我尝试将相同的命令作为 InputParameters("CommandLine")
的一部分时,它不起作用,并且不会产生错误。请注意,我可以使用 WMI 连接到目标,并且远程执行工作得很好,因为我可以启动 calc.exe 等。以下是不起作用的代码:
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
任何想法为什么这不起作用在职的?或者有人有更好的方法来做我想做的事情吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
弗兰克,您实际上应该给自己点信心,因为您创建的方法可能是第一个绕过远程文件复制的 WMI 限制的方法!我花了三周的时间寻找信息/解决方法,而你的是唯一有效的!如果我有任何观点,我会投票支持你的解决方案......
我创建了一个完全工作的 VBS 和基于您的方法的 WMI 脚本:
根据需要将 myFTPCommands 替换为您想要进入文件 c:\ftpscript.bat 的任何脚本(或 .vbs、.ps1 或您喜欢的任何内容)。如果一行脚本中无法容纳足够的文本,请使用>>以相同的方法附加。现在,您可以使用 XCOPY、PSEXEC、COPY 或其他任何命令来运行刚刚在远程主机的文件系统上创建的脚本。
这是我使用你的方法编写的完整的 VBScript。再次感谢。 :)
哈,
莉兹
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:
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
出于安全原因,大多数以编程方式连接到远程计算机并告诉其从另一台计算机将文件复制到自身的方法都被阻止。最终对我有用的一件事是 FTP。使用上面的代码我可以执行如下操作:
这会导致 ftp 命令行实用程序在远程计算机上运行,使用 c:\ftpscript.txt 从中获取命令列表。由于无法将 ftp 脚本文件复制到目标(同样,没有 UNC 连接),我可以首先执行以下
操作: 这有效:)
更新:从未想过使用
XCOPY< /code> 并且它工作得很好:
更新:XCOPY 昨天工作了,现在不行了。一切都没有改变,所以我完全无法解释。
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:
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:
And this works :)
UPDATE: Never thought to use
XCOPY
and it works perfectly:UPDATE: XCOPY worked yesterday, now it doesn't. NOTHING has changed, so I am at a complete loss for explanation.