Jump to content

Recommended Posts

Posted

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? :confused:

 

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 )

Posted

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}

  • Thanks 1
Posted

Thanks for the help srochford that fixed the problem unfortunately I have another one now :o

 

I get a different error now to do with permissions :confused: 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 :(

Posted

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 )

  • Thanks 1

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...