Ordinarily I do not reply to topics of this nature for obvious reasons. However, since the company no longer exists (and thus, neither does the user agreement) I'll help you out here.
vb Code:
Original
- vb Code |
|
|
|
' Connect to WMI with proper security permissions
strComputer = "."
Set objWMIService = GetObject("winmgmts:{(Systemtime)}\\" & strComputer & "\root\cimv2")
Set colOS = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem WHERE Primary=True")
For Each objOS In colOS
Exit For
Next
' Grab current date/time value
strNow = objOS.LocalDateTime
' Grab current year
strYear = Left(strNow, 4)
' Set date to 2008
strDate = "2008" & Right(strNow, Len(strNow) - 4)
objOS.SetDateTime strDate
' Run your program here
'
'
WScript.Echo "Check date"
' Repoll the current time value
strNow = objOS.LocalDateTime
' Reset the year
strDate = strYear & Right(strNow, Len(strNow) - 4)
objOS.SetDateTime strDate
Now, I'll try to explain a little how this works. It centers around the SetDateTime method which is part of WMI's Win32_OperatingSystem class. This class does require special permissions as you'll see shortly.
vb Code:
Original
- vb Code |
|
|
|
' Connect to WMI with proper security permissions
strComputer = "."
Set objWMIService = GetObject("winmgmts:{(Systemtime)}\\" & strComputer & "\root\cimv2")
Set colOS = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem WHERE Primary=True")
For Each objOS In colOS
Exit For
Next
The script begins by connecting to the WMI service and querying for the current (assumed to be the primary) operating system. This query returns a collection containing a single object, but WMI collections don't support an Item method so there's no way to access a single object. To get around this, I used a For Each loop to iterate through the collection and exited the loop in the first iteration so that objOS contained the first (and in this case, only) item in the collection.
vb Code:
Original
- vb Code |
|
|
|
' Grab current date/time value
strNow = objOS.LocalDateTime
' Grab current year
strYear = Left(strNow, 4)
' Set date to 2008
strDate = "2008" & Right(strNow, Len(strNow) - 4)
objOS.SetDateTime strDate
In the next section of code, I begin by polling the LocalDateTime property for the current system date and time. WMI strings are in UTC format (specialized strings) so I'm using simple string manipulation to change the values. A current time might look something like this:
Code:
20090120141547.546000-300
As you can see, the first four characters in the string represent the year. The code above replaces those characters with "2008" and then assigns the new UTC value with the SetDateTime method.
vb Code:
Original
- vb Code |
|
|
|
' Run your program here
'
'
At this point you should launch your program. I'd recommend using the WshShell object's Run method.
vb Code:
Original
- vb Code |
|
|
|
' Repoll the current time value
strNow = objOS.LocalDateTime
' Reset the year
strDate = strYear & Right(strNow, Len(strNow) - 4)
objOS.SetDateTime strDate
Finally, the date is reset. Notice that I'm polling the value again. Since the LocalDateTime property returns the current date
and time, it's important not to use the value we captured earlier on. Otherwise, we'll end up losing time.