WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Boot and Logon Timer

 Keywords:  

This script calculates the amount of time to boot up and logon to a machine. It sets up an autologon so the amount of time to type in credentials is not an issue.
; Name: BootTime.wbt
; Purpose: Set up auto-admin logon, restart computer, and measure time to boot up and logon.
; Author: Les Ferch
; 2008-10-27: Initial release

GoSub Initialize

If AutoAdminLogon=="1"
   FileDelete(ShortcutFile)
   GoSub DisableAutoAdminLogon
   GoSub GetBootTime
Else
   GoSub GetPassword
   IntControl(1003,1,0,0,0) ; Enable WinBatch Window
   BoxOpen(Title,"Restarting...")
   ShortCutMake(ShortcutFile,MyFullPath,"",DirScript(),@NORMAL)
   GoSub BackupCurrentState
   GoSub EnableAutoAdminLogon
   IntControl(67,0,1,0,0) ; Reboot
   TimeDelay(300)
EndIf

Exit


;---------------------------------------------------------------------------------------------------------
:Initialize

IntControl(1003,0,0,0,0) ; Disable WinBatch Window
IntControl(12,5,0,0,0) ; Terminate silently
ErrorMode(@OFF) ; So sue me

Title = "BootTime"
BoxTitle(Title)

WinVer = WinVersion(1):".":WinVersion(0)
Terminate(Version()<"2006D",Title,"ERROR: WinBatch 2006D or higher is required to run this script")
Terminate(WinVer<"4.0",Title,"ERROR: This utility can only be run on Windows NT4/2000/XP computers.")

#DefineFunction AdminUser() ; Check for Admin rights
Admin = @FALSE
RegSetValue(@REGMACHINE,"System\CurrentControlSet\Control[Admin]","1")
Admin = RegQueryValue(@REGMACHINE,"System\CurrentControlSet\Control[Admin]")
RegDelValue(@REGMACHINE,"System\CurrentControlSet\Control[Admin]")
If Admin=="1" Then Admin = @TRUE
Return Admin
#EndFunction

Terminate(!AdminUser(),Title,"ERROR: This utility requires System or Admin rights to run.")

DirChange(DirScript())

WinDir = Environment("WinDir")
ComputerName = Environment("ComputerName")
ProgramFiles = Environment("ProgramFiles")
Comspec = Environment("Comspec")
Temp = Environment("Temp")
AllUsersProfile = Environment("AllUsersProfile")
Username = Environment("Username")
UserDomain = Environment("UserDomain")

ShortcutFile = AllUsersProfile:"\Start Menu\Programs\Startup\BootTime.lnk"
MyFullPath = IntControl(1004,0,0,0,0)
ShortcutPath = MyFullPath
If RtStatus()!=1 Then ShortcutPath = '"':DirHome():'WinBatch.exe" "':MyFullPath:'"'

WinLogonKey = "Software\Microsoft\Windows NT\CurrentVersion\WinLogon"
BackupKey = WinLogonKey:"\AutoAdminBackup"
PoliciesKey = "Software\Microsoft\Windows\CurrentVersion\policies\system"

ZENWorks = FileExist(ProgramFiles:"\Novell\ZENworks\NALDESK.EXE") ; You may need to modify this test for your environment

#DefineSubRoutine GetValue(Key,ValName,DefVal)
v = RegQueryValue(@REGMACHINE,Key:"[":ValName:"]")
If !RegExistValue(@REGMACHINE,Key:"[":ValName:"]") Then v = DefVal
Return v
#EndFunction

AutoAdminLogon = GetValue(WinLogonKey,"AutoAdminLogon","")

Return


