Jump to content

[Solved] SCHTASKS that extend to the next day and beyond?


Recommended Posts

Hello all,

I have the following script that will create a Schduled Task to remove a user from the Administrators group after an amount of time has elapsed. It defaults to 2 hours, but via the command line, I'd like to schedule for any number of hours or days. The problem I have is how do I take a number of hours passed from the command line and convert it to days, or days + hours? Which can then be properly passed to SCHTASKS as /SD (startdate) and /ST (starttime)?

Here is what I have so far:

#include <Date.au3>

If $CmdLine[0] = 1 Then

$objName = $CmdLine[1]
$sDateTime = _DateAdd('h', 2, _NowCalc())

ElseIf $CmdLine[0] = 2 Then

$objName = $CmdLine[1]

If $CmdLine[2] > 0 And $CmdLine[2] < 24 Then
$sDateTime = _DateAdd('h', $CmdLine[2], _NowCalc())
ElseIf $CmdLine[2] > 23 Then
$sDateTime = _DateAdd('D', 1, _NowCalc())
Else
$sDateTime = _DateAdd('h', 2, _NowCalc())
EndIf

Else

$objName = "domain\username"
$sDateTime = _DateAdd('h', 2, _NowCalc())

EndIf

If @OSVersion = "WIN_VISTA" Or @OSVersion = "WIN_7" Or @OSVersion = "WIN_8" Then
Run('SCHTASKS /Create /SC ONCE /TN DeleteAdmin /TR "net localgroup administrators /delete "' & $objName & ' /ST ' & $sRunTime & ' /RU SYSTEM /Z /V1', "", @SW_HIDE)
ElseIf @OSVersion = "WIN_XP" Then
Run('SCHTASKS /Create /SC ONCE /TN DeleteAdmin /TR "net localgroup administrators /delete "' & $objName & ' /ST ' & $sRunTime & ' /RU SYSTEM', "", @SW_HIDE)
Else
Exit
EndIf

Any suggestions, or a post you can point me toward?

Thanks,

-Mike

Edited by mdwerne
Link to comment
Share on other sites

_Dateadd() will roll over the date if necessary....

You just need to split up the result into different date and time fields possibly using string functions like:

#include <Date.au3>

;Global $objName = $CmdLine[1], $hours = 2 ; commented out for testing
Global $sDateTime, $sStartDate, $sStartTime, $sCmd, $sXtra_Parms

If $CmdLine[0] = 2 Then $hours = $CmdLine[2]

$objName = "\\Server1\RemotePC2" ; for testing
$hours = 12 ; for testing

$sDateTime = _DateAdd('h', $hours, _NowCalc())
$sStartDate = StringMid($sDateTime, 6, 3) & StringMid($sDateTime, 9, 2) & "/" & StringLeft($sDateTime, 4)
$sStartTime = StringRight($sDateTime, 8)

If @OSVersion = "WIN_VISTA" Or @OSVersion = "WIN_7" Or @OSVersion = "WIN_8" Then $sXtra_Parms = " /Z /V1"
$sCmd = 'SCHTASKS /Create /SC ONCE /TN DeleteAdmin /TR "net localgroup administrators /delete "' & $objName & ' /SD ' & $sStartDate & ' /ST ' & $sStartTime & ' /RU SYSTEM' & $sXtra_Parms
;Run($sCmd, "", @SW_HIDE) ; commented out for testing
MsgBox(0, "", $sCmd) ; for testing

Edit: I didn't run this, but it looks like you have a spacing error in the cmd string about where the $objName variable is inserted.

Edited by Spiff59
Link to comment
Share on other sites

Thanks Spiff59!

That did the trick. I changed the hours to 8765 (roughly a year) just to see what would happen, and the msgbox shows the right date and time. I also like how you shortened up the OS test. :thumbsup:

I'll have a look at the spacing error, thanks for noticing.

All the best,

-Mike

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...