Page 1 of 2 12>
Topic Options
#177094 - 2007-06-20 05:41 PM Script to display Account Expiration date to user.
RFW Offline
Fresh Scripter

Registered: 2006-04-19
Posts: 9
Loc: New York
We use the Account Expiration date to ensure users complete a mandatory yearly training requirement. Unfortunately, our users are always the brightess lightbulbs in the house, and they forget when they need to complete the training by again. I've been working on a script that will display to the user when their account expires and reminds them of the mandatory training. Unfortunately, when it runs from non-admin account the net user commands. Can someone please take a look and see if they can provide another method to retrieve and necessary information? Many thanks in advance.

Code starts
---------------------------
 Code:
$User = ucase(@USERID)
$Location = "Z:\" + @WKSTA + "_" + @USERID + ".txt"
$A = 0
$B = 0
$MM
$DD
$YYYY

Shell '%comspec% /c net user ' + @USERID + ' /domain > ' + $Location
Sleep 10
If Open(1, $Location) = 0
	$x = ReadLine(1)
	While SubStr($x,1,15) <> "Account expires"
		$x = ReadLine(1)
	Loop
	$A = 30
	While $M <> "/"
		$M = SubStr($X,$A,1)
		If $M <> "/"
			$A = $A + 1
			$B = $B + 1
		EndIf
	Loop
	If $B = 2
		$MM = SubStr($X, $A-$B, $B)
	Else
		$MM = "0" + SubStr($X, $A-$B, $B)
	EndIf
	$A = $A + 1
	$B = 0
	While $D <> "/"
		$D = SubStr($X,$A,1)
		If $D <> "/"
			$A = $A + 1
			$B = $B + 1
		EndIf
	Loop
	If $B = 2
		$DD = SubStr($X, $A-$B, $B)
	Else
		$DD = "0" + SubStr($X, $A-$B, $B)
	EndIf
	$A = $A + 1
	$B = 0
	While $Y <> " "
		$Y = SubStr($X,$A,1)
		If $Y <> " "
			$A = $A + 1
			$B = $B + 1
		EndIf
	Loop
	$YYYY = SubStr($X, $A-$B, $B)
	$EXPIRE = $YYYY + "/" + $MM + "/" + $DD
	$DAYS = DATECALC($EXPIRE, @DATE)
	Cls
	SetConsole("alwaysontop")
	Select
	Case $DAYS > 90
	;White/Green
	Color W+/G
	Box (0, 0, 23, 79, " ")
	Color G+/G
	At (3,23) Small "Your account expiration date is:"
	At (7,0) Big $EXPIRE
;	At (17,18) Small "You have " + $DAYS + " days to complete the mandatory"
;	At (18,23) Small "Information Assurance Training."
	Sleep 3
	Case $DAYS < 90 And $DAYS > 45
	;White/Yellow
	Color W+/Y+
	Box (0, 0, 23, 79, " ")
	Color N/Y+
	At (3,23) Small "Your account expiration date is:"
	At (7,0) Big $EXPIRE
	At (17,18) Small "You have " + $DAYS + " days to complete the mandatory"
	At (18,23) Small "Information Assurance Training."
	Sleep 7
	Case $DAYS < 45
	;White/Red
	Color W+/R+
	Box (0, 0, 23, 79, " ")
	Color R/R+
	At (3,23) Small "Your account expiration date is:"
	At (7,0) Big $EXPIRE
	At (17,18) Small "You have " + $DAYS + " days to complete the mandatory"
	At (18,23) Small "Information Assurance Training."
	Sleep 15
	EndSelect
	Close(1)
Else
	Beep
	? "Expire.txt file not opened, error code: [" + @ERROR + "]"
	Sleep 5
EndIf

Del $Location /c

Return ; Must be last line of file. Do not remove this line

