VB Script - Windows 2008 Server

Trying to modify below VB Script to check to make sure script is running off hours.  No issues are coming up when manually running script but it's not producing a log file, can you tell me where I'm going wrong?   Thanks.

Option Explicit
On Error Resume Next

' Establish global variables.
Dim g_intMain, g_intLocale
Dim g_objFS, g_objShell, g_objCSV, g_objRegister
Dim g_strLogFolder, g_strLogFile, g_strServerName, g_strCurrentPath, g_strHosts
Dim g_strCSVTemp, g_strWinDir, g_strCSV, g_strUtils, g_strServerIPAddress, g_strEnvType
Dim g_strBPS, dNow

' Constants
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const MinWindow = 7
'Const SuccessCode = 555
'Const PendingCode = 777
'Const RegisterThreshold = 0.75
'Const LocaleUSA = 1033
dNow = Now()

' Set global variables.
Set g_objFS = CreateObject("Scripting.FileSystemObject")
Set g_objShell = CreateObject("WScript.Shell")
Set g_objRegister = CreateObject("Scripting.Dictionary")
Set g_intLocale = SetLocale(LocaleUSA)

g_strServerName = UCase(g_objShell.ExpandEnvironmentStrings("%COMPUTERNAME%"))
g_strServerIPAddress = GetIPAddress(g_strServerName)
g_strCurrentPath = g_objFS.GetAbsolutePathName(".")
g_strHosts = GetOutput("type %systemroot%\system32\drivers\etc\hosts")

' Establish location for log file.
g_strLogFolder = "C:\TJXLogs"
If Not g_objFS.FolderExists(g_strLogFolder) Then
End If
If Not WScript.Arguments.Named.Exists("Target") And Not WScript.Arguments.Named.Exists("Controller") Then
    g_strLogFile = g_strLogFolder & Chr(92) & "2EncryptLoggingLevelChange_" & DateSuffix() & ".log"
    If g_objFS.FileExists(g_strLogFile) Then
        Call g_objFS.DeleteFile(g_strLogFile, True)
    End If
'    g_strCSV = g_strLogFolder & Chr(92) & "PWRotationDeliveryResults.csv"
    g_strLogFile = g_strLogFolder & Chr(92) & "2EncryptLoggingLevelChange_" & DateSuffix() & "_Target.log"

End If

' Declare function variables.
Dim arrEnvType, blnEnvType

 ' Determine software environment.
    blnEnvType = False
    arrEnvType = Array("DEV", "QA", "PROD")
    If WScript.Arguments.Named.Exists("EnvType") Then
        For Each g_strEnvType In arrEnvType
            If UCase(WScript.Arguments.Named("EnvType")) = g_strEnvType Then
                Call WriteToLog("Software environment marked as" & Chr(32) & EnvType(g_strEnvType) & ".")
                blnEnvType = True
                Exit For
            End If
    End If
	' Software environment must be detected to proceed.
    If Not blnEnvType Then
        Call WriteToLog("Error: Software environment type not recognized or specified as an argument.")
        Call WriteToLog("Aborting installation script.")
        Main = 6
'        Exit Function
    End If

' Windows 2008 requires reboot, so the script is allowed to run before 8 AM OR after 11 PM OR manually at any time of the day
If EnvType(g_strEnvType) = "Production" Then
  If  g_objFS.FileExists("C:\Windows\System32\ServerManagerCmd.exe") Then
	Call WriteToLog("Windows 2008 require reboot to complete the 2Encrypt Logging Level Change")
	If DatePart("h",Now()) > 8 Then

	  If DatePart("h",Now()) < 23 Then
		Call WriteToLog("The script is not allowed to run during business hours ie 8 AM to 11 PM, checking if it is a manual install")
			If WScript.Arguments.Named.Exists("Now") Then
				Call WriteToLog("Running the script manually to overcome the time restriction")
				Call WriteToLog("Manual install switch is missing, exiting the script")
				Main = 2
'				Exit Function
			End If
	  End If
		Call WriteToLog("The script is running on off-business hours")
	End If
  End If
End If

Open in new window

Who is Participating?
RobSampsonConnect With a Mentor Commented:
Hi, the code contains a whole bunch of calls to a WriteToLog procedure, but that procedure does not exist in the code.

Have you forgotten to include that procedure, or do you need one written?


David Johnson, CD, MVPOwnerCommented:
more missing functions:

poor testing
If  g_objFS.FileExists("C:\Windows\System32\ServerManagerCmd.exe") Then
      Call WriteToLog("Windows 2008 require reboot to complete the 2Encrypt Logging Level Change")

use some code from this get-os-info.vbs
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    Wscript.Echo "Boot Device: " & objOperatingSystem.BootDevice
    Wscript.Echo "Build Number: " & objOperatingSystem.BuildNumber
    Wscript.Echo "Build Type: " & objOperatingSystem.BuildType
    Wscript.Echo "Caption: " & objOperatingSystem.Caption
    Wscript.Echo "Code Set: " & objOperatingSystem.CodeSet
    Wscript.Echo "Country Code: " & objOperatingSystem.CountryCode
    Wscript.Echo "Debug: " & objOperatingSystem.Debug
    Wscript.Echo "Encryption Level: " & objOperatingSystem.EncryptionLevel
    dtmConvertedDate.Value = objOperatingSystem.InstallDate
    dtmInstallDate = dtmConvertedDate.GetVarDate
    Wscript.Echo "Install Date: " & dtmInstallDate
    Wscript.Echo "Licensed Users: " & _
    Wscript.Echo "Organization: " & objOperatingSystem.Organization
    Wscript.Echo "OS Language: " & objOperatingSystem.OSLanguage
    Wscript.Echo "OS Product Suite: " & objOperatingSystem.OSProductSuite
    Wscript.Echo "OS Type: " & objOperatingSystem.OSType
    Wscript.Echo "Primary: " & objOperatingSystem.Primary
    Wscript.Echo "Registered User: " & objOperatingSystem.RegisteredUser
    Wscript.Echo "Serial Number: " & objOperatingSystem.SerialNumber
    Wscript.Echo "Version: " & objOperatingSystem.Version
wscript.echo GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth

Open in new window

David Johnson, CD, MVPOwnerCommented:
are you trying to change the hosts file? or read the hosts file ?
SDJ_1Author Commented:
I've requested that this question be deleted for the following reason:

issue has been resolved
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.