Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - StdOut.ReadAll() in VBScript

Reply
 
Old 02-25-2009   #1 (permalink)
tpreitano


 
 

StdOut.ReadAll() in VBScript

I am writing a script to read user names from a txt file then run the
calcs command on their application data folder in their home dir to
check their permissions. If they dont have them set correctly, I take
ownership, removed whatever rights they have on the home dir and
reapply.

It works fine with reading the txt file and running the last three
commands for reapplying rights, but when I added to read the output
from the initial cacls command, I get an error. Object Required: '2'
on line... strText = cmdCacls.StdOut.ReadAll()

I can even add oFiletxt.WriteLine(cmdCacls) to test and the output is
2.

Here is the complete code. Thanks!
-------------------------------

Dim cmdCacls

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\users.txt")
Set oFiletxt = objFSO.CreateTextFile("C:\results.txt")
Set objShell = CreateObject("Wscript.Shell")

Do Until objTextFile.AtEndOfStream
strUserLine = objTextFile.Readline
strHomeFolder = "<main user share>" & strUserLine

cmdCacls = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
& "\applic~1", 0, True)
strText = cmdCacls.StdOut.ReadAll()
If Instr(strText, strUserLine) > 0 Then

cmdOwn = objShell.Run("%COMSPEC% /c Echo Y| takeown /f " &
strHomeFolder & " /r /a", 0, True)
cmdTake = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
& " /t /e /c /r <domain>\" & strUserLine, 0, True)
cmdPerm = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
& " /t /e /c /g <domain>\" & strUserLine & ":F ", 0, True)
oFiletxt.WriteLine("Done for user " & strUserLine)
Else

oFiletxt.WriteLine("Error assigning permissions to user " &
strUserLine & " on " & strHomeFolder)

End If

Loop

My System SpecsSystem Spec
Old 02-25-2009   #2 (permalink)
ekkehard.horner


 
 

Re: StdOut.ReadAll() in VBScript

tpreitano@xxxxxx schrieb:
Quote:

> I am writing a script to read user names from a txt file then run the
> calcs command on their application data folder in their home dir to
> check their permissions. If they dont have them set correctly, I take
> ownership, removed whatever rights they have on the home dir and
> reapply.
>
> It works fine with reading the txt file and running the last three
> commands for reapplying rights, but when I added to read the output
> from the initial cacls command, I get an error. Object Required: '2'
> on line... strText = cmdCacls.StdOut.ReadAll()
>
> I can even add oFiletxt.WriteLine(cmdCacls) to test and the output is
> 2.
>
> Here is the complete code. Thanks!
> -------------------------------
>
> Dim cmdCacls
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objTextFile = objFSO.OpenTextFile("C:\users.txt")
> Set oFiletxt = objFSO.CreateTextFile("C:\results.txt")
> Set objShell = CreateObject("Wscript.Shell")
>
> Do Until objTextFile.AtEndOfStream
> strUserLine = objTextFile.Readline
> strHomeFolder = "<main user share>" & strUserLine
>
> cmdCacls = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
> & "\applic~1", 0, True)
objShell.Run returns an integer - the return value of the started process.
Probably you meant to call .Exec:

Set cmdCacls = objShell.Exec( _
"%COMSPEC% /c Echo Y| cacls " & strHomeFolder & "\applic~1" )
Quote:

> strText = cmdCacls.StdOut.ReadAll()
> If Instr(strText, strUserLine) > 0 Then
>
> cmdOwn = objShell.Run("%COMSPEC% /c Echo Y| takeown /f " &
> strHomeFolder & " /r /a", 0, True)
> cmdTake = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
> & " /t /e /c /r <domain>\" & strUserLine, 0, True)
> cmdPerm = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
> & " /t /e /c /g <domain>\" & strUserLine & ":F ", 0, True)
> oFiletxt.WriteLine("Done for user " & strUserLine)
> Else
>
> oFiletxt.WriteLine("Error assigning permissions to user " &
> strUserLine & " on " & strHomeFolder)
>
> End If
>
> Loop
My System SpecsSystem Spec
Old 02-25-2009   #3 (permalink)
Richard Mueller [MVP]


 
 

Re: StdOut.ReadAll() in VBScript


<tpreitano@xxxxxx> wrote in message
news:4191b193-50db-495b-9e14-f49268b19acf@xxxxxx
Quote:

