Link to home
Start Free TrialLog in
Avatar of SDJ_1
SDJ_1

asked on

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
    g_objFS.CreateFolder(g_strLogFolder)
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"
Else
    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
        Next
    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")
			Else
				Call WriteToLog("Manual install switch is missing, exiting the script")
				Main = 2
'				Exit Function
			End If
	  End If
	Else
		Call WriteToLog("The script is running on off-business hours")
	End If
  End If
End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
more missing functions:
getipaddress
GetOutput

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: " & _
        objOperatingSystem.NumberOfLicensedUsers
    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
Next
wscript.echo GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth

Open in new window

http://blog.biolizards.be/vbscript-check-os-version-information-and-detect-32-bit-or-64-bit-windows/
are you trying to change the hosts file? or read the hosts file ?
Avatar of SDJ_1
SDJ_1

ASKER

I've requested that this question be deleted for the following reason:

issue has been resolved