+ Post New Thread
Results 1 to 6 of 6
How do you do....it? Thread, List computers in Domain in a text file with vbs in Technical; Hi all, I'm trying to list all the computers on my domain using vbs and output them to a text ...
  1. #1
    FatBoy's Avatar
    Join Date
    Oct 2007
    Location
    Kent, UK
    Posts
    249
    Thank Post
    55
    Thanked 20 Times in 16 Posts
    Rep Power
    17

    List computers in Domain in a text file with vbs

    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 )

  2. #2

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,156
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    124
    You need to specify that you're going to write to the text file in the open statement:

    Code:
    Set objTextFile = objFSO.OpenTextFile("computers.txt",2)
    details here: OpenTextFile Method

  3. Thanks to srochford from:

    FatBoy (15th February 2010)

  4. #3
    somabc's Avatar
    Join Date
    Oct 2007
    Location
    London
    Posts
    2,337
    Thank Post
    83
    Thanked 388 Times in 258 Posts
    Rep Power
    111
    FYI You can also do this in Powershell

    Code:
    $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}

  5. Thanks to somabc from:

    FatBoy (15th February 2010)

  6. #4
    FatBoy's Avatar
    Join Date
    Oct 2007
    Location
    Kent, UK
    Posts
    249
    Thank Post
    55
    Thanked 20 Times in 16 Posts
    Rep Power
    17
    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

  7. #5

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,156
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    124
    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 ...)

    Code:
    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 )

  8. Thanks to srochford from:

    FatBoy (15th February 2010)

  9. #6
    FatBoy's Avatar
    Join Date
    Oct 2007
    Location
    Kent, UK
    Posts
    249
    Thank Post
    55
    Thanked 20 Times in 16 Posts
    Rep Power
    17
    thanks very much Steve you have been a great help and probably saved me hours!

    Cheers

SHARE:
+ Post New Thread

Similar Threads

  1. PHP to text file
    By Hightower in forum Web Development
    Replies: 21
    Last Post: 24th October 2008, 09:52 AM
  2. VBS Change File on a Certain Line?
    By Samson in forum Scripts
    Replies: 7
    Last Post: 31st August 2007, 10:52 AM
  3. Replies: 4
    Last Post: 7th March 2007, 03:37 PM
  4. VBS script for prestaging computers anyone?
    By ChrisH in forum Scripts
    Replies: 7
    Last Post: 23rd June 2006, 09:59 AM
  5. URLblacklist.com as text file
    By adamyoung in forum How do you do....it?
    Replies: 11
    Last Post: 9th February 2006, 02:35 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •