【问题标题】:Read text file on remote computer on different domain在不同域的远程计算机上读取文本文件
【发布时间】:2014-01-12 10:26:58
【问题描述】:

我下面的代码是在服务器的文本文件中查找一个单词 (\\10.0.1.22\C$\Logs\text.txt)

对于用户:SBICAD\user 工作正常。 (SBICAD 是服务器中的域)

对于用户:ThisComputer\user 失败(“登录失败:未知用户名或密码错误”)

注意:我在本地机器 XP 上运行脚本。 Server 2003 和 Local Machine XP 在同一个网络中。

dim objService 
Set objShell = CreateObject("WScript.Shell")
strComputer = "10.0.1.22" 
strDomain = "SBICAD"
Const WbemAuthenticationLevelPktPrivacy = 6

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, _
  "root\cimv2:Win32_LogicalDisk='c:'", _
  "administrator", _
  "jan@2014", _
  "MS_409", _
  "ntlmdomain:" + strDomain)

objSWbemServices.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")

strFilePath = "\\" & strComputer & "\C$\Logs\text.txt"
Set objTextFile = objFSO.OpenTextFile(strFilePath , ForReading)

Do Until objTextFile.AtEndOfStream
  CurrentLine= objTextFile.ReadLine
  if ((InStr(1, CurrentLine, "James", 1) > 0) And (InStr(1, CurrentLine, "John", 1) > 0) )Then 
    Wscript.Echo "James and John Found"
  end if
Loop

objTextFile.Close

【问题讨论】:

    标签: vbscript automation windows-xp wmi


    【解决方案1】:

    您的问题有点不清楚,因为您发布的代码的第 26 行无法引发您描述的错误。

    但是,假设我正确理解了您的问题,问题是ConnectServer 在您尝试从本地计算机对远程服务器的本地用户进行身份验证时引发错误。这不起作用,因为主机 A 上的本地用户在远程主机 B 上是未知的,因此无法在那里进行身份验证:

    您需要在两个主机上都知道的用户(这是域提供的):

    或远程主机 B 上的本地用户:


    话虽如此,您为什么首先尝试建立 WMI 连接?您永远不会在代码中的任何地方使用它,并且它不会验证您实际尝试使用的 SMB 连接。 SMB 连接(访问文件共享)必须经过身份验证in a different way

    Set net = CreateObject("WScript.Network")
    
    username   = "user"  'domain user or user on the remote host!
    password   = "pass"
    drive      = "S:"
    remotePath = "\\" & strComputer & "\C$\Logs"
    
    net.MapNetworkDrive drive, remotePath, False, username, password
    

    然后您可以像这样在远程位置读取文件:

    Set objTextFile = objFSO.OpenTextFile("S:\text.txt")
    
    Do Until objTextFile.AtEndOfStream
      ...
    Loop
    
    objTextFile.Close
    

    完成后,您可以像这样删除网络驱动器:

    net.RemoveNetworkDrive drive, True
    

    请注意,用户帐户必须具有远程主机上的管理权限才能访问管理共享 C$。创建一个允许非管理员访问日志目录的专用共享可能是个好主意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-23
      • 2012-07-17
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      相关资源
      最近更新 更多