FatBoy Posted February 15, 2010 Posted February 15, 2010 Hi all, I'm trying to list all the computers on my domain using vbs and output them to a text file. I think I'm nearly there but I don't know how to fix the error I'm getting: Line 9 Char 2 Bad file mode Can someone help please? Sub ListConnectedComputers( strDomain ) Dim objPDC Set objTextFile = objFSO.OpenTextFile("computers.txt") Set objPDC = getobject("WinNT://" & strDomain ) objPDC.filter = Array("Computer") For Each objComputer In objPDC 'WScript.Echo "Name: " & objComputer.Name objTextFile.WriteLine(objComputer.Name) Next objTextFile.Close End Sub Dim strDomain Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.CreateTextFile("computers.txt", True) Do strDomain = inputbox( "Please enter a domainname", "Input" ) Loop until strDomain <> "" ListConnectedComputers( strDomain )
srochford Posted February 15, 2010 Posted February 15, 2010 You need to specify that you're going to write to the text file in the open statement: Set objTextFile = objFSO.OpenTextFile("computers.txt",2) details here: OpenTextFile Method 1
somabc Posted February 15, 2010 Posted February 15, 2010 FYI You can also do this in Powershell $strCategory = "computer" $objDomain = New-Object System.DirectoryServices.DirectoryEntry $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = $objDomain $objSearcher.Filter = ("(objectCategory=$strCategory)") $colProplist = "name" foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)} $colResults = $objSearcher.FindAll() foreach ($objResult in $colResults {$objComputer = $objResult.Properties; $objComputer.name} 1
FatBoy Posted February 15, 2010 Author Posted February 15, 2010 Thanks for the help srochford that fixed the problem unfortunately I have another one now I get a different error now to do with permissions as if I don't have the rights to write to the txt file. Error Line 3 Char 2 Permission Denied Why wouldn't I have rights to the file? once its created I have checked the permissions and everything looks ok
srochford Posted February 15, 2010 Posted February 15, 2010 OK; I've now actually read your whole script and can see what's going on :-) You're trying to create the same file twice; the first one is the "Set objFile = objFSO.CreateTextFile("computers.txt", True)" line near the end, the second is the one I "fixed" You only need one such line - the createline is good (the "true" at the end will delete the existing file which keeps things tidy if you run this regularly to get a list) the code below does now work (I've actually run it this time ...) Sub ListConnectedComputers( strDomain ) Dim objPDC Set objPDC = getobject("WinNT://" & strDomain ) objPDC.filter = Array("Computer") For Each objComputer In objPDC 'WScript.Echo "Name: " & objComputer.Name objTextFile.WriteLine(objComputer.Name) Next objTextFile.Close End Sub Dim strDomain Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.CreateTextFile("computers.txt", True) Do strDomain = inputbox( "Please enter a domainname", "Input" ) Loop until strDomain <> "" ListConnectedComputers( strDomain ) 1
FatBoy Posted February 15, 2010 Author Posted February 15, 2010 thanks very much Steve you have been a great help and probably saved me hours! Cheers
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now