Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbscript + shell + output var?

Status
Not open for further replies.

g37s

IS-IT--Management
Dec 18, 2008
6
US
I created this command in vbscript and output to a file, but really I would like to assign the output to a variable so I can 'If variable = "whatever" then' in my vbscript code. Any ideas?

Set objShell = CreateObject("WScript.Shell")
strRun = "%comspec% /c cacls C:\ | find ""Domain Admins"" > C:\woop.txt"
objShell.Run strRun, 1, True
 
I was kind of able to get this command to set the variable. I say kind of because its setting the var twice but the 2nd one is correct. Which kind of works but its sloppy. Anyway after this code I tried to set the variable in vbscript but it does not return anything for %myvar2%

MS-DOS
--------
C:\>for /F "TOKENS=*" %x in ('find C:\woop.txt "Domain Admins"') do set myvar2=%
x

C:\>set myvar2=---------- C:\WOOP.TXT

C:\>set myvar2=ATUSA\Domain Admins:(OI)(CI)F

VBSCRIPT
---------
Set objShell2 = CreateObject("WScript.Shell")
strRun = "%comspec% /c for /F ""TOKENS=*"" %x in ('find C:\woop.txt ""Domain Admins""') do set myvar2=%x"

objShell2.Run strRun, 1, True
doit = shell.ExpandEnvironmentStrings("%myvar2%")
wscript.echo doit
 
That didn't help me... not sure what I'm looking for really.
 
This is what PHV is suggesting...

Code:
Option Explicit

Dim oShell, oExec, sLine

Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.Exec("%COMSPEC% /c cacls C:\")

Do While oExec.Status = 1
	WScript.Sleep 50
Loop

Do Until oExec.StdOut.AtEndOfStream
	sLine = oExec.StdOut.ReadLine
	If InStr(LCase(sLine), "domain admins") > 0 Then
		WScript.Echo "Admins be heer, Arr!"
	End If
Loop

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
I need it to output when it does not find any admins. I add that line of code but it seems to output 'no admins' like 15 times! Any way to fix that? Thx again. Code below.


Dim oShell, oExec, sLine

Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.Exec("%COMSPEC% /c cacls D:\")

Do While oExec.Status = 1
WScript.Sleep 50
Loop

Do Until oExec.StdOut.AtEndOfStream
sLine = oExec.StdOut.ReadLine
If InStr(LCase(sLine), "domain admins") > 0 Then
WScript.Echo "Admins be heer, Arr!"
Else
WScript.Echo "No Admins"
End If

Loop
 
Replace this:
sLine = oExec.StdOut.ReadLine
with this:
sLine = oExec.StdOut.ReadAll

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top