尝试使用 WMI 将文件从一台 XP PC 复制到另一台 PC,因为 RPC 和 UNC 不可用 [英] Trying to copy file from one XP PC to another using WMI, since RPC and UNC are not available

查看:25
本文介绍了尝试使用 WMI 将文件从一台 XP PC 复制到另一台 PC,因为 RPC 和 UNC 不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 VBScript 的新手.我找不到在 VBS 中使用 WMI 将文件从一台 XP 主机复制到另一台主机的方法.复制文件的常用方法(RPC - 远程过程调用、SMB、UNC)不适用于多个主机,但 WMI 可用于所有主机,我需要将文件从我的管理主机复制到目标 Windows 主机.我以为我会在那里找到一些示例代码,但我没有找到有关它的信息.也没有发现任何告诉我它无法完成的事情.

I'm new to VBScript. I cannot find a way to copy files from one XP host to another using WMI in a VBS. The usual way of copying files (RPC - Remote Procedure Call, SMB, UNC) are not available to several hosts but WMI is available to all hosts, and I need to copy files from my admin host to a target Windows host. I thought I'd find some sample code out there but I've found no info on it. Haven't found anything telling me it can't be done, either.

源文件是我的管理计算机的F:TEMP"文件夹中的一个可执行文件和test1.txt".我想将文件放在远程主机 HOST1 的C:TEMP"文件夹中.我在两台主机上都拥有完全的管理员权限.到目前为止,这是我所拥有的,仅用于一个文件(以保持测试简单):

The source files are an executable and 'test1.txt' in my admin computer's 'F:TEMP' folder. I want to put the files on remote host HOST1's 'C:TEMP' folder. I have full admin rights on both hosts. Here is what I have so far, just for one file (to keep the testing simple):

strComputer = "HOST1"
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "
ootcimv2")
Set colFiles = objWMIService.ExecQuery( _
    "Select * from Win32_Directory where Name = 'c:\temp'")
For Each objFiles in colFiles
    errResults  = objFolder.Copy("f:	emp	est1.txt")
    Wscript.Echo errResults
Next

推荐答案

我了解到 WMI 无法在远程主机上创建文件,也无法通过网络连接复制文件:http://msdn.microsoft.com/en-us/library/windows/desktop/aa389288%28v=vs.85%29.aspx

I learned that WMI cannot create files on a remote host, and it cannot copy files over a network connection: http://msdn.microsoft.com/en-us/library/windows/desktop/aa389288%28v=vs.85%29.aspx

但是,它可以运行一个 cmd 进程.这是弗兰克怀特的 C 语言代码,然后是他的例子:https://stackoverflow.com/a/8913231/1569434

However, it can run a cmd process. Here's Frank White's code in C sharp, followed by his example: https://stackoverflow.com/a/8913231/1569434

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

您需要四件事才能使用以下所有脚本小程序,它们相互构建以使用 psexec 在远程主机上运行普通"VBScript 或批处理脚本:

You will need four things to use all the following scriptlets, which build on each other to use psexec to run a "normal" VBScript or batch script on the remote host:

  1. 远程主机的管理员权限;
  2. 在远程主机上启用 WMI
  3. 您的远程主机可以访问的网络共享(使用 RPC、UNC、FTP 等,但不是 DFS!(分布式文件系统" - 请参阅注释);和
  4. psexec.exe 和网络共享上的普通"脚本.
  1. admin rights on the remote host;
  2. WMI enabled on the remote host
  3. a network share (using RPC, UNC, FTP, etc., but NOT DFS! ("Distributed File System" - see note) that your remote host can access; and
  4. psexec.exe and your "normal" script(s) on the network share.

重要说明:不要使用DFS 映射网络共享!如果您使用分布式文件系统作为网络共享,它失败.根据您的尝试方式,您可能得到的错误代码是 "系统错误 1312",无论您使用哪种操作系统(例如 XP、Win 7).

Important Note: Do NOT use DFS to map the network share! It will fail if you use Distributed File System for your network share. An error code you might get depending on how you try is "System error 1312", no matter which operating system (e.g., XP, Win 7) you use.

当远程主机上的 RPC 不可用而 WMI 可用时,以下方法将在远程主机的 c: emp 文件夹中创建一个本地 ASCII 文件,其中包含文本myTextCommands",不带引号.

When RPC is not available on a remote host but WMI is, then the following method will create a local ASCII file on the remote host's c: emp folder, containing the text "myTextCommands", without the quotes.

' https://stackoverflow.com/questions/8884728/wmi-remote-process-to-copy-file
strCommand = "cmd /c echo myTextCommands > c:	emp	estscript.txt"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" _
    & strComputer & "
ootcimv2")
Set objProcess = objWMIService.Get("Win32_Process")
errReturn = objProcess.Create(strCommand, null, null, intProcessID)
' See following link for error codes returned by errReturn
' http://msdn.microsoft.com/en-us/library/windows/desktop/aa389388(v=vs.85).aspx

请注意上面脚本中的重要限制:它只能创建 ASCII 文件 - 而不是二进制文件.

Notice the important limitation in the script above: it can only create ASCII files - not binary.

让我们使用该技术来映射驱动器号:

Let's use that technique to map a drive letter:

strCommand = "cmd /c net use z: " & MyShare & " /user:%USERDOMAIN%\%USERNAME% " _
    & strPassword & ">" & strRemoteLog
Set objProcess = objWMIService.Get("Win32_Process")
Call errProcess

其中strRemoteLog"设置为类似c: empMyLog.txt"的内容,提示strPassword"(请参阅​​底部的完整脚本示例和参考),errProcess"是运行以下内容的子例程使用上面提到的cmd/c"技巧进行处理:

where "strRemoteLog" is set to something like "c: empMyLog.txt", "strPassword" is prompted (see full script example and reference at bottom), and "errProcess" is a subroutine that runs the following process using the "cmd /c" trick mentioned above:

Sub errProcess
errReturn = objProcess.Create(strCommand, null, null, intProcessID)
If errReturn = 0 Then
    Wscript.Echo "Process was started with a process ID: " & intProcessID
    WScript.Sleep 5000
Else
    Wscript.Echo "Process could not be started due to error: " & errReturn
End If
End Sub

映射网络驱动器后,将脚本复制到主机:

With a network drive mapped, copy your script to the host:

strCommand="cmd /c xcopy Z:scriptsSCRIPT1.bat c:	emp >>" & strRemoteLog
Call errProcess

SCRIPT1.bat 已准备就绪,因此在远程主机上针对它启动 psexec,向您的脚本传递一个变量 strUserID,该变量将在之前获得,例如:

SCRIPT1.bat is ready, so start psexec against it on the remote host, passing your script a variable strUserID that would be obtained earlier and is here for example:

strCommand="cmd /c Z:psexec \%COMPUTERNAME% /accepteula -s -n 120 " _
    & cmd /c c:	empSCRIPT1.bat " & strUserID & ">>" & strRemoteLog
Call errProcess

一旦 psexec 完成,您可能想要保存结果.因此,您重命名日志文件、上传它、取消映射您的驱动器并清理残留文件:

Once psexec finishes, you might want to save the results. So you rename the log file, upload it, unmap your drive, and clean up residual files:

strCommand="cmd /c REN " & strRemoteLog & " SCRIPT1-%COMPUTERNAME%.txt"
Call errProcess
strCommand="cmd /c MOVE /Y c:	empSCRIPT1*.txt Z:scriptsLOGS"
Call errProcess
strCommand="cmd /c net use * /del /Y"
Call errProcess
strCommand="cmd /c del c:	empSCRIPT1*.bat /q"
Call errProcess

你已经完成了.您已成功映射驱动器,针对远程主机运行例程脚本,并上传其输出.

You're done. You've successfully mapped a drive, run a routine script against the remote host, and uploaded its output.

请注意,此方法也适用于带有 UAC 的 Windows 7 和 Windows 2008.

Note this method also works on Windows 7 and Windows 2008 with UAC.

这是完整的示例"集成脚本.随时提出修复、改进等建议.

Here's the full 'sample' integrated script. Feel free to suggest fixes, improvements, etc.

On Error Resume Next

 MyShare="\SHARE1"
 strRemoteLog="c:	empMapZ.txt"

' Set remote hostname
strComputer="HOST2"
'strComputer = InputBox("Enter Computer name", _
'"Find PC", strComputer)

' Set remote userid
strUserID="USERID1"
'strComputer = InputBox("Enter userid", _
'"Find User", strComputer)

' Enumerate cimv2 on remote host strComputer
Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=Impersonate}!//" & strComputer & "
ootcimv2")

' Verify remote host exists on domain
If( IsEmpty( objWMIService ) = True ) Then
    WScript.Echo( "OBJECT_NOT_INITIALIZED :: " & strComputer )
    WScript.Quit( OBJECT_NOT_INITIALIZED )
End If

' Prompt for masked password
strPassword=GetPass

' Build and run command to execute on strComputer
strCommand = "cmd /c net use z: " & MyShare & " /user:%USERDOMAIN%\%USERNAME% " & strPassword & ">" & strRemoteLog
Set objProcess = objWMIService.Get("Win32_Process")
Call errProcess

' Copy script(s) from MyShare to HOST2 since psexec cannot run scripts on shared drives
strCommand="cmd /c xcopy Z:scriptscleanpclocal.bat c:	emp /V /C /I /Q /H /R /Y>>" & strRemoteLog
Call errProcess

' Change directory to c:	emp
'strCommand="cmd /c cd c:	emp>" & strRemoteLog
'Call errProcess

' Start PSEXEC against script
strCommand="cmd /c Z:psexec \%COMPUTERNAME% /accepteula -s -n 120 cmd /c c:	empcleanpclocal.bat " & strUserID & ">>" & strRemoteLog
Call errProcess

' Rename logfile to include hostname, upload to share,  unmap networked drive, and delete script
strCommand="cmd /c REN " & strRemoteLog & " cleanpc-%COMPUTERNAME%.txt"
Call errProcess
strCommand="cmd /c MOVE /Y c:	empclean*.txt Z:scriptsLOGS"
Call errProcess
strCommand="cmd /c net use * /del /Y"
Call errProcess
strCommand="cmd /c del c:	empclean*.bat /q"
Call errProcess

WScript.Quit





' ***********
' APPENDIX
' Subroutines, functions
' ***********

' **SUBROUTINES**
'strCommand="cmd /c dir z:scripts>" & strRemoteLog ' Works to get dir of z:scripts

' Function to handle errReturn
Sub errProcess
WScript.Echo "strCommand=" & strCommand
errReturn = objProcess.Create(strCommand, null, null, intProcessID)

If errReturn = 0 Then
    Wscript.Echo "Process was started with a process ID: " & intProcessID
    WScript.Sleep 5000
Else
    Wscript.Echo "Process could not be started due to error: " & errReturn
End If
WScript.Echo

' Error return codes for Create method of the Win32_Process Class
' http://msdn.microsoft.com/en-us/library/windows/desktop/aa389388(v=vs.85).aspx
' 0=Successful Completion
' 2=Access Denied
' 3=Insufficient Privilege
' 8=Unknown failure
' 9=Path Not Found
' 21=Invalid Parameter

End Sub



' **FUNCTIONS**

' Subroutine to get masked password
Function GetPass
' Mask Passwords Using Internet Explorer
' Ensure you follow the technet.com instructions and create file password.htm
' http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/04/how-can-i-mask-passwords-using-an-inputbox.aspx

Set objExplorer = WScript.CreateObject _
    ("InternetExplorer.Application", "IE_")

objExplorer.Navigate "file:///C:SCRIPTSpassword.htm"   
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 400
objExplorer.Height = 350 
objExplorer.Left = 300
objExplorer.Top = 200
objExplorer.Visible = 1             

Do While (objExplorer.Document.Body.All.OKClicked.Value = "")
    Wscript.Sleep 250                 
Loop 

strPassword = objExplorer.Document.Body.All.UserPassword.Value
strButton = objExplorer.Document.Body.All.OKClicked.Value
objExplorer.Quit
Wscript.Sleep 250

If strButton = "Cancelled" Then
    Wscript.Quit
'Else
'    Wscript.Echo strPassword
End If

' Return the password
GetPass = strPassword

End Function

这篇关于尝试使用 WMI 将文件从一台 XP PC 复制到另一台 PC,因为 RPC 和 UNC 不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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