#190238 - 2008-10-22 01:15 PM
checking expired user
Saleem
Hey THIS is FUN
Registered: 2001-04-11
Posts: 280
Loc: UAE
I have a script to check expired user in my Domain. I am checking expired user <= the day I run the script, Now I need to modify the script to display the user expired 30 days before the run date ie: today is 22/10/2008 I need to display user expired on 22/09/2008 or before Please find the current script:
Break ON
; $null = RedirectOutput ( "D:\expired.txt" )
$pcpool = GetObject ( "WinNT://@Domain" ); Connect to domain
If @ ERROR = 0 AND VarType ( $pcpool ) = 9 ; Confirm connection
For Each $user In $pcpool
If $user .class= "User"
$= Execute ( "Exit 0" ) ; Reset error flag
$Expired = $User . AccountExpirationDate
If NOT @ ERROR
; Reformat date to suitable ( from European format )
$aDate = Split ( $Expired , "/" )
$dt1 =@ date
$syy = Left ( $dt1 , 4 )
$smm = SubStr ( $dt1 , 6 , 2 )
$sdd = SubStr ( $dt1 , 9 , 2 )
$sresult = $syy + $smm + $sdd
If CStr ( $aDate [ 2 ])+ $aDate [ 1 ]+ $aDate [ 0 ] <= $sresult
? $user . name + " " + $Expired
EndIf
EndIf
EndIf
Next
Else
? @ ERROR
?
? @ SERROR
EndIf
Edited by Saleem (2008-10-22 01:17 PM )
_________________________
“I’ll not change you unless you don’t have intention to change yourself”
--H:Quran
Top
#190239 - 2008-10-22 01:25 PM
Re: checking expired user
[Re: Saleem ]
Mart
KiX Supporter
Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
You could use the DateCalc() UDF like shown below.UDF Library » DateCalc() - Calculates Days between or returns calculated Date (Days as modifier)
Break on
;use positive to look into the future.
;use negative to look at the past.
$mod = -30
? ? "in/before $mod day(s) it was/will be " + DateCalc(@date, $mod)
Sleep 5
;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
_________________________
Mart - Chuck Norris once sold ebay to ebay on ebay.
Top
#190244 - 2008-10-22 04:03 PM
Re: checking expired user
[Re: Mart ]
Glenn Barnas
KiX Supporter
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Here's a tool that I use once a week to email a report on expiring/expired passwords to the help desk - just change the MaxDays value to 30 and config the INI file with the mail info. This app uses Blat for sending mail.
;; KixGenerated: 2008/10/22 10:00:32
Break On
Dim $Members , $Member ; array of domain user members, enumerator var
Dim $I ; index var
Dim $objUser ; AD user query object pointer
Dim $Flags ; user flags
Dim $ExpDate , $Days ; expiration date, days remaining
Dim $LStat , $EStat ; locked and Expired status flags
Dim $RptData [0 ], $R ; report array, record pointer
Dim $Header ; report page header
Dim $MaxDays ; max days before expiration
Dim $MailTo , $MailSender , $MailServer ; mail parameters
Dim $BlatCmd ; Blat command string
$MaxDays = 10 ; passwords expiring within 10 days
; get array of user accounts
'Collecting'
$Members = GroupMembers(@DOMAIN , 'Domain Users' , 2 )
Chr (13 ) 'Reporting ' ?
$R = -1
; header
;00000000011111111112222222222333333333344444444445555555555666666666677777777778
;12345678901234567890123456789012345678901234567890123456789012345678901234567890
$Header = 'UserID UserName Locked? Expired?' + @CRLF
$Header = $Header + ' Expiration date' + @CRLF
$Header = $Header + '===============================================================================' + @CRLF
For Each $Member In $Members
; get specific user data
$objUser = GetObject ('WinNT://' + @DOMAIN + '/' +$Member + ',user' )
; read flags
$Flags = $objUser .UserFlags
; if not disabled, and not Never Expires, report on expired or about to expire accounts
If Not $objUser .AccountDisabled And Not ($Flags & 65536 )
$ExpDate = FixDate($objUser .PasswordExpirationDate)
$Days = Int (TimeDiff('Now' , $ExpDate , 'D' ))
$LStat = $objUser .IsAccountLocked
$EStat = $objUser .PasswordExpired
If $EStat Or $LStat Or $Days < 10
$R = $R + 1
ReDim Preserve $RptData [$R ]
$RptData [$R ] = Left ($Member + ' ' , 15 )
$RptData [$R ] = $RptData [$R ] + Left ($objUser .FullName + ' ' , 40 )
; display LOCKED and EXPIRED status
If $LStat
$RptData [$R ] = $RptData [$R ] + 'LOCKED '
Else
$RptData [$R ] = $RptData [$R ] + ' '
EndIf
If $EStat
$RptData [$R ] = $RptData [$R ] + 'EXPIRED ' + @CRLF + ' ' + $ExpDate
Else
; get remaining days
If $Days < $MaxDays
$RptData [$R ] = $RptData [$R ] + $Days + ' REMAIN ' + @CRLF + ' ' + $ExpDate
Else
$RptData [$R ] = $RptData [$R ] + ' '
EndIf
EndIf
If Exist ('.\debug.txt' )
$RptData [$R ] ?
$I = $I + 1
If $I > 100 Quit 0 EndIf
EndIf
EndIf
EndIf
Next
Del '%S_CONFIG%\Logs\ExpiredPW.txt' ; remove prior report
$R = RedirectOutput ('%S_CONFIG%\Logs\ExpiredPW.txt' ) ; create new report file
$Header ; output the header
For $R = 0 to UBound ($RptData )
If $R Mod 52 = 0 And $R > 0
Chr (12 ) $Header ; output a formfeed and header every 52 records
EndIf
$RptData [$R ] ?
Next
$R = RedirectOutput ('' ) ; close the report file
$MailTo = ReadProfileString ('%S_CONFIG%\lockout.ini' , 'MAIL' , 'MailTo' )
$MailSender = ReadProfileString ('%S_CONFIG%\lockout.ini' , 'MAIL' , 'MailSender' )
$MailServer = ReadProfileString ('%S_CONFIG%\lockout.ini' , 'MAIL' , 'MailServer' )
; send the report via email (BLAT)
If $MailTo <> ''
$BlatCmd = '%COMSPEC% /c %S_BIN%\blat.exe %S_CONFIG%\Logs\ExpiredPW.txt'
$BlatCmd = $BlatCmd + ' -to ' + $MailTo
$BlatCmd = $BlatCmd + ' -subject "Expiring Passwords"'
$BlatCmd = $BlatCmd + ' -f ' + $MailSender
$BlatCmd = $BlatCmd + ' -server ' + $MailServer
$BlatCmd = $BlatCmd + ' >NUL:'
Shell $BlatCmd
EndIf
Exit 0
; ================================================================================
; correct the date format - change from "11/1/2007 2:31:30 PM" to "2007/11/01 14:31:30"
Function FixDate($_Date )
Dim $_ODate
Dim $_Tmp
$_ODate = Split ($_Date , ' ' )
; Change the date format
$_Tmp = Split ($_ODate [0 ], '/' )
$_ODate [0 ] = $_Tmp [2 ] + '/' + Right ('0' + $_Tmp [0 ], 2 ) + '/' + Right ('0' + $_Tmp [1 ], 2 )
; Change the time format
$_Tmp = Split ($_ODate [1 ], ':' )
If $_ODate [2 ] = 'PM' And Val ($_Tmp [0 ]) < 12
$_Tmp [0 ] = Right ('0' + (Val ($_Tmp [0 ]) + 12 ), 2 )
Else
$_Tmp [0 ] = Right ('0' + $_Tmp [0 ], 2 )
EndIf
$_ODate [1 ] = Join ($_Tmp , ':' )
$FixDate = $_ODate [0 ] + ' ' + $_ODate [1 ]
Exit 0
EndFunction
;
;FUNCTION GroupMembers
;
;ACTION Returns an array of all group members of the specified group
;
;SYNTAX GroupMembers(Target, Group, [FLAG])
;
;PARAMETERS Target
; The Domain name or Workstation to work with. For faster workstation
; execution, include the Domain Name that the workstation is a meber of.
;
; "Kixtart/beanbag" would be working with the workstation Beanbag in the
; Kixtart domain
;
; Group
; The Group you want to query
;
; [FLAGS]
; To use the flags options add the numbers of the desired flags toghthers and
; Use that number in the flag field.
;
; Filter :(only one filter flag at a time please)
; 1 = all
; 2 = Users only
; 4 = Groups only
;
; ADSI Information(return ADSI information "pick only one")
; 8 = ADSPath field
; 16 = ADSI Object Handle
;
;RETURNS an array containing , if the ADSPath option is used the ADSPath
; will also be returned |.
;
;REMARKS ADSI com object must be installed.
;
;EXAMPLES ;this return all members of the Domain Admins group in the kixtart domain.
; $members = groupmembers("kixtart","Domain admins")
;
; ;this will will return all groups in the local administrators group on
; ;the Workstation beanbad in the kixtart domain. Also the
; $groups = groupmembers("kixtart/beanbag","Administratoos","group")
Function Groupmembers($target , $group , optional $flag )
DIM $temparray [8 ], $member , $i , $chunk , $flag , $ADSIFlag , $filterFlag
$chunk = ubound ($temparray )
$flag = val ($flag )
$i = 0
$group = getobject ("WinNT://" + $target + "/" + $group )
if vartype ($group ) <> 9 exit (@error ) endif
select
case $flag & 1
$filterflag = 1
case $flag & 2
$filterflag = 2
case $flag & 4
$filterflag = 4
case 1
$filterflag = 1
endselect
select
case $flag & 8
$ADSIFlag = 8
case $flag & 16
$ADSIFlag = 16
endselect
for each $member in $group .members
select
case $filterflag = 2 AND $member .class = "user"
if substr ($member .name,len ($member .name),1 ) <> Chr (36 )
$temparray [$i ] = $member .name
select
case $adsiflag = 8
$temparray [$i ] = $member .adspath
case $adsiflag = 16
$temparray [$i ] = $member
endselect
$i = $i + 1
endif
case $filterflag = 4 AND $member .class = "Group"
if substr ($member .name,len ($member .name),1 ) <> Chr (36 )
$temparray [$i ] = $member .name
select
case $adsiflag = 8
$temparray [$i ] = $member .adspath
case $adsiflag = 16
$temparray [$i ] = $member
endselect
$i = $i + 1
endif
case $filterflag = 1
if substr ($member .name,len ($member .name),1 ) <> Chr (36 )
$temparray [$i ] = $member .name
select
case $adsiflag = 8
$temparray [$i ] = $member .adspath
case $adsiflag = 16
$temparray [$i ] = $member
endselect
$i = $i + 1
endif
case $filterflag
;bit bucket
endselect
if $i = ubound ($temparray )
redim preserve $temparray [Ubound ($temparray )+$chunk ]
endif
next
if $i <> 0
redim preserve $temparray [$i -1 ]
$groupmembers =$temparray
endif
endfunction
;;
;;======================================================================
;;
;;FUNCTION TimeDiff()
;;
;;AUTHOR Glenn Barnas
;;
;;VERSION 2.2 / 2007/10/14
;; Modified to increase accuracy, permit fracional second calculations
;; 2.1 / 2007/03/17
;; added "now" and "today" options for both start and end times
;; 2.0 / 2006/11/20
;; Changes for code efficiency; added defaults for midnight
;;
;;ACTION Calculates the time difference between two given date/time strings
;;
;;SYNTAX TimeDiff(Start [, End] [, Format] [, MSec])
;;
;;PARAMETERS Start - REQUIRED, String value representing the start timestamp
;; Format yyyy/mm/dd hh:mm:ss
;;
;; End - OPTIONAL, Defaults to "now"
;; String value representing the ending time
;; Format yyyy/mm/dd hh:mm:ss
;; Can be the special value "now" for the current date/time, or "today"
;; for midnight of the current day.
;;
;; When the time value is not specified, it defaults to 00:00:00.000 (midnight)
;;
;; Format - OPTIONAL, one of:
;; "m" - return minutes
;; "h" - return hours
;; "d" - return days
;; "y" - return years
;; When a format value is specified, it returns the fractional part (ie 0.5 days for 12 hours).
;;
;; MSec - OPTIONAL, True if the fractional seconds should be returned. Default
;; is false, returning whole seconds, to maintain compatibility with earlier versions.
;; MSec only affects the return of fractional seconds, not fractional parts of other time formats.
;;
;;REMARKS Returns a value representing the difference in time between two date/time
;; strings. Assumes that "Start" is in the past, but will properly return a
;; negative value if it is in the future.
;;
;;RETURNS Double - difference between Start and End timestamps in seconds
;;
;;DEPENDENCIES None
;;
;;TESTED WITH Kix 4.2+, NT4, W2K, WXP, W2K3
;;
;;EXAMPLES If TimeDiff(GetFileTime('SomeFile.txt'), 'now', 'h') > 48
;; "File is more than 2 days old!" ?
;; EndIf
;
Function TimeDiff($_Start , OPTIONAL $_End , OPTIONAL $_Fmt , OPTIONAL $_MSec )
Dim $_ , $_SDate , $a_Start , $_EDate , $a_End , $_Duration
; Check for special START parameters
Select
Case $_Start = 'now'
$_Start = @DATE + ' ' + @TIME + '.' + @MSECS
Case $_START = 'today'
$_Start = @DATE + ' 00:00:00.000'
EndSelect
; Check for special END parameters
Select
Case $_End = 'now' Or $_End = ''
$_End = @DATE + ' ' + @TIME + '.' + @MSECS
Case $_End = 'today'
$_End = @DATE + ' 00:00:00.000'
EndSelect
; Validate parameters
; Parameters passed are "yyyy/mm/dd hh:mm:ss[.sss]" - make sure the default time is added
$a_Start = Split (Join (Split (Join (Split ($_Start + ' 00:00:00.000' , '/' ), ' ' ), ':' ), ' ' ), ' ' , 6 )
If UBound ($a_Start ) <> 5 Exit 87 EndIf ; bad start time parameter
For $_ = 0 to 5
$a_Start [$_ ] = CDbl ($a_Start [$_ ]) ; convert to numeric values
Next
$a_End = Split (Join (Split (Join (Split ($_End + ' 00:00:00.000' , '/' ), ' ' ), ':' ), ' ' ), ' ' , 6 )
If UBound ($a_End ) <> 5 Exit 87 EndIf ; bad start time parameter
For $_ = 0 to 5
$a_End [$_ ] = CDbl ($a_End [$_ ]) ; convert to numeric values
Next
; Convert dates to Days, then convert to seconds and add the time value
If $a_Start [1 ] < 3
$a_Start [1 ] = $a_Start [1 ] + 12
$a_Start [0 ] = $a_Start [0 ] - 1
EndIf
$_SDate = $a_Start [2 ] + ( 153 * $a_Start [1 ] - 457 ) / 5 + 365 * $a_Start [0 ] + $a_Start [0 ] / 4 - $a_Start [0 ] / 100 + $a_Start [0 ] / 400 - 306
$_SDate = CDbl ($_SDate ) * 86400.0
$_SDate = $_SDate + $a_Start [3 ] * 3600 + $a_Start [4 ] * 60 + $a_Start [5 ]
If $a_End [1 ] < 3
$a_End [1 ] = $a_End [1 ] + 12
$a_End [0 ] = $a_End [0 ] - 1
EndIf
$_EDate = $a_End [2 ] + ( 153 * $a_End [1 ] - 457 ) / 5 + 365 * $a_End [0 ] + $a_End [0 ] / 4 - $a_End [0 ] / 100 + $a_End [0 ] / 400 - 306
$_EDate = CDbl ($_EDate ) * 86400.0
$_EDate = $_EDate + $a_End [3 ] * 3600 + $a_End [4 ] * 60 + $a_End [5 ]
; Get the duration between the timestamps
$_Duration = CDbl ($_EDate - $_SDate )
; Trim fractional seconds if the MSec flag wasn't set
; Value returned is whole seconds
If Not $_MSec
$_Duration = CInt ($_Duration )
EndIf
; Return data as a Double - seconds (default), hours, minutes, days, or years
Select
Case $_Fmt = 'm' ; minutes
$TimeDiff = $_Duration / 60.0
Case $_Fmt = 'h' ; hours
$TimeDiff = $_Duration / 3600.0
Case $_Fmt = 'd' ; days
$TimeDiff = $_Duration / 86400.0
Case $_Fmt = 'y' ; years
$TimeDiff = $_Duration / 31536000.0
Case 1
$TimeDiff = $_Duration
EndSelect
Exit 0
EndFunction
_________________________
Actually I
am a Rocket Scientist!
Top
#190265 - 2008-10-23 10:11 AM
Re: checking expired user
[Re: Mart ]
Saleem
Hey THIS is FUN
Registered: 2001-04-11
Posts: 280
Loc: UAE
the one which is not set I am getting emty, but if I search by following:
Break ON
; $null = RedirectOutput ( "D:\expired.txt" )
$pcpool = GetObject ( "WinNT://@domain" )
If @ ERROR = 0 AND VarType ( $pcpool ) = 9
For Each $user In $pcpool
If $user .class= "User"
$= Execute ( "Exit 0" )
If NOT @ ERROR
If Left ( $user . name , 1 )= "9"
If User . AccountExpirationDate = "" ;? $user . name " " $User . AccountExpirationDate
? $user . name " " $User . AccountExpirationDate
? @ SERROR
EndIf
EndIf
EndIf
EndIf
Next
Else
? @ ERROR
?
? @ SERROR
EndIf
I am not getting any result. please help
_________________________
“I’ll not change you unless you don’t have intention to change yourself”
--H:Quran
Top
Moderator: Jochen , Allen , Radimus , Glenn Barnas , ShaneEP , Ruud van Velsen , Arend_ , Mart
0 registered
and 671 anonymous users online.