Thursday, 23 July 2009

Set Working Directory of Shortcut

Set objShell = WScript.CreateObject("WScript.Shell")
Set objShtCut = objShell.Createshortcut("D:\Documents and Settings\All Users\Start Menu\Programs\MKS Toolkit\Evaluation Guide\For Systems Administrators\Desktop Shortcuts.lnk")
objShtCut.WorkingDirectory = "C:\PROGRA~1\MKSTOO~1\Demonstrations\bin"
'WScript.Echo objShtCut.WorkingDirectory

Stop and Delete Service - VBScript

Option Explicit
On Error Resume Next

Dim objWshShell
Set objWshShell = CreateObject("WScript.Shell")

objWshShell.Run "sc stop Nicelog",0,true
objWshShell.Run "sc delete Nicelog",0,true

Set objWshShell = Nothing

Add Registry of Type REG_NONE - VBScript

Dim objWshShell

Set objWshShell=CreateObject("WScript.Shell")

WINDIR =objWshShell.ExpandEnvironmentStrings("%windir%")
objWshShell.Run "reg add HKCU\Software\Google\Google /v hello /t REG_NONE",0

Set objWshShell=Nothing

Create Binary Registry - VBSCript

const HKEY_CURRENT_USER = &H80000001
strKeyPath = "Software\Google\Google Earth Plus"
strComputer = "."
iValues = Array()
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
BinaryValueName = "FailureActions"
oReg.SetBinaryValue HKEY_CURRENT_USER,strKeyPath, BinaryValueName,iValues

Creating STRING and DWORD Values - VBScript

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set StdOut = WScript.StdOut

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\System Admin Scripting Guide"
strValueName = "String Value Name"
strValue = "string value"
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

strValueName = "DWORD Value Name"
dwValue = 82
oReg.SetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,dwValue

Check Service - VBScript

On Error resume next
Dim StrComputer: strComputer = "."
Dim oservice
Dim oWMIService : Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Dim colServiceList: Set colServiceList = oWMIService.ExecQuery("Select * from Win32_Service where Name='SharedAccess'")

For Each oService in colServiceList

if oService.startmode="Manual" or oService.startmode="Disabled" then
end if

if oservice.State="Stopped" then
end if


Stop and Start Service - VBScript

Option Explicit
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, intSleep
strComputer = "."
intSleep = 15000
WScript.Echo " Click OK, then wait " & intSleep & " milliseconds"

'On Error Resume Next
'NB strService is case sensitive

strService = " 'Alerter' "
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name ="_
& strService & " ")
For Each objService in colListOfServices
WSCript.Sleep intSleep
WScript.Echo "Your "& strService & " service has Started"
' End of Example WMI script to Start / Stop services

Delete Registry if its Empty - VBScript

On Error Resume next
Const HKEY_LOCAL_MACHINE = &H80000002
Set wsh1=createobject("")

strKeyRoot = "SOFTWARE\Microsoft\Microsoft SQL Server"
strKeyRoot1 = "SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent"
strKeyRoot2 = "SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib\VIA"
strKeyRoot3 = "SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib"
strKeyRoot4 = "SOFTWARE\Microsoft\MSSQLServer\Client"
strKeyRoot5 = "SOFTWARE\Microsoft\MSSQLServer"
strKeyRoot6 = "SOFTWARE\Microsoft\SQL Redist\Setup"
strKeyRoot7 = "SOFTWARE\Microsoft\SQL Redist\1.00.000"
strKeyRoot8 = "SOFTWARE\Microsoft\SQL Redist"
strKeyRoot9 = "SOFTWARE\Microsoft\dasetup"
Set oReg=GetObject("winmgmts:root\default:StdRegProv")

oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\"
End If

oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot1, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent\"
End If

oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot2, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib\VIA\"
End If

oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot3, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib\"
End If

oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot4, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\"
End If

oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot5, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\MSSQLServer\"
End If

oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot6, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\SQL Redist\Setup\"
End If

oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot7, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\SQL Redist\1.00.000\"
End If

oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot8, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\SQL Redist\"
End If

oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyRoot9, arrSubKeys
If IsNull(arrSubKeys) Then
wsh1.RegDelete "HKLM\SOFTWARE\Microsoft\dasetup\"
End If


Kill Process -VBScript

Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strProcessKill = "'crvw.exe'"
Set objWMIService = GetObject("winmgmts:root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess

Script to Shutdown the system - VBScript

strComputer = "."
Set objWMIService = GetObject_
("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & _
strComputer & "\root\cimv2")

Set colOperating Systems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems

Wednesday, 15 April 2009

Find Serial Key of OS intalled - VBScript

Set SNSet = GetObject("winmgmts:").InstancesOf ("win32_OperatingSystem")

For each SN in SNSet
MsgBox "The serial number for the installed OS is: " & SN.SerialNumber

Wednesday, 28 January 2009

Search for Script Path and copy files to Destination - VBScript

The below script will search for the directory in which the script file is placed and will copy the files from the path in script to the destination path in script

Dim fso,l_path,oWsh,l_prgfiles
Set fso = CreateObject("Scripting.FileSystemObject")
Set oWsh = CreateObject("Wscript.Shell")
l_prgfiles = oWsh.ExpandEnvironmentStrings("%programfiles%") & "\"
l_source = fso.GetParentFolderName(wscript.ScriptFullName) & "\bo6data"
fso.CopyFolder l_source,l_prgfiles,true

Adding Registries using VBScript

Option Explicit
const HKEY_LOCAL_MACHINE = &H80000002
const HKEY_CLASSES_ROOT = &H80000000

Dim objReg, Return
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!"& "\root\default:StdRegProv")

Return = objReg.CreateKey(HKEY_CLASSES_ROOT,"Interface\{000C0300-0000-0000-C000-000000000046}")
Return = objReg.SetStringValue(HKEY_CLASSES_ROOT,"Interface\{000C0300-0000-0000-C000-000000000046}","","_IMsoDispObj")
Return = objReg.CreateKey(HKEY_CLASSES_ROOT,"Interface\{000C0300-0000-0000-C000-000000000046}\ProxyStubClsid")
Return = objReg.SetStringValue(HKEY_CLASSES_ROOT,"Interface\{000C0300-0000-0000-C000-000000000046}\ProxyStubClsid","","{00020424-0000-0000-C000-000000000046}")
Return = objReg.CreateKey(HKEY_CLASSES_ROOT,"Interface\{4EF6100A-AF88-11D0-9846-00C04FC29993}\ProxyStubClsid32")
Return = objReg.SetStringValue(HKEY_CLASSES_ROOT,"Interface\{000C0300-0000-0000-C000-000000000046}\ProxyStubClsid32","","{00020424-0000-0000-C000-000000000046}")
Return = objReg.CreateKey(HKEY_CLASSES_ROOT,"Interface\{4EF6100A-AF88-11D0-9846-00C04FC29993}\TypeLib")
Return = objReg.SetStringValue(HKEY_CLASSES_ROOT,"Interface\{000C0300-0000-0000-C000-000000000046}\TypeLib","","{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}")
Return = objReg.SetStringValue(HKEY_CLASSES_ROOT,"Interface\{000C0300-0000-0000-C000-000000000046}\TypeLib","Version","2.3")

Return = objReg.CreateKey(HKEY_CLASSES_ROOT,"Interface\{618736E0-3C3D-11CF-810C-00AA00389B71}")
Return = objReg.SetStringValue(HKEY_CLASSES_ROOT,"Interface\{618736E0-3C3D-11CF-810C-00AA00389B71}","","IAccessible")
Return = objReg.CreateKey(HKEY_CLASSES_ROOT,"Interface\{618736E0-3C3D-11CF-810C-00AA00389B71}\NumMethods")
Return = objReg.SetStringValue(HKEY_CLASSES_ROOT,"Interface\{618736E0-3C3D-11CF-810C-00AA00389B71}\NumMethods","","28")
Return = objReg.CreateKey(HKEY_CLASSES_ROOT,"Interface\{618736E0-3C3D-11CF-810C-00AA00389B71}\ProxyStubClsid")
Return = objReg.SetStringValue(HKEY_CLASSES_ROOT,"Interface\{618736E0-3C3D-11CF-810C-00AA00389B71}\ProxyStubClsid","","{00020424-0000-0000-C000-000000000046}")
Return = objReg.CreateKey(HKEY_CLASSES_ROOT,"Interface\{618736E0-3C3D-11CF-810C-00AA00389B71}\ProxyStubClsid32")
Return = objReg.SetStringValue(HKEY_CLASSES_ROOT,"Interface\{618736E0-3C3D-11CF-810C-00AA00389B71}\ProxyStubClsid32","","{00020424-0000-0000-C000-000000000046}")
Return = objReg.CreateKey(HKEY_CLASSES_ROOT,"Interface\{618736E0-3C3D-11CF-810C-00AA00389B71}\TypeLib")
Return = objReg.SetStringValue(HKEY_CLASSES_ROOT,"Interface\{618736E0-3C3D-11CF-810C-00AA00389B71}\TypeLib","","{1EA4DBF0-3C3B-11CF-810C-00AA00389B71}")
Return = objReg.SetStringValue(HKEY_CLASSES_ROOT,"Interface\{618736E0-3C3D-11CF-810C-00AA00389B71}\TypeLib","Version","1.1")

Set objReg = Nothing

Tuesday, 20 January 2009

Restoring Registry - Value of Default - VBScript

The following script will restore a registry with value under Default

Dim objWshShell

Set objWshShell=CreateObject("WScript.Shell")

WINDIR =objWshShell.ExpandEnvironmentStrings("%windir%")
objWshShell.RegWrite "HKCR\CLSID\{0BE35200-8F91-11CE-9DE3-00AA004BB851}\","Font Property Page","REG_SZ"
objWshShell.RegWrite "HKCR\CLSID\{0BE35200-8F91-11CE-9DE3-00AA004BB851}\InprocServer32\", WINDIR & "\System32\MFC42u.DLL","REG_SZ"

Set objWshShell=Nothing

Friday, 16 January 2009

Delete files of common naming using VBScript

Suppose cr123.tmp, crequ.tmp, crkkin.tmp...........etc files are present under C:\Windows folder.

The following script can be used to delete cr*.tmp files from C:\Windows folder

strDir = "C:\WINDOWS"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objDir = FSO.GetFolder(strDir)
Sub getInfo(pCurrentDir)

For Each aItem In pCurrentDir.Files
If (LCase(Left(Cstr(aItem.Name), 2)) = "cr") AND fso.GetExtensionName(LCase(aItem.Name)) = "tmp" Then
End If
For Each aItem In pCurrentDir.SubFolders
'wscript.Echo aItem.Name & " passing recursively"
End Sub

Sunday, 11 January 2009

MSI Errors List

  • 1620: This installation package could not be opened. Contact the application vendor to verify that this is a valid Windows Installer package.
  • 1911: Could not register type library for file [2]. Contact your support personnel.
  • 1601: The Windows Installer service could not be accessed. Contact your support personnel to verify that the Windows Installer service is properly registered.
  • 1605: This action is only valid for products that are currently installed.
  • 1601: The Windows Installer Service Could Not Be Accessed
  • 1155: File [1] not found
  • 1325: ''[2]'' is not a valid short file name.
  • 2303: Error getting volume info. GetLastError: [2].
  • 2717: Bad action condition or error calling custom action ''[2]''.
  • 1613: This installation package cannot be installed by the Windows Installer service. You must install a Windows service pack that contains a newer version of the Windows Installer service.
  • 1603: A fatal error occurred during installation.
  • 1706: No valid source could be found for product [2].
  • 1316: A network error occurred while attempting to read from the file: [2]
  • 1723: There is a problem with this Windows Installer package. A DLL required for this install to complete could not be run. Contact your support personnel or package vendor. Action [2], entry: [3], library: [4]
  • 2755: Server returned unexpected error [2] attempting to install package [3].
  • 1719: Windows Installer service could not be accessed. Contact your support personnel to verify that it is properly registered and enabled.
  • 1635: This patch package could not be opened. Verify that the patch package exists and is accessible, or contact the application vendor to verify that this is a valid Windows Installer patch package.
  • 1612: The installation source for this product is not available. Verify that the source exists and that you can access it.
  • 1601: The Windows Installer service could not be accessed. Contact your support personnel to verify that the Windows Installer service is properly registered.
  • 1713: [2] cannot install one of its required products. Contact your technical support group. System Error: [3].
  • 1321: The Installer has insufficient privileges to modify this file: [2].

Install MSI from Remote Machine

The following is from Microsoft's "System Administration Scripting Guide". Note that Active Directory is needed for this VB script to work.


Installs a hypothetical software program (using a Windows Installer package) on a remote computer. Requires delegation for the computer and user accounts involved in the procedure.

Script Code


Const wbemImpersonationLevelDelegate = 4
Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objConnection = objwbemLocator.ConnectServer _
("WebServer", "root\cimv2", "fabrikam\administrator", _
"password", , "kerberos:WebServer")
objConnection.Security_.ImpersonationLevel = wbemImpersonationLevelDelegate
Set objSoftware = objConnection.Get("Win32_Product")
errReturn = objSoftware.Install("\\atl-dc-02\scripts\1561_lab.msi",,True)
Wscript.Echo errReturn


MSI Log Files

MSI log files can be incredibly useful for creating silent installations of vendor provided MSI's - for example, an application that has a serial number, or asks various questions, if you do a manual install fully logged (msiexec.exe /i /L*v c:\temp\funkylog.log) and then look in the log for the command line - it will change as the installation progresses but often things that the installation asks the user for are uses as properties, which can be entered in the command line, or in an MST and aid a silent installation.

Local Admin Check - VBScript

Check if the current user is a local administrator using the Windows Installer service.

Dim oInst, oSess, iAdmin, oProducts
Const msiUILEVELNONE = 2
Set oInst = CreateObject("WindowsInstaller.Installer")
oInst.UILevel = msiUILEVELNONE
Set oProducts = oInst.Products
Set oSess = oInst.OpenProduct(oProducts(0))
iAdmin = oSess.Property("AdminUser")
If iAdmin = 1 Then
Msgbox "You are a local administrator!", vbInformation, "ADMINISTRATOR"
Msgbox "You are not a local administrator!", vbCritical, "NON-ADMIN"
End If

Restart for the Setup Capture Process

Many applications come up with a message after installation stating you must now restart the Computer.

Some packagers and Instructors are taught that during the setup capture of an application you MUST restart the Computer to capture any file updates and or registry changes that take place.

This is not always true.

Many times after the installation of an application simply running any and or all shortcuts that are created before doing the End Capture will update or create the Registry keys and files needed for the application package to work properly. Being able to bypass the restart during the setup capture does save time and depending what kind of environment you are packaging in (hopefully a VMware OS only environment) you will not capture any Registry keys or files that are not needed for the application package to function properly.

I have found that doing the steps above works atleast 80% of the time.

All about MSI Re-Packaging

First of all MSI installations are already in a in a distributable format, so there is no benefit to recreating the setup. MSI packages are full of internal references, which cannot be captured and recreated. These internal references are critical to the self-healing functionality of MSI packages and are fundamental to their operation.

Repackaging an MSI may result in uninstall problems and other Windows Installer related problems. Finally, and perhaps most important, when the vendor releases and update or patch (MSP) for the product, it will be designed to update their MSI and not yours. When repackaging, all of the globally unique identifiers (GUIDs) used to identify items in the package are regenerated and will not match up to the updates provided by the software vendor package.

There are no changes that cannot be implemented at install-time using an MST (transform) file. Aside from dictating installation preferences, you can add, delete or change any items you wish. I have had some people challenge the fact that the capabilities of MST negate the need to repackage an MSI. While the statement is certianly a fact- it can admittedly be a very difficult task to carry out when dealing with certian packages. Have you given up on MST and repackaged an MSI? Please rate the applications difficulty and provide comments in the software's Package Knowledge base entry. Fighting with a package right now? The Package Knowledge base may hold some answers for you. Also visit our package development message board.