Archive | VBScript

Simple vbscript (HTA) to install fonts via SCCM

We have a group of users that need the ability to install fonts (.ttf and .otf). They are not administrators for their machines, so we usually go down there and install the fonts using runas. Since advertised SCCM programs can run as system, I can write a script to copy the fonts into the fonts directory. If I mark the package as allow user to intereact and run as administrator, the script will pop for the user to pick the fonts they want to install. Here is my hta code that runs once a button is clicked:

	Set objShell = CreateObject("Shell.Application")
	Set objFolder = objShell.BrowseForFolder (0, "Install Fonts From (Source):", (0))
	If objFolder Is Nothing Then
		window.close
	Else
		Set objFolderItem = objFolder.Self
		objPath = objFolderItem.Path
	End If

	Set objFso = CreateObject("Scripting.FileSystemObject")
	Set objFolder = objFso.GetFolder(objPath)
	bolGotFonts = False
	For each objFile in objFolder.Files
		If objFolder.Files.Count > 0 Then
		  If lcase(objFso.GetExtensionName(objFile.Path))="ttf" OR lcase(objFso.GetExtensionName(objFile.Path))="otf" then
			bolGotFonts = True
			DataArea.InnerHTML = DataArea.InnerHTML & "<input type=""checkbox"" name=""" & objFile.Path & """>" & objFile.Path & "</input><br/>"
		  End if
		End If
	Next
	if bolGotFonts Then DataArea.InnerHTML = DataArea.InnerHTML & "<br/><input id=runbutton  class=""button"" type=""button"" value=""Install Font"" name=""run_button""

This code will popup a browse dialog and put the filenames found in the select directory into the HTA’s DataArea.innerHTML (DataArea is just a <div>) with a checkbox and button to initiate the copy of the files:

SUB InstallFont
    DIM colChkElem, strDriveName, objChkBox
    SET colChkElem = window.document.getElementsByTagName("input")
    FOR EACH objChkBox IN colChkElem
        IF objChkBox.Type = "checkbox" THEN
            IF objChkBox.checked THEN
                strFileName = objChkBox.name
                Set objShell = CreateObject("Shell.Application")
                Set objFolder = objShell.Namespace(FONTS)
                objFolder.CopyHere strFileName
            END IF
        END IF
    NEXT
    DataArea.InnerHTML = ""
END SUB

Seems to work!

Waking up a SCCM collection from vbscript.

I wanted to wake up all the machines in a collection using a vbscript. I know that SCCM has this built in, but I could not get it working. To troubleshoot I figured I would write a script to get collection members, and then wake them via the command line with this tool: http://www.gammadyne.com/cmdline.htm#wol

GetCollectionMembers "XXX00018"

Sub GetCollectionMembers (COLLECTION_NAME)
  Set objLocation = CreateObject("WbemScripting.SWbemLocator")
  Set objService = objLocation.ConnectServer("SERVERNAME", "root\SMS\site_XXX")
  strQuery = "SELECT * FROM SMS_FullCollectionMembership WHERE CollectionID = '" & COLLECTION_NAME & "'"
  Set objSourceCollectionMembers = objService.ExecQuery(strQuery)
  For Each Resource In objSourceCollectionMembers
	WakeMachine objService,Resource.ResourceID
  Next
End Sub

Sub WakeMachine (objService,ResourceID)
  Set Machines = objService.ExecQuery("Select * From SMS_R_System where ResourceID =" & ResourceID)
  For Each Machine In Machines
	Set objShell = CreateObject("Wscript.Shell")
	strCurrentDir = Replace(WScript.ScriptFullName,WScript.ScriptName,"")
	strCommand = strCurrentDir & "\wol.exe " & Replace(Machine.MACAddresses(0),":","")
	Set objExecObject = objShell.Exec(strCommand)
  Next
End Sub

Detach PSTs

We are looking to get rid of .pst files in our environment and we wanted to remove pst files from peoples machines. This link shows how to prevent adding new psts and how to turn off auto archiving via GPO, but what about existing .psts in peoples Outlook profiles?

I put together this vbscript:

Set objFSO = CreateObject( "Scripting.FileSystemObject" )
Set objOutlook = CreateObject( "Outlook.Application" )
Set objNamespace =objOutlook.GetNameSpace( "MAPI" )

For i = objOutlook.Session.Folders.Count To 1 Step -1
    strName = objOutlook.Session.Folders.Item(i).Name
    if instr(strName,"Mailbox -") > 0 or strName = "Public Folders" or strName = "SharePoint Lists" or strName = "Microsoft Dynamics CRM" then
       'wscript.echo "not removing:" &amp; objOutlook.Session.Folders.Item(i).Name
    Else
       'wscript.echo "removing:" & objOutlook.Session.Folders.Item(i).Name
       objOutlook.Session.RemoveStore(objOutlook.Session.Folders.Item(i))
    End If
next

Uninstall old Java Version via vbscript

Here is my current script from removing previous versions of java via VBScript

'  FILENAME: UninstallAllOldJava.vbs
'  AUTHOR: jbmurphy
'  SYNOPSIS: This script looks for older versions of Java and removes them
'  DESCRIPTION: Searches add remove programs for J2SE or Java and removes if not current version
'  NOTES: - Must edit strCurrentVersion to match the version you want to keep
'	- if called with a computer name will, run against remote machine
'	- logs to local path defined in strLogPath
'	- assumes admin priv
'  LINKS:
'  EXAMPLE: UninstallAllOldJava.vbs
'  EXAMPLE: UninstallAllOldJava.vbs \\workststion
'  INPUTS: \\workststion (optional)
'  RETURNVALUE: logs to value in strLogPath
'  ChangeLog:
'  	2009-10-27: jbmurphy-changes made

'On Error Resume Next
Option Explicit
DIM objFSO, strComputer, strCurrentVersion, objWMIService, colInstalledVersions
DIM objVersion, strLogPath, strLogName, strExecQuery

IF WScript.Arguments.Count > 0 then
    strComputer = replace(WScript.Arguments(0),"\\","")
ELSE
    strComputer = "."
END If

strLogPath = "%TEMP%"
strLogName = "Java_Uninstall.log"

strCurrentVersion = "Java(TM) 6 Update 15"
strExecQuery = "Select * from Win32_Product Where Name LIKE '%Java 2 Runtime Environment%' OR Name LIKE '%J2SE Runtime Environment%' OR Name LIKE '%Java(TM)%'"
KillProc

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledVersions = objWMIService.ExecQuery (strExecQuery)

LogIt String(120, "_")
LogIt String(120, "¯")
For Each objVersion in colInstalledVersions
    If objVersion.Name = strCurrentVersion then
       LogIt Now() & ": " &replace(strComputer,".","localhost") & ": Current version is installed: " & objVersion.Name & ":" & objVersion.IdentifyingNumber
    else
       LogIt Now() & ": " &replace(strComputer,".","localhost") & ": Uninstalling: " & objVersion.Name  & ":" & objVersion.IdentifyingNumber
       objVersion.Uninstall()
    end if
Next
LogIt String(120, "_")
LogIt String(120, "¯")
LogIt String(120, " ")

Sub LogIt (strLineToWrite)
    'wscript.echo strLineToWrite
    DIM ts
    If Not objFSO.FolderExists(strLogPath) Then MakeDir(strLogPath)
    Set ts = objFSO.OpenTextFile(strLogPath & strLogName, 8, True)
    ts.WriteLine strLineToWrite
    ts.close
End Sub

Function MakeDir (strPath)
	Dim strParentPath
	On Error Resume Next
	strParentPath = objFSO.GetParentFolderName(strPath)

  If Not objFSO.FolderExists(strParentPath) Then MakeDir strParentPath
	If Not objFSO.FolderExists(strPath) Then objFSO.CreateFolder strPath
	On Error Goto 0
  MakeDir = objFSO.FolderExists(strPath)
End Function

Sub KillProc()
   '# kills jusched.exe and jqs.exe if they are running.  These processes will cause the installer to fail.
   Dim wshShell
   Set wshShell = CreateObject("WScript.Shell")
   wshShell.Run "Taskkill /F /IM jusched.exe /T", 0, True
   wshShell.Run "Taskkill /F /IM jqs.exe /T", 0, True
End Sub