Thursday 26 April 2018

VB Script: Apply permission for folder using ICACLS

For some cases we need to give full control permission for Users to the INSTALLDIR to make the application work without any issues.

NOTE: Initially tried with CACLS but this not applying permission when executing in system context, but the same worked when running the script as normal administrator. so used ICACLS in below script to apply permission to users through system context.

Script:
Option Explicit
Dim strHomeFolder,WshShell,SysRoot,HomeFolder
Dim intRunError,objFSO,win,exepath,file

Set WshShell = CreateObject("Wscript.Shell")
SysRoot = WshShell.ExpandEnvironmentStrings("%SystemDrive%")
win = WshShell.ExpandEnvironmentStrings("%windir%")
Set objFSO = CreateObject("Scripting.FileSystemObject")

HomeFolder = WshShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%")

strHomeFolder = Chr(34) & HomeFolder & "\Java" & Chr(34)

exepath=win & "\system32\icacls.exe"
file= chr(34) & exepath & chr(34) & Chr(32) & strHomeFolder & " /Q /C /T /grant Users:(OI)(CI)F"
intRunError = WshShell.Run(file,0,True)
MsgBox "Exit code:" & intRunError


Before Applying permission


After applied permission using script.

No comments:

Post a Comment