'-------------------------------------------------------------------------------------- ' logon.vbs ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Date 5-June-2000 '-------------------------------------------------------------------------------------- ' HISTORY ----------------------------------------------------------------------------- ' ' ' 3.01 5 Jun 2001 ' ----------------- ' Enstated second call strategy - Startup Group can change with the policy so is ' set from the profile environment Startup. ' ' ' if logon.ini says to allow post logon processing, and ' if the netlogon share contains the file postlogon.vbs, then ' set it up to run via a the startup group. ' ' Post logon processing is a better place to add and remove desktop shortcuts because ' the desktop is fixed for all O/S's. Removed all support for desktop shortcuts. '-------------------------------------------------------------------------------------- ' DECLARATIONS :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: '-------------------------------------------------------------------------------------- Option Explicit ' Force variable declarations '-------------------------------------------------------------------------------------- ' CONSTANTS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: '-------------------------------------------------------------------------------------- const sVersion = "3.01" ' Script version const nSuccess = 0 ' Success exit const nFailure = 1 ' Failure exit const bWait = TRUE ' Wait for a child to return const bNoWait = FALSE ' Don't wait for a child to return const bForce = TRUE ' Force a file delete and/or overwrite const bNoForce = FALSE ' Don't force a file delete and/or overwrite const bPersist = TRUE ' Persistent connections const bNoPersist = FALSE ' No persistent connections const bOverWrite = TRUE ' Overwrite a file const bNoOverWrite = FALSE ' Don't overwrite a file const nOpenRead = 1 ' Open a file to read const nOpenWrite = 2 ' Open a file to write const nOpenAppend = 8 ' Open a file to append const nHidden = 0 ' Windowstyle is hidden const nNormal = 1 ' Windowstyle is normal const nMinimised = 7 ' Windowstyle is minimised const nMaximised = 3 ' Windowstyle is maximised '-------------------------------------------------------------------------------------- ' GLOBALS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: '-------------------------------------------------------------------------------------- Dim wshShell ' Shell object Dim wshNet ' Handle to the network Dim wshProEnv ' Process Environment Dim wshUsrEnv ' User environment Dim objFileSys ' Handle to the file system Dim objArgs ' Arguments object Dim sIniFile ' Logon.ini filespec Dim aIniFile() ' Dynamic array to hold logon.ini contents Dim nIniFile ' Size of array Dim aGroups() ' Dynamic array to hold logon user groups Dim nGroups ' Size of array Dim sOS ' O/S Dim sDnsDomain ' DNS domain Dim sDomain ' User domain Dim sLogonServer ' Logon server Dim sComputer ' Client computer Dim sUserId ' User logonname Dim sFullName ' User fullname Dim sEmailStub ' User Email name Dim sWinDir ' %windir% Dim hLog ' Log file handle Dim bLog ' Log or not Dim sOldLog ' Old log file Dim sNewLog ' New log file Dim bdebug ' For conditional debug printing '-------------------------------------------------------------------------------------- ' MAINLINE :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: '-------------------------------------------------------------------------------------- set wshShell = WScript.CreateObject("WScript.Shell") ' Create objects set wshNet = WScript.CreateObject("WScript.Network") set wshProEnv = wshShell.Environment("Process") set wshUsrEnv = wshShell.Environment("User") set objFileSys = CreateObject("Scripting.FileSystemObject") set objArgs = WScript.Arguments ForceCScript ' Force CScript sIniFile = SetExtension(WScript.ScriptFullName,"ini") ' Form .INI spec LoadIniFile sIniFile, aIniFile, nIniFile ' Load .INI file bdebug = not (lcase(GetIniKey(aIniFile,"debug control","skipdebug") )="yes") ' Debug or not? bLog = not (lcase(GetIniKey(aIniFile,"log control","skiplog") )="yes") ' Log or not? if (bLog) then sOldLog = GetIniKey(aIniFile,"log control","oldlogfile") ' Old log file if (bLog) then sNewLog = GetIniKey(aIniFile,"log control","newlogfile") ' New log file if (bLog) then OpenLog sOldLog, sNewLog ' Open log if (bLog) then MarkLog "+ Console" ' Console log WScript.echo string(27,vbLF) ' Clear screen Print (" Assumption College " & vbCRLF) ' title sDomain = wshProEnv("UserDomain") ' NETENV Cheat #1 if (sDomain = "") then AbortScript "Domain not found." ' ** Abort Print (" Domain ............ " & sDomain) ' Display domain GetLogonServer sLogonServer ' Get logon server if (sLogonServer = "") then AbortScript "Logon server not found." ' ** Abort Print (" Logon Server ...... " & sLogonServer & vbCRLF) ' Display server Print (" Logon Script ...... " & ScriptName _ & " (" & sVersion & ")" & vbCRLF) ' Display script Print (" Date .............. " & formatdatetime(date,vbLongDate)) ' Display date Print (" Time .............. " & formatdatetime(time,vbShortTime) _ & vbCRLF) ' Display time sComputer = ucase(wshNet.ComputerName) ' Get client if (sComputer = "") then AbortScript "Client not found." ' ** Abort Print (" Workstation ....... " & sComputer) ' Display client sOS = wshProEnv("OS") ' Get %OS% if ((sOS<>"Windows_NT") and (sOS<>"Windows_98")) then ' Accept NT and 98 if (sOS = "") then ' sOS = "Windows_98" ' Default to 98 else ' AbortScript "Unknown OS value of " & sOS & "." ' ** Abort end if ' end if ' Print (" Workstation O/S ... " & sOS & vbCRLF) ' Display OS sWinDir = wshProEnv("windir") ' Get %WINDIR% if (sWinDir = "") then AbortScript "%windir% not found." ' ** Abort sDnsDomain = GetIniKey(aIniFile,"DNS Domain","DNSDomain") ' Get dns domain if (sDnsDomain="") then sDnsDomain = "qed.qld.gov.au" ' Default GetUserNames sUserId, sFullName, sDomain ' Get user names if (sUserId = "") then AbortScript "User not found." ' ** Abort Print (" User ID ........... " & sUserId) ' Display userid Print (" User Name ......... " & sFullName) ' Display fullname sEmailStub = EMailStub(sFullName) ' Get email stub Print (" EMail Name ........ " & sUserId & "@" & sDnsDomain) ' Display name Print (" EMail Alias ....... " & sEmailStub & "@" & sDnsDomain _ & vbCRLF) ' Display alias ExpandIniFileMacros ' Expand macros GetUserGroups sDomain, sUserId, aGroups, nGroups ' Get user groups ProcessConditionalKeys ' Process .INI SynchTime ' Synch time MapDrives ' Map drives SetEnvironment ' Setup environment SetPath ' Setup path if (bLog) then MarkLog "- Console" ' Console log if (bLog) then LogEnvironment ' Log environment if (bLog) then LogIniFile ' Log ini file if (bLog) then LogConnections ' Log connections if (bLog) then CloseLog ' Close log SetupRunOnce sOS, "\\" & sLogonServer & "\netlogon\postlogon.vbs" ' Run postlogon.vbs WScript.quit(nSuccess) ' First call exit '-------------------------------------------------------------------------------------- Private Sub SetupRunOnce( ByVal sOS, ByVal sFileSpec ) '-------------------------------------------------------------------------------------- ' Setup postlogon.vbs to run via a RunOnce registry hack ' Check logon.ini says it's ok to run it if (lcase(GetIniKey(aIniFile,"post logon","skippost"))="yes") then exit sub ' Do nothing if the file's not there if not (objFileSys.FileExists(sFileSpec)) then exit sub end if ' Add key if sOS= "Windows_98" then ForceStartupShortcut "Postlogon.LNK", "cscript.exe", sFileSpec , "C:\", nNormal ' WshShell.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce\RunThis", """" & sFileSpec & """" else ForceStartupShortcut "Postlogon.LNK", "cscript.exe", sFileSpec , "C:\", nNormal ' WshShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce\RunThis", """" & sFileSpec & """" end if end sub ' Ensure desktop shortcuts exist 'ForceDesktopShortcut "Edit Autoexec.Bat.LNK", sWinDir & "\notepad.exe", "c:\autoexec.bat", "C:\", nNormal '-------------------------------------------------------------------------------------- Private Sub LogPath( ByVal sPath, ByVal bOk ) '-------------------------------------------------------------------------------------- Dim bOutput, bWasOutput, p, s bOutput = (lcase(GetIniKey(aIniFile,"output control","showpath"))="yes") bWasOutput=FALSE if (sPath<>"") then p = instr(sPath,";") do while (p>0) s = left(sPath,p-1) sPath = mid(sPath,p+1) if (s<>"") then if (bOk) then if (bOutput) then Print (" Path .............. " & s) else Print ("* Path .............. " & s & " * ERROR *") end if bWasOutput=TRUE end if p = instr(sPath,";") loop end if if (bWasOutput) then Print ("") end sub '-------------------------------------------------------------------------------------- Private Sub SetPath() '-------------------------------------------------------------------------------------- ' Set PATH as per .INI Dim sNewPath, sTstPath, bOk, n if (sOS = "Windows_NT") then sNewPath=lcase(wshUsrEnv("PATH")) else sNewPath=lcase(wshProEnv("PATH")) end if if (nIniFile>0) then for n=0 to nIniFile - 1 step 3 if ((aIniFile(n+0)="path") and (aIniFile(n+1)="extn")) then if (instr(sNewPath,lcase(aIniFile(n+2)))=0) then sNewPath = sNewPath + ";" + lcase(aIniFile(n+2)) end if end if next end if if (sOS = "Windows_NT") then ' wshUsrEnv("PATH") = sNewPath else ' wshShell.Run ScriptHome & "\winset.exe " & "PATH=" & sNewPath,nHidden,bWait end if if (sOS = "Windows_NT") then sTstPath=lcase(wshUsrEnv("PATH")) else sTstPath=lcase(wshProEnv("PATH")) end if if (sTstPath=sNewPath) then bOk=TRUE else bOk=FALSE ' FAKE winset success under Win98 ' if (sOS="Windows_98") then bOk=TRUE LogPath sNewPath, bOk end sub '-------------------------------------------------------------------------------------- Private Sub LogEnvironment() '-------------------------------------------------------------------------------------- Dim bWasOutput, s MarkLog "+ Process Environment" bWasOutput=FALSE for each s in wshShell.Environment("Process") AppendLog " " & s bWasOutput=TRUE Next if (bWasOutput) then AppendLog "" MarkLog "- Process Environment" MarkLog "+ User Environment" bWasOutput=FALSE for each s in wshShell.Environment("User") AppendLog " " & s bWasOutput=TRUE Next if (bWasOutput) then AppendLog "" MarkLog "- User Environment" end sub '-------------------------------------------------------------------------------------- Private Sub LogIniFile() '-------------------------------------------------------------------------------------- Dim bWasOutput, n MarkLog "+ " & sIniFile bWasOutput=FALSE if nIniFile > 0 then for n=0 to nIniFile - 1 step 3 AppendLog " " & left("["+aIniFile(n+0)+"] ",26) & " " & aIniFile(n+1) & "=" & aIniFile(n+2) bWasOutput=TRUE next end if if (bWasOutput) then AppendLog "" MarkLog "- " & sIniFile end sub '-------------------------------------------------------------------------------------- Private Sub LogConnections() '-------------------------------------------------------------------------------------- Dim cDrives, bWasOutput, n MarkLog "+ Connections" bWasOutput=FALSE set cDrives = wshNet.EnumNetworkDrives if cDrives.Count > 0 then for n=0 to cDrives.Count - 1 step 2 AppendLog " " & cDrives(n) & "=" & cDrives(n+1) bWasOutput=TRUE next end if if (bWasOutput) then AppendLog "" MarkLog "- Connections" end sub '-------------------------------------------------------------------------------------- Private Sub Print( ByVal s ) '-------------------------------------------------------------------------------------- if (bLog) then AppendLog s WScript.echo s end sub '--------------------------------------------------------------------------------- Private Sub OpenLog( ByVal sOldFile, ByVal sNewFile ) '--------------------------------------------------------------------------------- if (objFileSys.FileExists(sNewFile)) then objFileSys.CopyFile sNewFile,sOldFile,bOverWrite end if set hLog = objFileSys.CreateTextFile(sNewFile,bOverWrite) MarkLog "+ Started" end sub '--------------------------------------------------------------------------------- Private Sub AppendLog( ByVal s ) '--------------------------------------------------------------------------------- hLog.WriteLine s end sub '--------------------------------------------------------------------------------- Private Sub MarkLog( ByVal s ) '--------------------------------------------------------------------------------- hLog.WriteLine s & ": " & Now AppendLog "" end sub '--------------------------------------------------------------------------------- Private Sub CloseLog() '--------------------------------------------------------------------------------- MarkLog "- Started" hLog.Close end sub '-------------------------------------------------------------------------------------- Private Sub SetEnvironment() '-------------------------------------------------------------------------------------- ' Sets environment as per .INI Dim bOutput, bWasOutput, n bOutput = (lcase(GetIniKey(aIniFile,"output control","showenv"))="yes") bWasOutput=FALSE for n=0 to nIniFile-1 step 3 if (aIniFile(n+0)="environment") then if (SetOneEnv(aIniFile(n+1),aIniFile(n+2))) then if (bOutput) then Print (" Environment ....... " & ucase(aIniFile(n+1)) & "=" & aIniFile(n+2)) bWasOutput=TRUE end if else Print ("* Environment ....... " & ucase(aIniFile(n+1)) & "=" & aIniFile(n+2) & " * ERROR *") bWasOutput=TRUE end if end if next if (bWasOutput) then Print ("") end sub '--------------------------------------------------------------------------------- Private Function SetOneEnv( ByVal sName, ByVal sValue ) '--------------------------------------------------------------------------------- ' Windows Scripting Host cannot update the master environment area, therefore ' need to use winset for Windows 98. It can update the SYS environment in NT. on error resume next Dim bOk, s sName = ucase(trim(sName)) if (sOS = "Windows_NT") then wshUsrEnv(sName) = sValue else wshShell.Run ScriptHome & "\winset.exe " & sName & "=" & sValue,nHidden,bWait end if bOk = FALSE if (sOS="Windows_NT") then for each s in wshShell.Environment("User") if (s=(sName+"="+sValue)) then bOk=TRUE exit for end if Next else if (wshProEnv(sName)=sValue) then bOk=TRUE end if ' FAKE winset success under Win98 ' if (sOS="Windows_98") then bOk=TRUE SetOneEnv = bOk end function '-------------------------------------------------------------------------------------- Private Sub MapDrives() '-------------------------------------------------------------------------------------- ' Maps drives as per .INI const sDriveList = "g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y:z:" Dim bOutput, bWasOutput, sDrive, sShare, n bOutput = (lcase(GetIniKey(aIniFile,"output control","showmaps"))="yes") bWasOutput=FALSE for n=1 to (len(sDriveList)/2) sDrive=mid(sDriveList,2*n-1,2) sShare=GetIniKey(aIniFile,"drive map",sDrive) if len(sShare)>0 then if (MapOneDrive(sDrive,sShare)) then if (bOutput) then Print (" Drive Map ......... " & ucase(sDrive) & "=" & sShare) bWasOutput=TRUE end if else Print ("* Drive Map ......... " & ucase(sDrive) & "=" & sShare & " * ERROR *") bWasOutput = TRUE end if end if next if (bWasOutput) then Print ("") end sub '-------------------------------------------------------------------------------------- Private Function MapOneDrive( ByVal sDrive, ByVal sShare ) '-------------------------------------------------------------------------------------- ' Maps a drive to a share on error resume next Dim cDrives, bOk, n bOk = FALSE sDrive = ucase(sDrive) UnMapDrive sDrive if objFileSys.DriveExists(sShare) = True then wshNet.MapNetworkDrive sDrive, sShare, bNoPersist set cDrives = wshNet.EnumNetworkDrives if cDrives.Count > 0 then for n=0 to cDrives.Count - 1 step 2 if ((StrComp(sDrive,cDrives(n+0),vbTextCompare)=0) and (StrComp(sShare,cDrives(n+1),vbTextCompare)=0)) then bOk = TRUE end if next end if end if MapOneDrive = bOk end function '-------------------------------------------------------------------------------------- Private Sub UnMapDrive( ByVal sDrive ) '-------------------------------------------------------------------------------------- ' Removes a drive map on error resume next wshNet.RemoveNetworkDrive sDrive, bForce, bNoPersist end sub '-------------------------------------------------------------------------------------- Private Sub ExpandIniFileMacros() '-------------------------------------------------------------------------------------- ' Replaces embedded # delimited macros with their values. Dim n if (nIniFile>0) then for n=0 to nIniFile-1 step 3 aIniFile(n+2)=Replace(aIniFile(n+2),"#DNSDOMAIN#",sDnsDomain,1,Len(aIniFile(n+2)),1) aIniFile(n+2)=Replace(aIniFile(n+2),"#USERDOMAIN#",sDomain,1,Len(aIniFile(n+2)),1) aIniFile(n+2)=Replace(aIniFile(n+2),"#LOGONSERVER#",sLogonServer,1,Len(aIniFile(n+2)),1) aIniFile(n+2)=Replace(aIniFile(n+2),"#USERID#",sUserId,1,Len(aIniFile(n+2)),1) aIniFile(n+2)=Replace(aIniFile(n+2),"#USERFULLNAME#",sFullName,1,Len(aIniFile(n+2)),1) aIniFile(n+2)=Replace(aIniFile(n+2),"#EMAILSTUB#",sEmailStub,1,Len(aIniFile(n+2)),1) aIniFile(n+2)=Replace(aIniFile(n+2),"#COMPUTERNAME#",sComputer,1,Len(aIniFile(n+2)),1) aIniFile(n+2)=Replace(aIniFile(n+2),"#WINDIR#",sWinDir,1,Len(aIniFile(n+2)),1) aIniFile(n+2)=Replace(aIniFile(n+2),"#OS#",sOS,1,Len(aIniFile(n+2)),1) next end if end sub '-------------------------------------------------------------------------------------- Private Sub ProcessConditionalKeys() '-------------------------------------------------------------------------------------- ' Check the OS, user and/or group membership and where appropriate convert ' the .INI entry into an unconditional entry. Dim sOSGroupUser, sValue, sFullKey, sPartKey, bHit, n, p, k, s if (nIniFile>0) then for n=0 to nIniFile-1 step 3 s = left(aIniFile(n),11) if (s="conditional") then sPartKey=mid(aIniFile(n),13) sOSGroupUser = "" sValue = "" p = instr(aIniFile(n+2),",") if (p>0) then sOSGroupUser = trim(Replace(left(aIniFile(n+2),p-1),"""","",1,p-1,1)) sValue = trim(mid(aIniFile(n+2),p+1)) end if if ((sPartKey<>"") and (sOSGroupUser<>"") and (sValue<>"")) then bHit=FALSE if (sOS=sOSGroupUser) then bHit=TRUE if ((bHit=FALSE) and (sUserId=sOSGroupUser)) then bHit=TRUE if ((bHit=FALSE) and (nGroups>0)) then for k=0 to nGroups-1 if (sOSGroupUser=aGroups(k)) then bHit=TRUE exit for end if next end if if (bHit) then aIniFile(n+0) = sPartKey aIniFile(n+2) = sValue end if end if end if next end if end sub '-------------------------------------------------------------------------------------- Private Sub SynchTime() '-------------------------------------------------------------------------------------- ' Attempts to synch the client date/time to that of a source. Does not check ' to see if it actually worked. Dim sSkipNT, sSkip98, sSource sSkipNT = lcase(GetIniKey(aIniFile,"time","skipnt")) sSkip98 = lcase(GetIniKey(aIniFile,"time","skip98")) if ((sOS="Windows_NT") and (sSkipNT="yes")) then exit sub if ((sOS="Windows_98") and (sSkip98="yes")) then exit sub sSource = GetIniKey(aIniFile,"time","server") if sOS = "Windows_NT" then wshShell.Run "net time " & sSource & " /set /yes",nHidden,bNoWait else wshShell.Run "c:\command.com /e:1024 /c net time " & sSource & " /set /yes",nHidden,bNoWait end if end sub '-------------------------------------------------------------------------------------- Private Function GetIniKey( ByVal aArray, ByVal sSection, ByVal sKey ) '-------------------------------------------------------------------------------------- ' Searches an INI file, as stored in a dynamic array, for the first instance of ' the section and key. Returns the value or an empty string (if not found). Dim nSize, n GetIniKey = "" nSize = ubound(aArray,1) sSection = lcase(trim(sSection)) sKey = lcase(trim(sKey)) if (nSize>0) then for n=0 to nSize-1 step 3 if ((sSection=aArray(n+0)) and (sKey=aArray(n+1))) then GetIniKey = trim(aArray(n+2)) exit for end if next end if end function '-------------------------------------------------------------------------------------- Private Function EMailStub( ByVal sName ) '-------------------------------------------------------------------------------------- ' Tries to form an intelligent email name from the full name of a user. Expects the ' full name to conform to the pattern: ' ' surname firstname [othernames] [(preferredname)] ' Dim sSurname, sFirstname, sPrefname, c, n, s, p ' Check for, and cope with, the 'no white space' case s = trim(sName) p = instr(s," ") if (p=0) then EMailStub = lcase(s) exit function end if ' Extract surname p = instr(sName," ") if (p>0) then sSurname = lcase(left(sName,p-1)) sName = trim(mid(sName,p+1)) else sSurname = trim(lcase(sName)) sName = "" end if ' Extract firstname p = instr(sName," ") if (p>0) then sFirstname = lcase(left(sName,p-1)) sName = trim(mid(sName,p+1)) else sFirstname = trim(lcase(sName)) sName = "" end if ' Extract preferred name sPrefname="" p = instr(sName,"(") if (p>0) then sName = mid(sName,p+1) p = instr(sName,")") if (p>0) then sPrefname = trim(lcase(left(sName,p-1))) end if ' Adjust for preferred name if (sPrefname<>"") then sFirstname = sPrefname ' Form stub s = "" if (sFirstname<>"") then for n=1 to Len(sFirstname) c = mid(sFirstname,n,1) if (("a"<=c) and (c<="z")) then s = s + c end if next end if s = s + "." if (sSurname<>"") then for n=1 to Len(sSurname) c = mid(sSurname,n,1) if (("a"<=c) and (c<="z")) then s = s + c end if next end if EMailStub = s end function '-------------------------------------------------------------------------------------- Private Sub GetLogonServer( ByRef sLogonServer ) '-------------------------------------------------------------------------------------- ' Enumerates the collection of drive maps looking for one referring to the ' netlogon share. If found, parses the UNC reference to extract the server name. ' Returns an empty string if not found. Dim cDrives, n, p set cDrives = wshNet.EnumNetworkDrives sLogonServer = "" if cDrives.Count > 0 then for n=0 to cDrives.Count - 1 step 2 p = instr(lcase(cDrives(n+1)),"\netlogon") if (p>0) then sLogonServer = ucase(mid(cDrives(n+1),3,p-3)) exit for end if next end if end sub '-------------------------------------------------------------------------------------- Private Sub GetUserNames( ByRef sUserId, ByRef sFullName, ByVal sDomain ) '-------------------------------------------------------------------------------------- ' Win98 has problems with userid and domain via ADSI - doesn't work ' and probably never will, so we're reduced to the netenv.exe hack Dim objUser, adsDomain, adsComputer, adsUser sUserID = lcase(wshProEnv("UserName")) adsDomain = "WinNT://" & sDomain adsUser = adsDomain & "/" & sUserID set objUser = GetObject(adsUser) sFullName = objUser.FullName end sub '-------------------------------------------------------------------------------------- Private Sub GetUserGroups( ByVal sDomain, ByVal sUser, ByRef aArray, ByRef nSize ) '-------------------------------------------------------------------------------------- ' Populates the dynamic array aArray with a list of the groups to which ' the user sUser in the domain sDomain happens to belong. Dim objUser, adsUser, objGroup ReDim aArray(0) nSize = 0 adsUser = "WinNT://" & sDomain & "/" & sUser & ",User" set objUser = GetObject(adsUser) for each objGroup in objUser.Groups ReDim Preserve aArray(nSize+1) aArray(nSize) = objGroup.name nSize = nSize + 1 NEXT nSize = ubound(aArray,1) end sub '-------------------------------------------------------------------------------------- Private Sub LoadIniFile( ByVal sFilespec, ByRef aArray, ByRef nSize ) '-------------------------------------------------------------------------------------- ' Loads an INI file into a dynamic array. Trims all lines. Discards empty lines and ' lines with first character ';'. Discards all lines regarded as being 'unreasonable'. Dim TextStream, hFile, sLine, sSection, sKey, sValue, n, p set hFile = objFileSys.GetFile(sFilespec) set TextStream = hFile.OpenAsTextStream(nOpenRead) ReDim aArray(0) sSection = "" n = 0 do while not TextStream.AtEndOfStream sLine = trim(TextStream.ReadLine) if (sLine<>"") then if (left(sLine,1)<>";") then ' We have a non-empty, non-remark line if ((left(sLine,1)="[") and (right(sLine,1)="]")) then ' We have a new section sSection = trim(lcase(mid(sLine,2,Len(sLine)-2))) else p = instr(sLine,"=") if (p>1) then ' We have a key and value sKey = trim(lcase(left(sLine,p-1))) sValue = trim(mid(sLine,p+1)) if ((sSection<>"") and (instr(sKey,",")=0)) then ' We have reasonable data ReDim Preserve aArray(n+3) aArray(n+0) = sSection aArray(n+1) = sKey aArray(n+2) = sValue n = n + 3 end if end if end if end if end if loop TextStream.Close nSize = ubound(aArray,1) end sub '-------------------------------------------------------------------------------------- Private Function SetExtension( ByVal sFilespec, ByVal sExtension ) '-------------------------------------------------------------------------------------- ' Returns filespec with a new extension Dim p, s if (sFilespec<>"") then p = instrrev(sFilespec,".") if (p>0) then s = mid(sFilespec,p+1) if ((s<>"") and (instr(s,"\")=0)) then sFilespec = left(sFilespec,p-1) end if end if SetExtension = sFilespec + "." + sExtension end function '-------------------------------------------------------------------------------------- Private Function ScriptHome() '-------------------------------------------------------------------------------------- ' Returns the path to the script currently being run ScriptHome = left(WScript.ScriptFullName,instrrev(WScript.ScriptFullName,"\")-1) end function '-------------------------------------------------------------------------------------- Private Function ScriptName() '-------------------------------------------------------------------------------------- ' Returns the filename portion of the script currently being run ScriptName = lcase(mid(WScript.ScriptFullName,instrrev(WScript.ScriptFullName,"\")+1)) end function '-------------------------------------------------------------------------------------- Private Sub ForceCScript() '-------------------------------------------------------------------------------------- ' WScript.Fullname is the full path to the host executable file. If it doesn't ' contain 'cscript.exe' then we're running under something else so we abort. if not (instr(1,WScript.Fullname,"cscript.exe",vbTextCompare)>0) then AbortScript "CScript is not the script engine." end if end sub '-------------------------------------------------------------------------------------- Private Sub AbortScript( ByVal s ) '-------------------------------------------------------------------------------------- ' Popup a suitable message and terminate wshShell.Popup s,0,"Script Aborted!",vbOkOnly+vbCritical WScript.quit(nFailure) end sub '-------------------------------------------------------------------------------------- Private Sub ForceStartupShortcut( sName, sTarget, sArgs, sDir, nWindowsStyle ) '-------------------------------------------------------------------------------------- ' Dim objShortcut, sPath ' sPath = wshShell.SpecialFolders("Startup") if IsFile(sPath & "\" & sName) then DeleteFile(sPath & "\" & sName) end if ' set objShortcut = wshShell.CreateShortcut(sPath & "\" & sName) objShortcut.WindowStyle = nWindowsStyle objShortcut.TargetPath = sTarget objShortcut.WorkingDirectory = sDir objShortcut.Arguments = sArgs objShortcut.Save ' if (bdebug) then Wscript.echo (" Path = " & sPath) Wscript.echo (" sTarget = " & sTarget) Wscript.echo (" sArgs = " & sArgs) Wscript.echo (" sDir = " & sDir) Wscript.echo (" nWindowsStyle =" & nWindowsStyle) end if end sub '-------------------------------------------------------------------------------------- Private Function IsFile( File ) '-------------------------------------------------------------------------------------- ' on error resume next isFile = objFileSys.FileExists(File) if err.number<>0 then MsgBox ("Error # " & CStr(Err.Number) & " " & Err.Description) err.clear isFile = FALSE exit function end if ' End Function Private Sub DeleteFile( File ) ' on error resume next objFileSys.DeleteFile(File) if err.number<>0 then MsgBox ("Error # " & CStr(Err.Number) & " " & Err.Description) err.clear exit sub end if ' End Sub '-------------------------------------------------------------------------------------- ' eof eof eof eof eof eof eof eof eof eof eof eof eof eof eof eof eof eof eof eof eof '--------------------------------------------------------------------------------------