>I am writing a script to read user names from a txt file then run the
> calcs command on their application data folder in their home dir to
> check their permissions. If they dont have them set correctly, I take
> ownership, removed whatever rights they have on the home dir and
> reapply.
>
> It works fine with reading the txt file and running the last three
> commands for reapplying rights, but when I added to read the output
> from the initial cacls command, I get an error. Object Required: '2'
> on line... strText = cmdCacls.StdOut.ReadAll()
>
> I can even add oFiletxt.WriteLine(cmdCacls) to test and the output is
> 2.
>
> Here is the complete code. Thanks!
> -------------------------------
>
> Dim cmdCacls
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objTextFile = objFSO.OpenTextFile("C:\users.txt")
> Set oFiletxt = objFSO.CreateTextFile("C:\results.txt")
> Set objShell = CreateObject("Wscript.Shell")
>
> Do Until objTextFile.AtEndOfStream
> strUserLine = objTextFile.Readline
> strHomeFolder = "<main user share>" & strUserLine
>
> cmdCacls = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
> & "\applic~1", 0, True)
> strText = cmdCacls.StdOut.ReadAll()
> If Instr(strText, strUserLine) > 0 Then
>
> cmdOwn = objShell.Run("%COMSPEC% /c Echo Y| takeown /f " &
> strHomeFolder & " /r /a", 0, True)
> cmdTake = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
> & " /t /e /c /r <domain>\" & strUserLine, 0, True)
> cmdPerm = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
> & " /t /e /c /g <domain>\" & strUserLine & ":F ", 0, True)
> oFiletxt.WriteLine("Done for user " & strUserLine)
> Else
>
> oFiletxt.WriteLine("Error assigning permissions to user " &
> strUserLine & " on " & strHomeFolder)
>
> End If
>
> Loop
I believe you need to use something similar to:
========
Set objCacls = objShell.Exec("<your command.")

Do Until objCacls.StdOut.AtEndOfStream
strText = objCalcs.StdOut.ReadAll()
Loop
==========
The main thing is to use the Exec method rather than the Run method and use
a Set statement to create the object reference. Note this requires WSH 5.6.
Since you use ReadAll(), perhaps the Do Until Loop is not necessary.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 02-25-2009   #4 (permalink)
Pegasus \(MVP\)


 
 

Re: StdOut.ReadAll() in VBScript


<tpreitano@xxxxxx> wrote in message
news:4191b193-50db-495b-9e14-f49268b19acf@xxxxxx
Quote:

