VBScript and PSExec - vbscript

Hi I m having problems reading the output provided by WScript.Shell and PSExec. My goal is to be able to read what PSExec.exe returns as text so I can perform some validation with InStr.
The Code is similar to this:
Const WshFinished = 1
Const WshFailed = 2
Dim cmdLine, strComputer
strComputer = "\\SomeComputer"
cmdLine = "psexec " & strComputer & " cmd /C " & """RD " & """%PROGRAMFILES%\APPFOLDER""" & " /S /Q | RD " & """%PROGRAMFILES%\COMMON FILES\APPFOLDER""" & " /S /Q | RD " & """%SYSTEMROOT%\temp\APPFOLDER""" & " /S /Q"""
Set WSH = WScript.CreateObject("WScript.Shell")
WSH.Exec(cmdLine)
Do While WSH.Status = 0
WScript.Sleep 100
Loop
Select Case WSH.Status
Case WshFinished
strOutput = WSH.StdOut.ReadAll
Case WshFailed
strOutput = WSH.StdErr.ReadAll
End Select
Wscript.Echo strOutput
If (InStr(strOutput, "validation text") > 0) Then
'Do Stuff
End IF
The Problem is that strOutput variable comes always empty and I can't perform text validation using - If (InStr(strOutput, "validation text") > 0)
Any Ideas?

Have you considered sending the output from psexec to an output file and then reading the output file from your vbscript?
psexec \\remotemachine command.exe >C:\temp\output.txt 2>&1
will execute command.exe and send output from stdOut and stdErr directly into the file output.txt in C:\temp. You can amend this for your own command.

Related

What's the difference between FSO.DeleteFolder() method and oWS.Run "%comspec% /c rmdir ...", 1, True?

