Script to change date on a computer

'
' script to change computer date
'
DIM objShell

strComputer = "." ' current host
' get current time
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
Wscript.Echo "Current Date: " & strDate
strNow = objOS.LocalDateTime

' loop through to get last 5 date
FOR counter=1 TO 5
' get current system date – 1
dtPrev = DateAdd("d", -1, Date)
' get month
dtMonth = Month(dtPrev)
If dtMonth < 10 Then
dtMonth = "0" & dtMonth
End If

' get day
dtDay = Day(dtPrev)
If dtDay < 10 Then
dtDay = "0" & dtDay
End If

dtStrPrev = Year(dtPrev) & dtMonth & dtDay ' set previous date string
strDate = dtStrPrev & Right(strNow, Len(strNow) – 8) ' set date
Wscript.Echo "Setting Date to: " & dtStrPrev
objOS.SetDateTime strDate ' set new date
' perform task with new date
set objShell = wscript.createObject("wscript.shell")
iReturn = objShell.Run("cmd.exe /C set var=hello", 1, TRUE)
Next

objOS.SetDateTime strNow ' Set the date back to what it was before
Wscript.Echo "Date Reset: " & Date

Output:
C:\temp>cscript c.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Current Date: 20100929221421.174000-360
Setting Date to: 20100928
Setting Date to: 20100927
Setting Date to: 20100926
Setting Date to: 20100925
Setting Date to: 20100924
Date Reset: 9/29/2010

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s