;FUNCTION         DateCalc()
;
;AUTHOR           Jochen Polster (jochenDOTpolsterATgmxDOTnet)
;                 based on date algorithms by Peter Baum to be found here :
;                 http://www.capecod.net/~pbaum/date/date0.htm
;
;VERSION          1.12
;
;VERSION HISTORY  1.0  2001/12/10 Initial release
;
;                 1.1  2004/02/18 Added support for single digit month/day input
;                                 and optional single digit month/day date return
;
;                 1.11 2004/02/20 Minor Variable handling fix
;
;                 1.12 2005/03/31 Finally supports "NoVarsInStrings" and "Explicit" set to "ON" in
;                                 all possible variations
;
;ACTION           Calculates days between 2 dates or returns a date string calculated from
;                 a given date and a given amount of days ( Addition of positive or negative
;                 integer value )
;
;SYNTAX           DateCalc( Date1, Date2|Modifier, [SingleDigit] )
;
;PARAMETERS       Date1 (Required)
;                  -  (Gregorian) Date string in Format : YYYY/M[M]/D[D]
;
;                 Date2|Modifier (Required)
;                  - either a second (Gregorian) date string (YYYY/M[M]/D[D]) to calculate days between
;                    or a positive/negative amount of days to calculate with
;
;                 SingleDigit (Optional)
;                  - if not zero date will be returned unpadded, eg. 2004/2/9
;
;REMARKS          Date format must be KiX friendly : YYYY/M[M]/D[D] (2001/11/20)
;                 To calculate a date less than given assign a negative integer (ie. -45 )
;
;RETURNS          Either a positive integer value of days between two given dates,
;                 or a (Gregorian) date string.
;
;DEPENDENCIES     None !
;
;EXAMPLES
;                 break on
;                 call "[path]DateCalc.udf"
;
;                 "boot.ini last modified : " + DateCalc(@date,substr(getfiletime("c:\boot.ini"),1,10))
;                  + " days ago ..." ? ?
;
;                 $mod = 60
;                 "in/before $mod day(s) it was/will be " + DateCalc(@date,$mod) ? ?
;
;                 get $

Function DateCalc($date1, $DateOrMod, optional $SingleDigit)

    Dim $_intDate1, $_intYear1, $_intMonth1, $_intDay1
    Dim $_intDate2, $_intYear2, $_intMonth2, $_intDay2

    $date1 = Split($date1,'/')
    If Ubound($date1) <> 2
        Exit 1
    EndIf

    $_intYear1  = Val($date1[0])
    $_intMonth1 = Val($date1[1])
    $_intDay1   = Val($date1[2])

    If $_intMonth1 < 3
        $_intMonth1 = $_intMonth1 + 12
        $_intYear1  = $_intYear1  - 1
    EndIf

    $_intDate1 = $_intDay1 + ( 153 * $_intMonth1 - 457 ) / 5 + 365 * $_intYear1 +
                 $_intYear1 / 4 - $_intYear1 / 100 + $_intYear1 / 400 - 306

    Select

        Case VarType($DateOrMod) = 3

            $_intDate2  = $_intDate1 + $DateOrMod
            If InStr($_intDate2,'-') $_intDate2 = Val(SubStr($_intDate2,2,Len($_intDate2)-1)) EndIf

            $_intYear2  = ( 100 * ( ( ( 100*($_intDate2+306)-25)/3652425)
                                     - ( ((100*($_intDate2+306)-25)/3652425)/4)
                                   ) + (100*($_intDate2+306)-25)
                           ) / 36525

            $_intMonth2 = (   5 * ( ( ( 100*($_intDate2+306)-25)/3652425)
                                     - ( ((100*($_intDate2+306)-25)/3652425)/4)
                                     + ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
                                   ) + 456
                           ) / 153

            $_intDay2   = ( ( ( 100*($_intDate2+306)-25)/3652425)
                                     - ( ((100*($_intDate2+306)-25)/3652425)/4)
                                     + ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
                           )         - ( 153 * $_intMonth2 - 457
                                        ) / 5

            If $_intMonth2 > 12 $_intYear2 = $_intYear2 + 1 $_intMonth2 = $_intMonth2 - 12 EndIf

            If not $SingleDigit
                If Len($_intYear2 ) < 4
                    $_ = Execute("for $i=1 to 4-len($$_intYear2) $$_intYear2 = '0' + $$_intYear2 next")
                EndIf
                $_intMonth2 = right("0" + $_intMonth2,2)
                $_intDay2   = right("0" + $_intDay2,2)
            EndIf

            $DateCalc = '' + $_intYear2 + '/' + $_intMonth2 + '/' + $_intDay2

        Case VarType($DateOrMod) = 8

            $DateOrMod = Split($DateOrMod,'/')

            If Ubound($DateOrMod) <> 2
                Exit 1
            EndIf

            $_intYear2  = Val($DateOrMod[0])
            $_intMonth2 = Val($DateOrMod[1])
            $_intDay2   = Val($DateOrMod[2])

            If $_intMonth2 < 3
                $_intMonth2 = $_intMonth2 + 12
                $_intYear2  = $_intYear2  - 1
            EndIf

            $_intDate2 = $_intDay2 + ( 153 * $_intMonth2 - 457 ) / 5 + 365 * $_intYear2 +
                         $_intYear2 / 4 - $_intYear2 / 100 + $_intYear2 / 400 - 306

            $DateCalc  = $_intDate1 - $_intDate2

            ;comment the next line if you wish to return negative results also !!!
            If InStr($DateCalc,'-') $DateCalc = Val(SubStr($DateCalc,2,Len($DateCalc)-1)) EndIf

        Case 1

            Exit 1

    EndSelect

EndFunction

---------------------------
Code Ends

post edited by NTDoc to add CODE TAGS


Edited by NTDOC (2007-06-20 06:17 PM)

Top
#177095 - 2007-06-20 06:04 PM Re: Script to display Account Expiration date to user. [Re: RFW]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Might be able to use ADSI for this, example:

 Code:
Break On

$= SetOption("WrapAtEol","On")

$User = GetObject("WinNT://@LDOMAIN/@USERID,user")

If @ERROR = 0

 $Date = $User.PasswordExpirationDate

 If @ERROR = 0

  ?"Your password will expire on " + $Date

 Endif

Endif

If @ERROR

  ? "Error @ERROR : @SERROR"

Endif

Exit 0



Top
#177098 - 2007-06-20 07:13 PM Re: Script to display Account Expiration date to user. [Re: Shawn]
RFW Offline
Fresh Scripter

Registered: 2006-04-19
Posts: 9
Loc: New York
Don't need password expiration date. We are using the Account Expiration date, which seems to the big problem. Normal users can't run the NET USER command (due to our security policies).
Top
#177100 - 2007-06-20 07:24 PM Re: Script to display Account Expiration date to user. [Re: RFW]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
According to the Scripting Guy article linked below there also is a property AccountExpirationDate.

I don't have AD here @ home so I cant test but as far as I can see this should work (altered Shawn's code a bit).

 Code:
Break On

$= SetOption("WrapAtEol","On")

$User = GetObject("WinNT://@LDOMAIN/@USERID,user")

If @ERROR = 0
	$Date = $User.AccountExpirationDate
	If @ERROR = 0
		?"Your account will expire on " + $Date
	EndIf
EndIf

If @ERROR
	? "Error @ERROR : @SERROR"
EndIf

Exit 0

Hey, Scripting Guy! How Can I Return a List of All the Users Whose User Account Never Expires?


Edited by Mart (2007-06-20 07:25 PM)
Edit Reason: D#mn those typo's.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#177101 - 2007-06-20 07:51 PM Re: Script to display Account Expiration date to user. [Re: Mart]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
You will probably need to use LDAP for that bit of code. I don't have AD @ HOME either - this bit of code is totally un-tested - its just a quick conversion ...

 Code:
Break On

$= SetOption("WrapAtEol","On")

$objSysInfo = CreateObject("ADSystemInfo") 

$strUser = $objSysInfo.UserName 

$objUser = GetObject("LDAP://" + $strUser) 

?"Your account expires on " + $objUser.AccountExpirationDate + "." 

Exit 0


Top
#177102 - 2007-06-20 08:13 PM Re: Script to display Account Expiration date to user. [Re: Shawn]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Shame on you Shawn! No code tags anymore? \:o
Nah just yanking your chain ;\)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#177103 - 2007-06-20 10:43 PM Re: Script to display Account Expiration date to user. [Re: Mart]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
lol, i know your joking. Looks like someone fixed me up with code tags though, hmmmm wonder who ...

-Shawn

Top
#177139 - 2007-06-21 07:34 PM Re: Script to display Account Expiration date to user. [Re: Shawn]
RFW Offline
Fresh Scripter

Registered: 2006-04-19
Posts: 9
Loc: New York
Shawn, I think you are on the right path with the LDAP (as this is an Win2K3 network). When running the following in debug, everything seems to be working good (variables getting filled correctly, or so it seems) except for the $objUser.AccountExpirationDate, nothing there. I really appreciate the assistance in this, I just wish I was better adapted to deal with the whole AD crap. Thanks again.

 Code:
Break On

$= SetOption("WrapAtEol","On")

$objSysInfo = CreateObject("ADSystemInfo") 

$strUser = $objSysInfo.UserName 

$objUser = GetObject("LDAP://" + $strUser) 

?"Your account expires on " + $objUser.AccountExpirationDate + "." 

Exit 0

[/quote]