I got a vbs that I wrote.
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oWS = CreateObject("WScript.Shell")
Set objFolderUsers = FSO.GetFolder("\\"& strComputer &"\C$\Users\").Subfolders
...
Later, I do something like :
For Each objFlder In objFolderUsers
user = Right(objFlder, Len(objFlder) - InStrRev(objFlder, "\"))
temp = objFlder & "\AppData\Local\Temp\"
'That's the line !...
If FSO.FolderExists(temp) Then FSO.DeleteFolder(temp)
If Not IsExcludeProfile(user) Then
If Left(objFlder.DateLastModified, 4) <= minYear Then
oWS.Run "%comspec% /c rmdir " & objFlder & " /s /q", 0, True
oWS.Run "%comspec% /c net user " & user & " /delete", 0, True
End If
End If
Next
my question is : Is there a difference between FSO.DeleteFolder(temp) and oWS.Run "%comspec% /c rmdir " & temp, 1, True because when I do the first all things are OK but when I do the second (the oWS.Run) AND objFlder = "Default" it is all deleted, not just the Temp as I want ...
Try to put enclosing quotes in the file name passed to the rmdir, because blank spaces may terminate the string that it receives before its end:
oWS.Run "%comspec% /c rmdir """ & objFlder & """ /s /q", 0, True
Just reminding that "" inside a string literal in vbs means a single " in the contents of the string.

Brackets in the folder name

I'm trying to create a script that runs a file the problem is that the folder where is it has the character "[]" and every time I try to open it displays an error saying that the path was not found... as I would to ignore this character?
Sub Main()
If WScript.Arguments.Count >= 1 Then
MyFileSWF = WScript.Arguments.Item(0)
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "CMD /C Start /Max "" ""[ Utilities ]\Flash Player Standalone 18.exe"" " & """" & MyFileSWF & """", 0, True
End If
End Sub
On Error Resume Next
Main
If Err.Number Then
WScript.Quit 4711
End If
This:
"CMD /C Start /Max "" ""[ Utilities ]\Flash Player Standalone 18.exe"" " & """" & MyFileSWF & """"
Is being interpreted as:
CMD /C Start /Max " "[ Utilities ]\Flash Player Standalone 18.exe" "%swfFile%"
Note how the directory name (with square-brackets) is actually excluded from the first quote-enclosed string.
Change it to this:
"CMD /C Start /Max ""[ Utilities ]\Flash Player Standalone 18.exe"" """ & MyFileSWF & """"

Popup to appear and close with a target process

I am trying to use the code located here, and with some tweaking for HTA. It works to a point:
Set wshShell = WScript.CreateObject( "WScript.Shell" )
changes into:
Set wshShell = CreateObject( "WScript.Shell" )
The popup comes up, but it won't go away until I click it. I need it appear when a process is running, then disappear when it ends. Why is my execution failing to do this?
ProgressMsg "Copying, Please wait.", "File Being Copied"
strCMD = "cmd.exe /c robocopy " & strLocalSemesterCourse & " " & strServerSemesterCourse &" " & strFileName & " /LOG+:" & strLogName & " /V /FP /TEE"
nReturn = objShell.Run(strCMD, 1, true)
ProgressMsg "", "Finished"
You need to define objProgressMsg as a global variable for this to work:
Dim objProgressMsg
...
ProgressMsg "Copying, Please wait.", "File Being Copied"
strCMD = "cmd.exe /c robocopy " & strLocalSemesterCourse & " " _
& strServerSemesterCourse &" " & strFileName & " /LOG+:" & strLogName _
& " /V /FP /TEE"
nReturn = objShell.Run(strCMD, 1, true)
ProgressMsg "", "Finished"
Without the global variable, ProgressMsg() will use a local variable objProgressMsg. Local variables don't retain their value after the function exits, so the variable will be empty every time you call the function.

VBScript WSHell.Run Multiple Processes

Does anyone know how I can accomplish the following:
I have a script that will run and get all users from AD with a homedir assigned and reset the permissions on this folder and everything under it. The problem is, it takes a very long time to complete and if I don't set the .run variable for Wait on return to true, I end up with thousands of cacls processes running. I want to limit the number of processes to say 50, and once one process finishes, move on to the next user. The script is really simple:
' Get Users Home Directories from AD
' Then grant Administrators FULL CONTROL and the USER MODIFY ONLY
'
Set WshShell = WScript.CreateObject("WScript.Shell")
'
' Get users with home directories
WScript.Echo "Retrieving user account names and home directories from active directory. . ."
Set objWMIService = GetObject("winmgmts:root\directory\ldap")
Set colItems = objWMIService.ExecQuery("Select ds_sAMAccountName, ds_homeDirectory From ds_user WHERE ds_homeDirectory IS NOT NULL")
WScript.Echo ""
' Loop through users setting permissions
For Each oUser in colItems
WScript.Echo "Setting permissions for " & oUser.ds_sAMAccountName & " Directory " & oUser.ds_homeDirectory
WshShell.Run "xcacls " & oUser.ds_homeDirectory & " /y /t /g Administrators:F", 0, True
WshShell.Run "xcacls " & oUser.ds_homeDirectory & " /e /t /g " & oUser.ds_sAMAccountName & ":C", 0, True
Next
Does anyone have any idea how I can do this?
Thanks
Edit::::
I think i got it figured out...
For Each oUser in colItems
numrunning = HowMany
while numrunning > 25
WScript.Echo "Waiting for other processes to finish"
WScript.Sleep 10000
numrunning = HowManyRunning
wend
WScript.Echo "Setting permissions for " & oUser.ds_sAMAccountName & " Directory " & oUser.ds_homeDirectory
WshShell.Run "xcacls " & oUser.ds_homeDirectory & " /y /t /g Administrators:F", 0, False
WshShell.Run "xcacls " & oUser.ds_homeDirectory & " /e /t /g " & oUser.ds_sAMAccountName & ":C", 0, False
Next
Function HowManyRunning()
Dim Proc1,Proc2,Proc3
Set Proc1 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set Proc2 = Proc1.ExecQuery("select * from win32_process" )
HowMany=0
For Each Proc3 in Proc2
If LCase(Proc3.Caption) = "xcacls.exe" Then
HowMany=HowMany + 1
End If
Next
End Function

set permissions with a vbs script

I have a VBS script that downloads a file on login and places it in a given folder, It works brilliantly in some places but in others it falls over because the file was created by user1 and user2 can't overwrite it.
How would i give the group "Everyone" full control of a given file using a VBS script?
Something like this:
Set WshShell = CreateObject("WScript.Shell")
strFile = "c:\test_folder\test_file.txt"
setPerms = "%COMSPEC% /c echo Y| C:\windows\system32\cacls.exe " & Chr(34) & strFile & Chr(34) & " /G domain\everyone:F"
wscript.echo setPerms
WshShell.run setPerms
Partially gleaned from here:
http://social.technet.microsoft.com/forums/en-us/ITCG/thread/6CDA091A-6B3D-4F58-8374-9A46F59F389A
One way of doing it would be to use the CACLS command line tool. Just run it from your script using Shell.Run.
Here's another link to information about how to use CACLS that has some samples.
Function giveFullPermissionToFolder(strFolder)
Dim objShell, strCmd, intRunError
Set objShell = CreateObject("Wscript.Shell")
strCmd = "%comspec% /c echo Y| cacls " & strFolder & " /T /E /C /G Users:F"
intRunError = objShell.Run(strCmd, 2, True)
If intRunError<>0 Then
Reporter.ReportEvent micFail, "giveFullPermissionToFolder" , "Unable to give full permission to " & strFolder
End If
Set objShell=Nothing
End Function

Resources