keyongtech


  keyongtech > scripting.vbscript > 02/2009

 #1  
02-25-09, 09:11 PM
tpreitano
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
 #2  
02-25-09, 09:45 PM
ekkehard.horner
tpreitano schrieb:
[..]
> 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" )
[..]
 #3  
02-25-09, 09:47 PM
Richard Mueller [MVP]
<tpreitano> wrote in message
news:9acf
[..]
> & " /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.
 #4  
02-25-09, 09:51 PM
Pegasus \(MVP\)
<tpreitano> wrote in message
news:9acf
[..]
> & " /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.
 #5  
02-26-09, 02:30 PM
tpreitano
On Feb 25, 4:51 pm, "Pegasus \(MVP\)" <I> wrote:
[..]
>      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
Similar Threads
StdOut.ReadAll blocking?

Hello! I'm working on a script using the WshShell.Exec() method. To avoid buffer overflow problem (if I read the whole StdOut at the end of the execution, sometimes my...

redirect powershell stdout to objShell.Exec.Stdout.ReadAll()(

Hi, I have a similar problem I am dealing with. Hence posting in this discussion chain. I have a powershell script text.ps1 And then I have a .js file which is invoking...

copy stdout fails with permission denied when stdout is redirected

Running Windows XP / ActivePerl v5.8.7 The following combination causes a permission denied failure: * duplicate stdout in my script * redirect stdout on command line * run...

vbscript does .exec("ftp"), then hangs while accessing stdout and stderr

Hello, I am trying to write a vbscript ftp crawler. Using wscript.shell .run, I can run ftp with some pre-generated script. If I want to go recursively into folders, I...

Wsh.exec hangs getting StdOut.ReadAll from command line Winzip

I'm trying to write a simple backup script. I'd like to use wsh.exec to run the command line Winzip to create a zip file and capture the StdOut and write it to a log. It...


All times are GMT. The time now is 04:51 AM. | Privacy Policy