Thursday 26 April 2018

VB Script: UnZip a compressed folder using script.

Have faced issues on deploying an application through SCCM, Install source has many sub-directories within it and getting long file path name error while copying to Distribution point from file-share.
so zipped the whole source and written a script to unzip the install file,Directories before starting the installation using script.

Below are the sample script to UnZip a zipped folder.

Script:
Option Explicit
Dim input,oshell,scriptpath
Dim intOptions, objShell, objSource, objTarget
Set oShell = WScript.CreateObject("WScript.Shell")
ScriptPath = Left(WScript.ScriptFullName, InstrRev(WScript.ScriptFullName, "\"))
oShell.CurrentDirectory = ScriptPath

Input = InputBox("Enter your Zip file name","UnZipping Folder!!!")
Extract Scriptpath & input &".zip", scriptpath

Sub Extract( ByVal myZipFile, ByVal myTargetDir )
    Set objShell = CreateObject( "Shell.Application" )
    Set objSource = objShell.NameSpace( myZipFile ).Items( )
    Set objTarget = objShell.NameSpace( myTargetDir )
    intOptions = 20
    objTarget.CopyHere objSource, intOptions
   
    Set objSource = Nothing
    Set objTarget = Nothing
    Set objShell  = Nothing

End Sub

Steps to DO:
1. Copy code and save the script as XXX.vbs
2. Run the script as Administrator.
3. Enter the Zip file Name without extension.

4. Click Ok, it will UnZip the folder on your script directory.

Note: Keep the Zipped folder along with your script to run without errors.

VB Script: Kill multiple processes

Sometimes we may need to kill the running process to proceed for uninstallation of an application without any issues.

Script:
Option Explicit
'On Error Resume Next
Dim objShell, X, strProcesses

Set objShell = CreateObject("WScript.Shell")

strProcesses = Array(_
"notepad.exe",_
"calc.exe",_
"winword.exe",_
"excel.exe")

For X = 0 to Ubound(strProcesses)
objShell.Run "%COMSPEC% /c TASKKILL.EXE /F /IM """ & strProcesses(X) & """ /T ", 0, True
Next

Set objShell = Nothing


NOTE: For examples, used Notepad,Calculator,Word and Excel process. replace the process name as per your needs in the array.

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.