Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading a text file then run DSQuery then output to text file

Posted on 2011-03-24
13
Medium Priority
?
2,027 Views
Last Modified: 2012-05-11
Hi Team,
I need a assist with coding for VBS + Dsquery, Here what i need it to do.
1. Read a text file for input line by line (possible to have a string).
2. Then the input then would run against the following command (to capture any Distingushname result)
dsquery ou forestroot -name -o dn
dsquery user forestroot -name -o dn
dsquery group forestroot -name -o dn
3. If there is a result, capture the distingushname then output it to another text file.

I have the following starting code for reading the input but not further on how to progress below.
Dim WshShell, oExec
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objInputFile = objFSO.OpenTextFile("c:\scripts\computers.txt",1)
Set objOutputFile = objFSO.OpenTextFile("C:\scripts\output.txt",8,True)

Do until objInputFile.AtEndofStream
	strcomputer = objInputFile.ReadLine
	strCommand = "dsquery ou forestroot -name " & strComputer "-o dn"
	Set oExec = WShShell.Exec(strCommand)
	If (oExec.Status = 0) Then
		If (oExec.stdOut.ReadAll = "") Then
			objOutputFile.WriteLine(strComputer)
		End If
	End If
Loop

objInputFile.Close
objOutputFile.Close

Open in new window

Base Code from Ravikanth.Chaganti
0
Comment
Question by:AhMike
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 35212550
Hi, I haven't tested it, but does this work?

Regards,

Rob.
Dim WshShell, oExec
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objInputFile = objFSO.OpenTextFile("c:\scripts\computers.txt",1)
strOutput = "C:\scripts\output.txt"

Do until objInputFile.AtEndofStream
	strLine= objInputFile.ReadLine
	strCommand = "cmd /c dsquery ou forestroot -name " & strLine & " -o dn | find ""OU="" >> " & strOutput
	WShShell.Run strCommand, 0, True
	strCommand = "cmd /c dsquery user forestroot -name " & strLine & " -o dn | find ""CN="" >> " & strOutput
	WShShell.Run strCommand, 0, True
	strCommand = "cmd /c dsquery group forestroot -name " & strLine & " -o dn | find ""CN="" >> " & strOutput
	WShShell.Run strCommand, 0, True
Loop

objInputFile.Close
objOutputFile.Close

MsgBox "Done"

Open in new window

0
 

Author Comment

by:AhMike
ID: 35212614
testing it now... 1 moment
0
 

Author Comment

by:AhMike
ID: 35212628
Getting a runtime error :

Line 19
Error: Object required : 'objOutputfile'
Code :800A01A8
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:AhMike
ID: 35212662
Hi Rob,

Thanks abit tweak and you code work... but strange

group and user seems to be working fine. However there no return value for OU. Anyway i will award you the points 1st.

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35212772
Hi, thanks for the grade.  That is a bit odd about the OUs....if you run the command from a command prompt manually, does it find it?

Rob.
0
 

Author Comment

by:AhMike
ID: 35213019
when i run a manually with this : it will return the results.
dsquery ou forestroot -name DRA* -o dn
"OU=meow Groups,OU=Group Directory,DC=abc-ap,DC=office,DC=home,DC=com"

Could it because the find was skip bcos of two OU ?
0
 

Author Comment

by:AhMike
ID: 35213064
Hmmm if i insert

List File :
abc  
BCD Group


The BCD Group Ou that cant be searched
0
 

Author Comment

by:AhMike
ID: 35213141
I'm adding a & chr(34) to create " string " ..  but still getting a syntax error :

Dim WshShell, oExec
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objInputFile = objFSO.OpenTextFile("c:\temp\mike\list.txt",1)
strOutput = "C:\temp\mike\output.txt"

Do until objInputFile.AtEndofStream
	strLine= objInputFile.ReadLine
	strCommand = "cmd /c dsquery ou forestroot -name " & Chr(34) & strLine & Chr(34) " -o dn | find ""OU="" >> " & strOutput
	WShShell.Run strCommand, 0, True
	strCommand = "cmd /c dsquery user forestroot -name " & chr(34) & strLine & chr(34) " -o dn | find ""CN="" >> " & strOutput
	WShShell.Run strCommand, 0, True
	strCommand = "cmd /c dsquery group forestroot -name " & chr(34) & strLine & chr(34) " -o dn | find ""CN="" >> " & strOutput
	WShShell.Run strCommand, 0, True
Loop

objInputFile.Close


MsgBox "Done"

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35213815
You won't need the Chr(34).

If you are running DRA* in your manual search, then try putting
DRA*

in your text file.

Regards,

Rob.
0
 

Author Comment

by:AhMike
ID: 35228495
Hi Rob,

If i need string ... how should i run it ? lets say the group name is Class A instead of ClassA
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35228557
Oh, I understand....sorry, missed that....try this.

Rob.
Do until objInputFile.AtEndofStream
	strLine= objInputFile.ReadLine
	strCommand = "cmd /c dsquery ou forestroot -name """ & strLine & """ -o dn | find ""OU="" >> " & strOutput
	WShShell.Run strCommand, 0, True
	strCommand = "cmd /c dsquery user forestroot -name """ & strLine & """ -o dn | find ""CN="" >> " & strOutput
	WShShell.Run strCommand, 0, True
	strCommand = "cmd /c dsquery group forestroot -name """ & strLine & """ -o dn | find ""CN="" >> " & strOutput
	WShShell.Run strCommand, 0, True
Loop

Open in new window

0
 

Author Comment

by:AhMike
ID: 35311677
IT WORKED!!! THANKS ROB !
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35311709
No problem. Glad to help.

Rob.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here's a look at newsworthy articles and community happenings during the last month.
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question