All Forums >> [Scripting] >> WSH & Client Side VBScript >> 800a330e input past enf of file error Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
I only get this if I specify an incorrect server name or leave it blank. I can't figure out how to make it check that the name is correct first and then write to my output then echo server name incorrect. Otherwise it works fine i have tried so many things to get this to work. The program checks local group membership of a specified user.
found = False errorlog = "c:\error.txt" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.CreateTextFile(errorlog, True) ' On Error Resume Next computer = inputbox ("computer name") user = lcase(inputbox ("enter user name")) strComputer = computer Set colGroups = GetObject("WinNT://" & strComputer) colGroups.Filter = Array("Group")
For Each objGroup In colGroups
For Each objUser In objGroup.Members
If lcase(objUser.name) = user Then found=True
End If Next If found=true Then objfile.writeline Now & " user " & user & " is a member of " & objGroup.Name & " on " & strcomputer Else objfile.writeline Now & " user " & user & " is NOT a member of " & objgroup.name end If
found=False Next
objFile.Close result = objfso.OpenTextFile(errorlog, 1).ReadAll wscript.echo result
The one I'd try would be to add an If statement that checks the value of Err.Number. If that value has changed from 0, an error has occured. This would be a good check to put in after the line "Set colGroups = GetObject("WinNT://" & strComputer)", where an error is likely the result of the computer not existing.
You can also add a loop that checks the values entered for the computer and user names to make sure that they are not blank.
Here's how it might look all together...
found = False errorlog = "c:\error.txt" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.CreateTextFile(errorlog, True)
'Unremarked the On Error statement so the script will get to the If statement to check for an error. On Error Resume Next
'Loop until a non-blank value is entered for both ehe user and computer names Do Until computer <> "" And user <> "" computer = inputbox ("computer name") user = lcase(inputbox ("enter user name")) Loop
strComputer = computer 'Clear any previous error codes Err.Clear Set colGroups = GetObject("WinNT://" & strComputer) 'Check to see if the script was able to contact a computer with the name provided If Err.Number <>0 then Wscript.Echo "Invalid computer name or remote system unavailable!" Wscript.quit End If
colGroups.Filter = Array("Group")
For Each objGroup In colGroups
For Each objUser In objGroup.Members
If lcase(objUser.name) = user Then found=True
End If Next If found=true Then objfile.writeline Now & " user " & user & " is a member of " & objGroup.Name & " on " & strcomputer Else objfile.writeline Now & " user " & user & " is NOT a member of " & objgroup.name end If
found=False Next
objFile.Close result = objfso.OpenTextFile(errorlog, 1).ReadAll wscript.echo result
thanks the loop appears to help but I can' t get the output to work correctly if err.number <> 0 it doesn't echo or write a line if I change the code to write. It just comes up witn a little window that has an ok and is blank. Here is my revised code which is from example two.
found = False errorlog = "c:\error.txt" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.CreateTextFile(errorlog, True) 'Unremarked the On Error statement so the script will get to the If statement to check for an error. On Error Resume Next 'Loop until a non-blank value is entered for both ehe user and computer names Do Until computer <> "" And user <> "" computer = inputbox ("computer name") user = lcase(inputbox ("enter user name")) Loop strComputer = computer 'Clear any previous error codes Err.Clear Set colGroups = GetObject("WinNT://" & strComputer) 'Check to see if the script was able to contact a computer with the name provided If Err.Number <> 0 then objfile.writeline Now & " computer " & strcomputer & " is invalid or available! " Wscript.quit End If colGroups.Filter = Array("Group") For Each objGroup In colGroups For Each objUser In objGroup.Members
If lcase(objUser.name) = user Then found=True
End If Next If found=true Then objfile.writeline Now & " user " & user & " is a member of " & objGroup.Name & " on " & strcomputer Else objfile.writeline Now & " user " & user & " is NOT a member of " & objgroup.name end If found=False Next objFile.Close result = objfso.OpenTextFile(errorlog, 1).ReadAll wscript.echo result