Skip to content

Instantly share code, notes, and snippets.

@stevenriggs
Created March 22, 2015 01:13
Show Gist options
  • Select an option

  • Save stevenriggs/de566e1518ce6c43402b to your computer and use it in GitHub Desktop.

Select an option

Save stevenriggs/de566e1518ce6c43402b to your computer and use it in GitHub Desktop.
Skeleton script to build a Windows application deployment for use with SCCM, Altiris, etc.
'base version:6
'DESCRIPTION OF TASKS
'AUTHOR
'DATE CREATED
'To run this silently:
'cscript.exe install_APPNAME.vbs /q
'Deal with command line arguments
Dim IsSilent
IsSilent = "NO"
Dim command_line_args
Set command_line_args = wscript.Arguments
If command_line_args.count > 0 Then
Select Case (command_line_args(0))
Case "/?"
WScript.Echo "Use /Q to run this script silently"
WScript.Quit
Case "/Q"
IsSilent = "YES"
Case "/q"
IsSilent = "YES"
Case Else
WScript.Echo "Invalid switch..."
WScript.Echo "Use /Q to run this script silently"
WScript.Quit
End Select
End If
Dim InstallStatus
Dim RegKeyString
Dim TheUninstallString
Dim FSO
Dim WshShell
Dim oEnv
Dim ProgramName
Dim QuitOnFail
Dim CommandToRun
Dim Action 'Probabaly Install or Uninstall
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
'disable open file security warning XP SP2 and better
'http://support.microsoft.com/kb/889815
set oEnv = WshShell.Environment("PROCESS")
oEnv("SEE_MASK_NOZONECHECKS") = 1
'***** BEGIN PLACE CODE HERE *****
'***** END PLACE CODE HERE *****
'Re-enable open file security warning
oEnv.Remove("SEE_MASK_NOZONECHECKS")
If IsSilent = "NO" Then
WScript.Echo "Script run is complete..."
End If
'THE END
Function QuitIfAppIsInAddRemovePrograms (UninstallKey, ProgramName)
If RegKeyExists ("HKLM", "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" & UninstallKey, "DisplayName") Then
'http://msdn.microsoft.com/en-us/library/aa372835(v=vs.85).aspx
If IsSilent = "NO" Then
WScript.Echo "1604: The product " & ProgramName & " is already installed, and has prevented the installation of this product."
End If
Wscript.Quit(1604)
End If
If RegKeyExists ("HKLM", "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & UninstallKey, "DisplayName") Then
'http://msdn.microsoft.com/en-us/library/aa372835(v=vs.85).aspx
If IsSilent = "NO" Then
WScript.Echo "1604: The product " & ProgramName & " is already installed, and has prevented the installation of this product."
End If
Wscript.Quit(1604)
End If
End Function
Function IsInAddRemovePrograms (UninstallKey, ProgramName)
IsInAddRemovePrograms = "False"
If RegKeyExists ("HKLM", "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" & UninstallKey, "DisplayName") Then
'http://msdn.microsoft.com/en-us/library/aa372835(v=vs.85).aspx
If IsSilent = "NO" Then
'WScript.Echo "The product " & ProgramName & " is already installed."
End If
IsInAddRemovePrograms = "True"
End If
If RegKeyExists ("HKLM", "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & UninstallKey, "DisplayName") Then
'http://msdn.microsoft.com/en-us/library/aa372835(v=vs.85).aspx
If IsSilent = "NO" Then
'WScript.Echo "The product " & ProgramName & " is already installed."
End If
IsInAddRemovePrograms = "True"
End If
End Function
Function GetUninstallStringFromRegistry (UninstallKey, ProgramName)
If RegKeyExists ("HKLM", "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" & UninstallKey, "UninstallString") Then
GetUninstallStringFromRegistry = RegKeyString
End If
If RegKeyExists ("HKLM", "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & UninstallKey, "UninstallString") Then
GetUninstallStringFromRegistry = RegKeyString
End If
If TheUninstallString = "" Then
If IsSilent = "NO" Then
WScript.Echo "Couldn't get uninstall string. " & ProgramName & " is not installed."
End If
End If
End Function
Function QuitIfAppIsNotInAddRemovePrograms (UninstallKey, ProgramName)
TheUninstallString = ""
If RegKeyExists ("HKLM", "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" & UninstallKey, "UninstallString") Then
TheUninstallString = RegKeyString
End If
If RegKeyExists ("HKLM", "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & UninstallKey, "UninstallString") Then
TheUninstallString = RegKeyString
End If
If TheUninstallString = "" Then
If IsSilent = "NO" Then
WScript.Echo "1608: " & ProgramName & " is not installed."
End If
WScript.Quit(1608)
End If
End Function
Function QuitIfFileExists (FileToFind, ProgramName)
If FSO.FileExists(FileToFind) Then
'http://msdn.microsoft.com/en-us/library/aa372835(v=vs.85).aspx
If IsSilent = "NO" Then
WScript.Echo "1604: The product " & ProgramName & " is already installed, and has prevented the installation of this product."
End If
Wscript.Quit(1604)
End If
End Function
Function QuitIfFileDoesntExist (FileToFind, ProgramName)
If FSO.FileExists(FileToFind) Then
'the file is there, do nothing
Else
If IsSilent = "NO" Then
WScript.Echo "1608: " & ProgramName & " is not installed."
End If
WScript.Quit(1608)
End If
End Function
'syntax: Call RunCommandForProductAndQuitOnFail ("COMMAND", "ProgramName", "YES OR NO")
Function RunCommandForProductAndQuitOnFail(Command, ProgramName, QuitOnFail)
'WScript.Echo Command
InstallStatus = WshShell.Run (Command,0,true)
If QuitOnFail = "YES" Then
Call CheckStatus(InstallStatus)
End If
If IsSilent = "NO" Then
WScript.Echo ProgramName & " " & Action & " finished with exit code: " & InstallStatus
End If
End Function
'Exits the script if the command fails.
Function CheckStatus(InstallStatus)
'Wscript.Echo InstallStatus
If (InstallStatus <> 0 AND InstallStatus <> 1641 AND InstallStatus <> 3010) Then
If IsSilent = "NO" Then
WScript.Echo ProgramName & " " & Action & " FAILED with exit code: " & InstallStatus
End If
Wscript.Quit(InstallStatus)
End If
End Function
'syntax: If RegKeyExists ("HIVE", "PATH", "KEY") Then
Function RegKeyExists(nHive, strPath, strValueName)
Select Case Left(nHive, 20)
Case "HKCR", "HKEY_CLASSES_ROOT"
nHive = &H80000000
Case "HKCU", "HKEY_CURRENT_USER"
nHive = &H80000001
Case "HKLM", "HKEY_LOCAL_MACHINE"
nHive = &H80000002
Case "HKU", "HKEY_USERS"
nHive = &H80000003
Case "HKCC", "HKEY_CURRENT_CONFIG"
nHive = &H80000005
Case Else
WScript.Echo "Hive Not Supported."
WScript.Quit
End Select
Dim objRegistry
Dim strComputer, strValue
strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
objRegistry.GetStringValue nHive, strPath, strValueName, strValue
RegKeyExists = Not IsNull(strValue)
RegKeyExists = CStr (RegKeyExists)
RegKeyString = strValue
End Function
Function QuitIfOSEquals (OSName, ProgramName)
If GetWindowsOS = OSName Then
If IsSilent = "NO" Then
WScript.Echo "This program cannot be installed on " & OSName & "."
End If
WScript.Quit(0)
End If
End Function
Function GetWindowsOS
Dim objWMI, objItem, colItems
Dim strComputer, VerBig
strComputer = "."
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem in colItems
VerBig = Left(objItem.Version,3)
Next
Select Case VerBig
Case "6.2" GetWindowsOS = "Windows Server 2008"
Case "6.1" GetWindowsOS = "Windows 7"
Case "6.0" GetWindowsOS = "Windows Vista"
Case "5.2" GetWindowsOS = "Windows Server 2003"
Case "5.1" GetWindowsOS = "Windows XP"
Case "5.0" GetWindowsOS = "Windows 2000"
Case "4.0" GetWindowsOS = "Windows NT"
Case Else GetWindowsOS = "Unknown"
End Select
End Function
Function GetOSArchitecture
GetOSArchitecture=WshShell.Environment("System").Item("PROCESSOR_ARCHITECTURE")
End Function
'Get Application Path
'Return: Script Application Path without script name
Function GetAppPath
Dim s
s = WScript.ScriptFullName
GetAppPath = UCase(Left(s, (Len(s) - Len(WScript.ScriptName)-1)))
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment