Post from January, 2006

How To Import a Regfile?

Tuesday, 24. January 2006 13:17

Batch programmers know that reg files can be imported silently with Regedit.exe /S.

This example shows how to implement this in VBScript:

Dim wsh
Set wsh = WScript.CreateObject("WScript.Shell")
wsh.Run "%SystemRoot%\regedit.exe /S regfile.reg", , True

It is advisable to use a Sub-procedure, especially if you have to import more than one regfile.

ImportRegFile "regfile1.reg"
ImportRegFile "regfile2.reg"
ImportRegFile "regfile3.reg"

Sub ImportRegFile(theFile)
	Dim fso, wsh
	Set fso = WScript.CreateObject("Scripting.FileSystemObject")
	If fso.FileExists(theFile) Then
		Set wsh = WScript.CreateObject("WScript.Shell")
		wsh.Run "%SystemRoot%\regedit.exe /S " & theFile, , True
		Set wsh = Nothing
	End If
	Set fso = Nothing
End Sub

Category:Scripting, VBScript | Comment (0) | Author: Frank-Peter

How To Determine The Name Of The Local Administrator Group?

Tuesday, 17. January 2006 13:03

The VBScript below, LocalAdminGroup.vbs, determines the name of the local administrator group by searching its SID in the list of local groups.

' ===================================================================
' Filename : LocalAdminGroup.vbs
' Revision : 1.0
' Datum    : 21.11.2005
' Author   : Frank-Peter Schultze
'
' Determines and shows the name of the local administrators group.
'
' ===================================================================

Option Explicit

Dim WshShell, strName, objWmi, colGroups, objGroup

Set WshShell  = WScript.CreateObject("Wscript.Shell")
strName       = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set objWmi    = GetObject("winmgmts:\\" & strName & "\root\cimv2")
Set colGroups = objWmi.ExecQuery ("Select * From Win32_Group Where Domain = '" _
                                       & strName & "' AND SID = 'S-1-5-32-544'")
For Each objGroup in colGroups
	Wscript.Echo objGroup.Name
Next

See also: Well-known security identifiers in Windows operating systems

Category:Scripting, VBScript | Comment (0) | Author: Frank-Peter

How To Call A VBScript Within A VBScript?

Tuesday, 17. January 2006 11:30

There are two ways of calling a second script (another script file), each with its advantages and disadvantages…

Run method

If you don’t need to share code and/or variables you can call the second script using the Run method.

Set objWsh = WScript.CreateObject("WScript.Shell")
objWsh.Run "cscript.exe ""AnotherVbscript.vbs"" [arguments]",, True

Though you can’t share variables you can use command line arguments in order to transfer information.

Using this method you can call a JScript file too.

ExecuteGlobal statement

You can read the second script into a variable and pass it to ExecuteGlobal. This enables you to share code and variables.

The procedure below loads the second script on the fly and executes it with ExecuteGlobal.

Include "AnotherVbscript.vbs"

Sub Include(strVBScriptFile)

	Dim objFS, objF, strCode

	Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
	Set objF  = objFS.OpenTextFile(strVBScriptFile)
	strCode   = objF.ReadAll
	objF.Close
	Set objFS = Nothing
	ExecuteGlobal strCode

End Sub

Imprtant note:

This method complicates debugging if an error occurs in the second script because you will get the line number of the ExecuteGlobal statement in the first script.

The first and the second script share the same namespace. Therefore, watch for unique variable and procedure naming.

Category:Scripting, VBScript | Comment (0) | Author: Frank-Peter