>I am writing a script to read user names from a txt file then run the
> calcs command on their application data folder in their home dir to
> check their permissions. If they dont have them set correctly, I take
> ownership, removed whatever rights they have on the home dir and
> reapply.
>
> It works fine with reading the txt file and running the last three
> commands for reapplying rights, but when I added to read the output
> from the initial cacls command, I get an error. Object Required: '2'
> on line... strText = cmdCacls.StdOut.ReadAll()
>
> I can even add oFiletxt.WriteLine(cmdCacls) to test and the output is
> 2.
>
> Here is the complete code. Thanks!
> -------------------------------
>
> Dim cmdCacls
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objTextFile = objFSO.OpenTextFile("C:\users.txt")
> Set oFiletxt = objFSO.CreateTextFile("C:\results.txt")
> Set objShell = CreateObject("Wscript.Shell")
>
> Do Until objTextFile.AtEndOfStream
> strUserLine = objTextFile.Readline
> strHomeFolder = "<main user share>" & strUserLine
>
> cmdCacls = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
> & "\applic~1", 0, True)
> strText = cmdCacls.StdOut.ReadAll()
> If Instr(strText, strUserLine) > 0 Then
>
> cmdOwn = objShell.Run("%COMSPEC% /c Echo Y| takeown /f " &
> strHomeFolder & " /r /a", 0, True)
> cmdTake = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
> & " /t /e /c /r <domain>\" & strUserLine, 0, True)
> cmdPerm = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
> & " /t /e /c /g <domain>\" & strUserLine & ":F ", 0, True)
> oFiletxt.WriteLine("Done for user " & strUserLine)
> Else
>
> oFiletxt.WriteLine("Error assigning permissions to user " &
> strUserLine & " on " & strHomeFolder)
>
> End If
>
> Loop
In your code
[cmdCacls = objShell.Run("%COMSPEC% /c Echo Y| . . .]
cmdCacls is not an object, yet a little further down you try to use it
as an object:
strText = cmdCacls.StdOut.ReadAll()

Try this instead:
Set cmdCacls = objShell.Exec("%COMSPEC% /c Echo Y| . . .
strText = cmdCacls.StdOut.ReadAll()

From an overall point of view I note that you're executing these four
commands:
- cacls.exe
- takeown.exe
- cacls.exe
- cacls.exe

Instead of repeatedly shelling out to a Command Line environment, you could
simplify the task by running the whole job in that environment, e.g. like
so:
@echo off
set UserNames=c:\Users.txt
set Results=c:\Results.txt
echo Job run on %date% at %time% > %Results%
for %%a in (%UserNames%) do (
cacls \\server\shares\%%a | find /i "%%a"
if %ErrorLevel% EQU 0 (
takeown.exe /..
cacls /..
cacls /..
echo Done for User %%a
)
)
IMHO, scripts that run wholly in one or the other environment are simpler to
code and more robust than hybrid scripts.


My System SpecsSystem Spec
Old 02-26-2009   #5 (permalink)
tpreitano


 
 

Re: StdOut.ReadAll() in VBScript

On Feb 25, 4:51*pm, "Pegasus \(MVP\)" <I....@xxxxxx> wrote:
Quote:

> <tpreit...@xxxxxx> wrote in message
>
> news:4191b193-50db-495b-9e14-f49268b19acf@xxxxxx
>
>
>
>
>
Quote:

> >I am writing a script to read user names from a txt file then run the
> > calcs command on their application data folder in their home dir to
> > check their permissions. If they dont have them set correctly, I take
> > ownership, removed whatever rights they have on the home dir and
> > reapply.
>
Quote:

> > It works fine with reading the txt file and running the last three
> > commands for reapplying rights, but when I added to read the output
> > from the initial cacls command, I get an error. Object Required: '2'
> > on line... * * *strText = cmdCacls.StdOut.ReadAll()
>
Quote:

> > I can even add oFiletxt.WriteLine(cmdCacls) to test and the output is
> > 2.
>
Quote:

> > Here is the complete code. Thanks!
> > -------------------------------
>
Quote:

> > Dim cmdCacls
>
Quote:

> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > Set objTextFile = objFSO.OpenTextFile("C:\users.txt")
> > Set oFiletxt = objFSO.CreateTextFile("C:\results.txt")
> > Set objShell = CreateObject("Wscript.Shell")
>
Quote:

> > Do Until objTextFile.AtEndOfStream
> > * *strUserLine = objTextFile.Readline
> > * *strHomeFolder = "<main user share>" & strUserLine
>
Quote:

> > cmdCacls = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
> > & "\applic~1", 0, True)
> > strText = cmdCacls.StdOut.ReadAll()
> > * *If Instr(strText, strUserLine) > 0 Then
>
Quote:

> > cmdOwn = objShell.Run("%COMSPEC% /c Echo Y| takeown /f " &
> > strHomeFolder & " /r /a", 0, True)
> > cmdTake = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
> > & " /t /e /c /r <domain>\" & strUserLine, 0, True)
> > cmdPerm = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder
> > & " /t /e /c /g <domain>\" & strUserLine & ":F ", 0, True)
> > oFiletxt.WriteLine("Done for user " & strUserLine)
> > Else
>
Quote:

> > oFiletxt.WriteLine("Error assigning permissions to user " &
> > strUserLine & " on " & strHomeFolder)
>
Quote:

> > End If
>
Quote:

> > Loop
>
> In your code
> [cmdCacls = objShell.Run("%COMSPEC% /c Echo Y| *. . .]
> cmdCacls is not an object, yet a little further down you try to use it
> as an object:
> strText = cmdCacls.StdOut.ReadAll()
>
> Try this instead:
> Set cmdCacls = objShell.Exec("%COMSPEC% /c Echo Y| *. . .
> strText = cmdCacls.StdOut.ReadAll()
>
> From an overall point of view I note that you're executing these four
> commands:
> - cacls.exe
> - takeown.exe
> - cacls.exe
> - cacls.exe
>
> Instead of repeatedly shelling out to a Command Line environment, you could
> simplify the task by running the whole job in that environment, e.g. like
> so:
> @echo off
> set UserNames=c:\Users.txt
> set Results=c:\Results.txt
> echo Job run on %date% at %time% > %Results%
> for %%a in (%UserNames%) do (
> * cacls \\server\shares\%%a | find /i "%%a"
> * if %ErrorLevel% EQU 0 (
> * * *takeown.exe /..
> * * *cacls /..
> * * *cacls /..
> * * *echo Done for User %%a
> * )
> )
> IMHO, scripts that run wholly in one or the other environment are simplerto
> code and more robust than hybrid scripts.- Hide quoted text -
>
> - Show quoted text -
Thanks everyone! I did switch it to objShell.Exec before, but forgot
to take out ,0 , True) That is when I gave up. I should have read the
error closer. Seeing your responses and the error, I realized, wow...
it's usually the little things.
Also, thanks for the advice also pegasus

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
redirecting stdin and stdout PowerShell
StdOut.ReadAll blocking? VB Script
redirect powershell stdout to objShell.Exec.Stdout.ReadAll()( PowerShell
stdout redirection PowerShell
Using convertto-html with stdout PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46