set permissions with a vbs script - vbscript

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

Related

wshShell.run single line command wont set variable and pass correctly

the script is executed from the network and is supposed to bring in a config file located in the local machines C: drive with a path for mapping.
I've tried a few different ideas but every time I get "System error 67 has occurred. The network name cannot be found." Any help is appreciated as I can't resolve the issue. (I'm a newbie to command line)
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd /k cd.. & cd CIEB_Group3 & set /p RootServer=<Server.txt & net use K: %RootServer% /pers:yes", 1, True
Instead of creating a VBS file that calls a single long command, it would be possible to do this using more VBScript, allowing further development at a later date if you so wished.
The below script makes the assumption that your "CIEB_Group3" stays in the same place (one cd .. up, then one cd down again) and that the text file remains as "server.txt" and only contains one entry.
Set WshShell = CreateObject("WScript.Shell")
'strpath = script execution directory
strPath = WshShell.CurrentDirectory
strPathReverse = StrReverse(strpath)
'create the parent folder filepath, equivalent to cd..
strParentFilePath = Left(strPath, Len(StrPath) - InStr(strPathReverse,"\"))
'open the text file, making the assumption it is in CIEB_Group3 and is called server.txt
Set objfso = CreateObject("Scripting.FileSystemObject")
Set textfile = objfso.OpenTextFile (strParentFilePath & "\CIEB_Group3\Server.txt", 1)
'read the text file first line into a variable
strNetworkShare = textfile.ReadLine
'map the drive, chr(32) = <space>, chr(34) = "
WshShell.Run "net use K:" & Chr(32) & Chr(34) & strNetworkShare & Chr(34) & Chr(32) & "/pers:yes", 1, 1
Alternatively, if you still would like to use the single CMD line - you may wish to try expanding the environment variable outside the CMD command, as per below.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd /k cd.. & cd CIEB_Group3 & set /p RootServer=<Server.txt & net use K: " & wshShell.ExpandEnvironmentStrings("%RootServer%") & " /pers:yes", 1, True

Run a vbs script from another vbs script and redirect right away output to a file

Running a vbscript from another vbscript, Is it possible to get right away an output to a file like this one:
dim shell
set shell=createobject("wscript.shell")
strCMD =replace("'myvbs.vbs' '"&a_parameter&"' ","'","""")
shell.Run strCMD >output.txt
Thank in advance
Yes, you can. Try this example.
'script1.vbs -----------------
Dim oShell, strCMD
Set oShell = CreateObject("WScript.Shell")
strCMD = Replace("CMD /C CScript.exe 'script2.vbs' " & 3, "'", """")
oShell.Run strCMD & " //NoLogo >output.txt", 0, True
'script2.vbs -----------------
result = WScript.Arguments(0) ^ 2
WScript.StdOut.Write result
Take a look at this answer too.

Prevent VBscript app from showing Console Window

I have a VBScript app which create Symbolic Links.
Set wshell = CreateObject("WScript.Shell")
.....
linkcmd = "mklink /D """ & linkFolderPath & "\" & linkName & """ """ & libfolder & "\" & folderName & """"
cmd = "cmd /C " & linkcmd
wshell.Run cmd, 4, true
This is fine and works, but when I create a lot of links, each execution of the wshell.Run command results in a transient console window appearing and promptly vanishing.
Is there anyway to prevent the console window from being created so visibly?
You can use this VBScript to run cmd commands hidden, just incorporate it into your script:
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cmd /c yourcommands", 0, True

vb script: Trying to open a command prompt, navigate to a directory and run a command

I need to write a script that looks in a directory, finds the latest .zip file (there are .zip and .log in there) then opens a command prompt in a different directory and runs the following command:
loaddb.bat -Dlc.file="C:\Program Files\XyEnterprise\SDL LiveContent\data_old\export\<name of the newest file.zip>" -Dlc.pswd=<oor password> RESTORE
We can not install any languages so it has to be able to run on a Windows 2003 & 2008 server so I chose vbscript...
I have everything working apart from the running the command and can't seem to crack it.
My code is as follows:
Dim fileNewest
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder("C:\Program Files\XyEnterprise\SDL LiveContent\data\export")
For Each aFile In oFolder.Files
sExtension = fso.GetExtensionName(aFile.Name)
If sExtension = "log" Then
'Msgbox "The file extension is a " & sExtension
Else
'Msgbox "The file extension is a " & sExtension
If fileNewest = "" Then
Set fileNewest = aFile
Else
If fileNewest.DateCreated < aFile.DateCreated Then
Set fileNewest = aFile
End If
End If
End If
Next
Msgbox "The Newest File in the folder is " & fileNewest.Name & chr(13) & "Size: " & fileNewest.Size & " bytes" & chr(13) & "Was last modified on " & FileNewest.DateLastModified
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /k c: & cd ../../../Program Files\XyEnterprise\SDL LiveContent\data\export"
How can I now run a command after opening that Dos prompt?
Thanks,
EDIT:
Adding the answer worked with a lot of help from Alex K:
objShell.Run "%comspec% /k c: & cd ""C:\Program Files (x86)\XyEnterprise\SDL LiveContent\"" & """"loaddb RESTORE -Dlc.file=C:\PROGRA~2\XYENTE~1\SDLLIV~1\data\Import\" & fileNewest.Name & " -Dlc.pswd=N2kAs72z"""""
You need to quote that path as it contains spaces;
objShell.Run "%comspec% /k c: & cd ""../../../Program Files\XyEnterprise\SDL LiveContent\data\export"""

VBS Script - Run series of .batch jobs

Help me run a series of .bat script
they are located like so:
p:\Co-Brand\export.bat
p:\Generic\export.bat
p:\Tri-Brand\export.bat
Thanks in advance,
Best regards,
Joe
Would a simple shell command do? You can call this from a command prompt:
for /R %F in (*.bat) do "%F"
or the following from a .bat file:
for /R %%F in (*.bat) do call "%%F"
found a way that works, should have tried this first of all.
I am a bit embarrassed that it was this easy actually:
cd P:\Co-Brand\
CALL Export.bat
cd P:\Generic\
CALL Export.bat
cd P:\TriBrand\
CALL Export.bat
cd P:\UBA\
CALL Export.bat
As originally asked, here is a VBScript solution...
The problem described is probably related to the "Script-Working-Directory".
Try this ...
Dim objShell
Dim blnWaitOnReturn
Dim strOriginalCD
Dim strCmd
Dim intWindowStyle
Dim intExitCode
Set objShell = WScript.CreateObject("Wscript.Shell")
'' if necessary, save the original "Script-Working-Directory"
strOriginalCD = objShell.CurrentDirectory
intWindowStyle = 1
blnWaitOnReturn = True
objShell.CurrentDirectory = "p:\Co-Brand\"
strCmd = "%comspec% /K export.bat"
intExitCode = objShell.Run(strCmd, intWindowStyle, blnWaitOnReturn)
objShell.CurrentDirectory = "p:\Generic\"
strCmd = "%comspec% /K export.bat"
intExitCode = objShell.Run(strCmd, intWindowStyle, blnWaitOnReturn)
objShell.CurrentDirectory = "p:\Tri-Brand\"
strCmd = "%comspec% /K export.bat"
intExitCode = objShell.Run(strCmd, intWindowStyle, blnWaitOnReturn)
'' if necessary, restore the original "Script-Working-Directory"
objShell.CurrentDirectory = strOriginalCD
Notes:
'' If filename contains spaces make sure to add double-quotes around filename
strCmd = "%comspec% /K " & Chr(34) & "File name with spaces.bat" & Chr(34)
'' To run the commands in a "Hidden" window, use:
intWindowStyle = 0
'' To run the commands "Minimized", use:
intWindowStyle = 7
More info on "objShell.Run" can be found here: http://ss64.com/vb/run.html
The above examples will cause VBScript to wait for each called ".bat" to complete and return an "ExitCode" before proceeding.
If you don't want VBScript to wait for one ".bat" to complete before proceeding to the next then set blnWaitOnReturn = False, and remove intExitCode like:
...
blnWaitOnReturn = False
objShell.CurrentDirectory = "p:\Co-Brand\"
strCmd = "%comspec% /K export.bat"
objShell.Run strCmd, intWindowStyle, blnWaitOnReturn
objShell.CurrentDirectory = "p:\Generic\"
strCmd = "%comspec% /K export.bat"
objShell.Run strCmd, intWindowStyle, blnWaitOnReturn
objShell.CurrentDirectory = "p:\Tri-Brand\"
strCmd = "%comspec% /K export.bat"
objShell.Run strCmd, intWindowStyle, blnWaitOnReturn
...
If you want the ability to get the "Status" and "ProcessID", and access the standard streams of the executable to read/write to the process's stdout/stderr in real-time while the process executes, then use "objShell.Exec".
More info on "objShell.Exec" can be found here: http://ss64.com/vb/exec.html

Resources