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

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

问题描述

我是VBScript的新手。我找不到一种方法使用WBS在VBS中将文件从一个XP主机复制到另一个。通常的复制文件(RPC - 远程过程调用,SMB,UNC)的方式对几个主机不可用,但WMI对所有主机可用,我需要将文件从我的管理主机复制到目标Windows主机。我想我会在那里找到一些示例代码,但我没有找到任何信息。



源文件是可执行文件,在我的管理计算机的'F: \TEMP'文件夹。我想把文件在远程主机HOST1的'C:\TEMP'文件夹。我对这两台主机拥有完全管理权限。这是我到目前为止,只是一个文件(以保持测试简单):

  strComputer =HOST1
Set objWMIService = GetObject(winmgmts:_
&{impersonationLevel = impersonate}!\\& strComputer&\root\cimv2)
colFiles = objWMIService.ExecQuery(_
Select * from Win32_Directory where Name ='c:\\temp')
对于colFiles中的每个objFiles
errResults = objFolder.Copy f:\temp\test1.txt)
Wscript.Echo errResults
下一页


解决方案

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



但是,它可以运行cmd进程。这里是Frank White在C sharp中的代码,接下来是他的例子:
http://stackoverflow.com/a/8913231/1569434

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

您将需要四件事来使用所有以下scriptlet,这些scriptlet彼此建立以使用psexec在远程主机上运行正常VBScript或批处理脚本:


  1. 远程主机上的管理权限; $ b
  2. 在远程主机上启用WMI

  3. 网络共享(使用RPC,UNC,FTP等,但不是 DFS!文件系统 - 请参阅注释),您的远程主机可以访问;

  4. psexec.exe和您的正常脚本在网络共享上。

重要注意事项 //en.wikipedia.org/wiki/Distributed_File_System_%28Microsoft%29rel =nofollow> DFS 来映射网络共享!如果您的网络共享使用分布式文件系统,则 会失败。根据您的尝试,您可能会遇到的错误代码为

当RPC不可用时,无论使用何种操作系统(例如XP,Win 7)远程主机但WMI是,则以下方法将在远程主机的c:\temp文件夹中创建一个本地ASCII文件,其中包含文本myTextCommands,不带引号。

 'http://stackoverflow.com/questions/8884728/wmi-remote-process-to-copy-file 
strCommand =cmd / c echo myTextCommands> ; c:\temp\testscript.txt
设置objWMIService = GetObject(winmgmts:&{impersonationLevel = impersonate}!\\_
& strComputer& \root\cimv2)
设置objProcess = objWMIService.Get(Win32_Process)
errReturn = objProcess.Create(strCommand,null,null,intProcessID)
' errReturn返回的错误代码
'http://msdn.microsoft.com/en-us/library/windows/desktop/aa389388(v=vs.85).aspx

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



让我们使用这种技术映射驱动器号:

  strCommand =cmd / c net use z:&我的分享& / user:%USERDOMAIN%\%USERNAME%_ 
& strPassword& > & strRemoteLog
设置objProcess = objWMIService.Get(Win32_Process)
调用errProcess

其中strRemoteLog设置为像c:\temp\MyLog.txt,提示strPassword(参见完整脚本示例和底部参考),errProcess是运行以下内容的子例程进程使用上述cmd / c技巧:

  Sub errProcess 
errReturn = objProcess.Create(strCommand, null,null,intProcessID)
如果errReturn = 0则
Wscript.Echo进程以进程ID:& intProcessID
WScript.Sleep 5000
Else
Wscript.Echo由于错误,无法启动进程:& errReturn
如果
结束End Sub

映射网络驱动器时,脚本到主机:

  strCommand =cmd / c xcopy Z:\scripts\SCRIPT1.bat c:\ temp \>> & strRemoteLog 
调用errProcess

SCRIPT1.bat已准备就绪,因此在远程启动psexec主机,传递您的脚本一个变量strUserID,它将早先获得,并在这里例如:

  strCommand =cmd / c Z :\psexec \\%COMPUTERNAME%/ accepteula -s -n 120_ 
& cmd /cc:\temp\SCRIPT1.bat& strUserID&>>& strRemoteLog
调用errProcess

psexec完成后,您可能需要保存结果。因此,您需要重命名日志文件,将其上传,取消映射驱动器并清除残留文件:

  strCommand =cmd / c REN& strRemoteLog&SCRIPT1-%COMPUTERNAME%.txt
调用errProcess
strCommand =cmd / c MOVE / Y c:\temp\SCRIPT1 * .txt Z:\scripts\LOGS\
调用errProcess
strCommand =cmd / c net use * / del / Y
调用errProcess
strCommand =cmd / c del c:\temp\SCRIPT1 * .bat / q
调用errProcess

完成后,您已成功映射驱动器,针对远程主机运行例程脚本并上传其输出。 p>

请注意,此方法也适用于Windows 7和Windows 2008与UAC。



 错误恢复下一页

MyShare = \\SHARE1
strRemoteLog =c:\temp\MapZ.txt

设置远程主机名
strComputer =HOST2
' strComputer = InputBox(Enter Computer name,_
'Find PC,strComputer)

设置远程用户ID
strUserID =USERID1
'strComputer = InputBox(Enter userid,_
Find User,strComputer)

在远程主机上枚举cimv2 strComputer
Set objWMIService = GetObject(winmgmts:& ; _
{impersonationLevel = Impersonate}!//& str计算机& \root\cimv2)

'验证远程主机是否存在于域
如果(IsEmpty(objWMIService)= True)然后
WScript.Echo(OBJECT_NOT_INITIALIZED :: & strComputer)
WScript.Quit(OBJECT_NOT_INITIALIZED)
结束如果

提示屏蔽的密码
strPassword = GetPass

'构建并运行命令以在strComputer上执行
strCommand =cmd / c net use z:&我的分享& / user:%USERDOMAIN%\%USERNAME%& strPassword& > & strRemoteLog
设置objProcess = objWMIService.Get(Win32_Process)
调用errProcess

'将脚本从MyShare复制到HOST2,因为psexec无法在共享驱动器上运行脚本
strCommand =cmd / c xcopy Z:\scripts\cleanpclocal.bat c:\temp \ / V / C / I / Q / H / R / Y> & strRemoteLog
调用errProcess

'将目录更改为c:\temp
'strCommand =cmd / c cd c:\temp> & strRemoteLog
'调用errProcess

'对脚本启动PSEXEC
strCommand =cmd / c Z:\psexec \\%COMPUTERNAME%/ accepteula -s -n 120 cmd /cc:\temp\cleanpclocal.bat& strUserID& >> & strRemoteLog
调用errProcess

'重命名日志文件以包括主机名,上传到共享,取消映射网络驱动器和删除脚本
strCommand =cmd / c REN& strRemoteLog& cleanpc-%COMPUTERNAME%.txt
调用errProcess
strCommand =cmd / c MOVE / Y c:\temp\clean * .txt Z:\scripts\LOGS\
调用errProcess
strCommand =cmd / c net use * / del / Y
调用errProcess
strCommand =cmd / c del c:\temp\clean * .bat / q
调用errProcess

WScript.Quit





'*** ********
'附录
'子程序,函数
'***********

'** SUBROUTINES **
'strCommand =cmd / c dir z:\scripts\> & strRemoteLog'工程来获取z:\scripts \

的函数处理errReturn的函数
Sub errProcess
WScript.EchostrCommand =& strCommand
errReturn = objProcess.Create(strCommand,null,null,intProcessID)

如果errReturn = 0则
Wscript.Echo进程以进程ID:& ; intProcessID
WScript.Sleep 5000
Else
Wscript.Echo由于错误,无法启动进程:& errReturn
End If
WScript.Echo

'Win32_Process类的Create方法的错误返回码
'http://msdn.microsoft.com/en- us / library / windows / desktop / aa389388(v = vs.85).aspx
'0 =成功完成
'2 =拒绝访问
'3 =权限不足
' 8 =未知失败
'9 =路径未找到
'21 =无效参数

结束子



' **函数**

获取屏蔽密码的子程序
功能GetPass
'使用Internet Explorer屏蔽密码
'确保遵循technet.com说明并创建文件password.htm
'http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/04/how-can-i-mask-passwords-using-an-inputbox.aspx

设置objExplorer = WScript.CreateObject _
(InternetExplorer.Application,IE_)

objExplorer.Navigatefile:/// C:\SCRIPTS \password.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

如果strButton =Cancelledthen
Wscript.Quit
'Else
'Wscript.Echo strPassword
End If

'返回密码
GetPass = strPassword

结束函数


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.

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 & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery( _
    "Select * from Win32_Directory where Name = 'c:\\temp'")
For Each objFiles in colFiles
    errResults  = objFolder.Copy("f:\temp\test1.txt")
    Wscript.Echo errResults
Next

解决方案

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

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

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

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

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.

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:\temp folder, containing the text "myTextCommands", without the quotes.

' http://stackoverflow.com/questions/8884728/wmi-remote-process-to-copy-file
strCommand = "cmd /c echo myTextCommands > c:\temp\testscript.txt"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
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

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

where "strRemoteLog" is set to something like "c:\temp\MyLog.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:\scripts\SCRIPT1.bat c:\temp\ >>" & strRemoteLog
Call errProcess

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:\temp\SCRIPT1.bat " & strUserID & ">>" & strRemoteLog
Call errProcess

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:\temp\SCRIPT1*.txt Z:\scripts\LOGS\"
Call errProcess
strCommand="cmd /c net use * /del /Y"
Call errProcess
strCommand="cmd /c del c:\temp\SCRIPT1*.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.

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:\temp\MapZ.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 & "\root\cimv2")

' 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:\scripts\cleanpclocal.bat c:\temp\ /V /C /I /Q /H /R /Y>>" & strRemoteLog
Call errProcess

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

' Start PSEXEC against script
strCommand="cmd /c Z:\psexec \\%COMPUTERNAME% /accepteula -s -n 120 cmd /c c:\temp\cleanpclocal.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:\temp\clean*.txt Z:\scripts\LOGS\"
Call errProcess
strCommand="cmd /c net use * /del /Y"
Call errProcess
strCommand="cmd /c del c:\temp\clean*.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:\SCRIPTS\password.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复制到另一个,因为RPC和UNC不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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