My below code is to find a word in a text file in server (\\10.0.1.22\C$\Logs\text.txt
)
For User: SBICAD\user
its working fine. (SBICAD is domain in server)
For User: ThisComputer\user
it fails ("Logon failure: unknown username or bad password")
Note: Im running the script on local machine XP. Server 2003 and Local Machine XP are in the same network.
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
Your question is a little unclear, because line 26 of the code you posted couldn't raise the error you described.
However, assuming I understand your question correctly the problem is that ConnectServer
is raising the error when you try to authenticate with a local user from your local machine against a remote server. This doesn't work, because a local user on host A is unknown on the remote host B and thus cannot authenticate there:
You need either a user that is known on both hosts (which is what a domain provides):
or a local user on the remote host B:
With that said, why are you trying to establish a WMI connection in the first place? You never use it anywhere in your code, and it won't authenticate the SMB connection you're actually trying to use. SMB connections (access to file shares) must be authenticated 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
Then you can work read files on the remote location like this:
Set objTextFile = objFSO.OpenTextFile("S:\text.txt")
Do Until objTextFile.AtEndOfStream
...
Loop
objTextFile.Close
After you're finished you can remove the network drive like this:
net.RemoveNetworkDrive drive, True
Note that the user account must have administrative privileges on the remote host to be able to access the administrative share C$
. It might be a good idea to create a dedicated share that allows non-admin access to the log directory.