Top
#177140 - 2007-06-21 07:40 PM Re: Script to display Account Expiration date to user. [Re: RFW]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
After this line:

?"Your account expires on " + $objUser.AccountExpirationDate + "."

Put this line:

? "ERROR @ERROR : @SERROR"

What error you getting ?

Top
#177141 - 2007-06-21 08:07 PM Re: Script to display Account Expiration date to user. [Re: Shawn]
RFW Offline
Fresh Scripter

Registered: 2006-04-19
Posts: 9
Loc: New York
This is what is displayed:

Error 6: The handle is invalid.

Thanks

Top
#177142 - 2007-06-21 08:13 PM Re: Script to display Account Expiration date to user. [Re: RFW]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
What about the line before that. Is that returning success ?
Top
#177143 - 2007-06-21 08:19 PM Re: Script to display Account Expiration date to user. [Re: Shawn]
RFW Offline
Fresh Scripter

Registered: 2006-04-19
Posts: 9
Loc: New York
-2147463168 : An invalid directory pathname was passed
Top
#177144 - 2007-06-21 08:44 PM Re: Script to display Account Expiration date to user. [Re: RFW]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
So if you trace-out the value of $strUser ?
Top
#177145 - 2007-06-21 09:49 PM Re: Script to display Account Expiration date to user. [Re: Shawn]
RFW Offline
Fresh Scripter

Registered: 2006-04-19
Posts: 9
Loc: New York
$strUser CN=Wolf\, Robert F Contr 914 CS/SCBN,OU=NCC Users,OU=NCC,OU=Niagara Falls,DC=reserves,DC=afrc,DC=ds,DC=af,DC=mil

It seems to be the

$objUser = GetObject("LDAP://IAGDC10/" + $strUser)

I was suppose to put my DC in the LDAP, correct?

That is when I get the error.

Top
#177146 - 2007-06-21 10:04 PM Re: Script to display Account Expiration date to user. [Re: RFW]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
As far as I know, shouldn't have to specify anything other than:

$objUser = GetObject("LDAP://" + $strUser)

The domain info is already part of the path. You get the same error with this code as well ?

-Shawn

Top
#177147 - 2007-06-21 10:57 PM Re: Script to display Account Expiration date to user. [Re: Shawn]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I'm guessing it's choking on the CN because of the whacks, hacks, and comma. I understand the hack before the comma is supposed to escape it.

Have you tried this in VBS?

Oh, and don't forget that if you don't set an expiry on the account you will get an error albeit a different number.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#177156 - 2007-06-22 04:23 AM Re: Script to display Account Expiration date to user. [Re: Les]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Finally got a chance to test this code on our domain. I have two accounts

1) Account#1 is a Domain Admin account that DOES have an expiry

2) Account#2 is a Domain User account that DOES NOT have an expiry

Running as Account#2 (the Domain User) - if I query my own ExpiryDate (should be none) - I get the following error:

COM exception error "AccountExpirationDate" ((null) - (null)) [-2147352567/80020
009]

So I guess this error means there is no expiry date. And you can test for it. However, if I query the status of my Domain Admin account - still running as Account#2 (Domain User) mind you, I get the following:

Your account expires on 11/1/2007

So it looks like it works ok - querying ExpiryDate as a normal user that is.




Top
#177223 - 2007-06-25 03:02 PM Re: Script to display Account Expiration date to user. [Re: Shawn]
RFW Offline
Fresh Scripter

Registered: 2006-04-19
Posts: 9
Loc: New York
Still nothing Shawn. Both my Admin and Normal user account have expirations. Can you post the actual code you used to get your results? Thanks.
Top
#177225 - 2007-06-25 03:09 PM Re: Script to display Account Expiration date to user. [Re: RFW]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
I set an expirydate on my Domain User account. Then logged into the domain with this account and ran this:

 Code:
Break On

$= SetOption("WrapAtEol","On")

$objSysInfo = CreateObject("ADSystemInfo") 

$strUser = $objSysInfo.UserName 

$objUser = GetObject("LDAP://"+$strUser)

?"Your account expires on " + $objUser.AccountExpirationDate + "." 

? @SERROR

Exit 0


The result was:

M:\>kix32 test

Your account expires on 1/1/2008 1:00:00 AM.
The operation completed successfully.

Top
#177226 - 2007-06-25 03:15 PM Re: Script to display Account Expiration date to user. [Re: Shawn]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Anyone available to test this code in their domain ? With an account that has an expiry ?
Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 778 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.079 seconds in which 0.03 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org