;---------------------------------------------------------------------------------------------------------
:GetPassword
Password = ""
While 1
   Password = AskPassword(Title,UserDomain:"\":Username:" password:")
   Success = RunWithLogon("wscript.exe","/b","",@NORMAL,@WAIT,Username,UserDomain,Password,0)
   If Success Then Break
   Else Message(Title,"Credential verifcation failed. Check password.")
EndWhile

Ready = AskYesNo(Title,"Restart system and measure boot time?")
If !Ready Then Exit
Return


;---------------------------------------------------------------------------------------------------------
:GetBootTime

; System startup time is calculated to be last modified time of oldest of the files listed below
; This yields a time that is very close to "stop watch time"
; Since the last modified time is actually at the end of shutdown, this technique is only
; useful if you reboot and auto-logon, which is what this script does

; Another approach is to write a script that just calculates based on the WMI last boot value
; That would be simpler, but less accurate (typically off by about 15 seconds)

Config = WinDir:"\System32\Config\"
T1 = FileYmdHms(Config:"default")
T2 = FileYmdHms(Config:"sam")
T3 = FileYmdHms(Config:"security")
T4 = FileYmdHms(Config:"software")
T5 = FileYmdHms(Config:"system")
TL = T1
If TimeDiffSecs(TL,T2)>0 Then TL = T2
If TimeDiffSecs(TL,T3)>0 Then TL = T3
If TimeDiffSecs(TL,T4)>0 Then TL = T4
If TimeDiffSecs(TL,T5)>0 Then TL = T5

; Caution. This will result in an infinite loop if the ZENWorks test passed but NALDesk.exe did not load.
If ZENWorks
   While !AppExist("NALDesk.exe",0,0)
      TimeDelay(1)
   EndWhile
   ; Wait for NAL icon to appear on desktop
   ZENIcon = "Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\{CF8EF420-3DF3-11d0-AC35-00C04FD9338A}"
   While !RegExistKey(@REGMACHINE,ZENIcon)
      TimeDelay(1)
   EndWhile
EndIf

; Display results
CurrentTime = TimeYmdHms()
Elapsed = TimeDiff(CurrentTime,TL)
ElapsedSecs = TimeDiffSecs(CurrentTime,TL)
L1 = "Time of last shutdown: ":TL:@CRLF:@CRLF
L2 = "Current time: ":CurrentTime:@CRLF:@CRLF
L3 = "Time elapsed (seconds): ":ElapsedSecs:@CRLF:@CRLF
L4 = "Time elapsed (YMDHMS): ":Elapsed
Message(Title,L1:L2:L3:L4)

Return


;---------------------------------------------------------------------------------------------------------
:BackupCurrentState

If RegExistKey(@REGMACHINE,BackupKey) Then Return

Null = Environment("")
DefaultDomainName = GetValue(WinLogonKey,"DefaultDomainName",ComputerName)
AltDefaultDomainName = GetValue(WinLogonKey,"AltDefaultDomainName",ComputerName)
AutoAdminLogon = GetValue(WinLogonKey,"AutoAdminLogon","")
DefaultUsername = GetValue(WinLogonKey,"DefaultUsername","")
DefaultPassword = GetValue(WinLogonKey,"DefaultPassword","")
ForceAutoLogon = GetValue(WinLogonKey,"ForceAutoLogon","")
NWDefDomain = GetValue("Software\Novell\Location Profiles\Services\{1E6CEEA1-FB73-11CF-BD76-00001B27DA23}\Default\Tab3","DefaultDomainName","")
NWAutoAdmin = GetValue("Software\Novell\Login","AutoAdminLogon","")
LegalNoticeCaption = GetValue(PoliciesKey,"LegalNoticeCaption","")
LegalNoticeText = GetValue(PoliciesKey,"LegalNoticeText","")
If StrCharCount(StrTrim(LegalNoticeCaption))<2 Then LegalNoticeCaption = ""
If StrCharCount(StrTrim(LegalNoticeText))<2 Then LegalNoticeText = ""
LegalNoticePolicy = "1"
If LegalNoticeCaption=="" && LegalNoticeText==""
   LegalNoticePolicy = "0"
   LegalNoticeCaption = GetValue(WinLogonKey,"LegalNoticeCaption","")
   LegalNoticeText = GetValue(WinLogonKey,"LegalNoticeText","")
   If StrCharCount(StrTrim(LegalNoticeCaption))<2 Then LegalNoticeCaption = ""
   If StrCharCount(StrTrim(LegalNoticeText))<2 Then LegalNoticeText = ""
EndIf

If DefaultDomainName=="0" Then DefaultDomainName = ""
If NWDefDomain=="0" Then NWDefDomain = ""

RegSetValue(@REGMACHINE,BackupKey:"[DefaultDomainName]",DefaultDomainName)
RegSetValue(@REGMACHINE,BackupKey:"[AltDefaultDomainName]",AltDefaultDomainName)
RegSetValue(@REGMACHINE,BackupKey:"[AutoAdminLogon]",AutoAdminLogon)
RegSetValue(@REGMACHINE,BackupKey:"[DefaultUsername]",DefaultUsername)
RegSetValue(@REGMACHINE,BackupKey:"[DefaultPassword]",DefaultPassword)
RegSetValue(@REGMACHINE,BackupKey:"[ForceAutoLogon]",ForceAutoLogon)
RegSetValue(@REGMACHINE,BackupKey:"[NWDefDomain]",NWDefDomain)
RegSetValue(@REGMACHINE,BackupKey:"[NWAutoAdmin]",NWAutoAdmin)
RegSetValue(@REGMACHINE,BackupKey:"[LegalNoticeCaption]",LegalNoticeCaption)
RegSetValue(@REGMACHINE,BackupKey:"[LegalNoticeText]",LegalNoticeText)
RegSetValue(@REGMACHINE,BackupKey:"[LegalNoticePolicy]",LegalNoticePolicy)

Return


;---------------------------------------------------------------------------------------------------------
:EnableAutoAdminLogon

RegSetValue(@REGMACHINE,WinLogonKey:"[AutoAdminLogon]","1")
RegSetValue(@REGMACHINE,WinLogonKey:"[DefaultUsername]",Username)
RegSetValue(@REGMACHINE,WinLogonKey:"[DefaultPassword]",Password)

If RegExistKey(@REGMACHINE,"Software\Novell")
   RegSetValue(@REGMACHINE,"Software\Novell\Login[AutoAdminLogon]","1")
   RegSetValue(@REGMACHINE,"Software\Novell\Login[DefaultUsername]",Username)
   RegSetValue(@REGMACHINE,"Software\Novell\Login[DefaultPassword]",Password)
EndIf

RegDelValue(@REGMACHINE,PoliciesKey:"[LegalNoticeCaption]")
RegDelValue(@REGMACHINE,PoliciesKey:"[LegalNoticeText]")
RegDelValue(@REGMACHINE,WinLogonKey:"[LegalNoticeCaption]")
RegDelValue(@REGMACHINE,WinLogonKey:"[LegalNoticeText]")

Return


;---------------------------------------------------------------------------------------------------------
:DisableAutoAdminLogon

DefaultDomainName = ComputerName
AltDefaultDomainName = ComputerName
AutoAdminLogon = "0"
DefaultUsername = ""
DefaultPassword = ""
ForceAutoLogon = "0"
NWDefDomain = ComputerName
NWAutoAdmin = ""
LegalNoticeCaption = ""
LegalNoticeText = ""

If RegExistKey(@REGMACHINE,BackupKey)
   DefaultDomainName = RegQueryValue(@REGMACHINE,BackupKey:"[DefaultDomainName]")
   AltDefaultDomainName = RegQueryValue(@REGMACHINE,BackupKey:"[AltDefaultDomainName]")
   AutoAdminLogon = RegQueryValue(@REGMACHINE,BackupKey:"[AutoAdminLogon]")
   DefaultUsername = RegQueryValue(@REGMACHINE,BackupKey:"[DefaultUsername]")
   DefaultPassword = RegQueryValue(@REGMACHINE,BackupKey:"[DefaultPassword]")
   ForceAutoLogon = RegQueryValue(@REGMACHINE,WinLogonKey:"[ForceAutoLogon]")
   NWDefDomain = RegQueryValue(@REGMACHINE,BackupKey:"[NWDefDomain]")
   NWAutoAdmin = RegQueryValue(@REGMACHINE,BackupKey:"[NWAutoAdmin]")
   LegalNoticeCaption = RegQueryValue(@REGMACHINE,BackupKey:"[LegalNoticeCaption]")
   LegalNoticeText = RegQueryValue(@REGMACHINE,BackupKey:"[LegalNoticeText]")
   LegalNoticePolicy = RegQueryValue(@REGMACHINE,BackupKey:"[LegalNoticePolicy]")
   If StrCharCount(StrTrim(LegalNoticeCaption))<2 Then LegalNoticeCaption = ""
   If StrCharCount(StrTrim(LegalNoticeText))<2 Then LegalNoticeText = ""
EndIf

If DefaultDomainName=="0" Then DefaultDomainName = ""
If NWDefDomain=="0" Then NWDefDomain = ""

RegSetValue(@REGMACHINE,WinLogonKey:"[DefaultDomainName]",DefaultDomainName)
RegSetValue(@REGMACHINE,WinLogonKey:"[AltDefaultDomainName]",AltDefaultDomainName)
RegSetValue(@REGMACHINE,WinLogonKey:"[AutoAdminLogon]",AutoAdminLogon)
RegSetValue(@REGMACHINE,WinLogonKey:"[DefaultUsername]",DefaultUsername)
RegSetValue(@REGMACHINE,WinLogonKey:"[DefaultPassword]",DefaultPassword)
RegSetValue(@REGMACHINE,WinLogonKey:"[ForceAutoLogon]",ForceAutoLogon)

If RegExistKey(@REGMACHINE,"Software\Novell")
   KeyVal = "Software\Novell\Location Profiles\Services\{1E6CEEA1-FB73-11CF-BD76-00001B27DA23}\Default\Tab3[DefaultDomainName]"
   If NWDefDomain=="" Then RegDelValue(@REGMACHINE,KeyVal)
   Else RegSetValue(@REGMACHINE,KeyVal,NWDefDomain)
   KeyVal = "Software\Novell\Login[AutoAdminLogon]"
   If NWAutoAdmin=="" Then RegDelValue(@REGMACHINE,KeyVal)
   Else RegSetValue(@REGMACHINE,KeyVal,NWAutoAdmin)
EndIf

If LegalNoticeCaption<>"" || LegalNoticeText<>""
   Key = WinLogonKey
   If LegalNoticePolicy=="1" Then Key = PoliciesKey
   RegSetValue(@REGMACHINE,Key:"[LegalNoticeCaption]",LegalNoticeCaption)
   RegSetValue(@REGMACHINE,Key:"[LegalNoticeText]",LegalNoticeText)
EndIf

RegDeleteKey(@REGMACHINE,BackupKey)

Return

Article ID:   W18237
Filename:   Boot and Logon Timer.txt
File Created: 2008:10:27:13:04:32
Last Updated: 2008:10:27:13:04:32