NTDOC
Administrator
Registered: 2000-07-28
Posts: 11625
Loc: CA
Okay Matthewst, Here is some example code WARNING! USE AT YOUR OWN RISK!! This UDF does not check to verify if the remote machine is running or if you have Admin rights on the system which you must have. It does not check who is logged on or if anyone is accessing any of the files within the profile. It simply checks the last write time against the current time and determines how old the NTUSER.DAT file is. You can set the age to any date you want for removing the profiles.PARAMETERS $Days = Number of days before the profile will be considered OLD$sComputer = The name of the remote computer, if left blank the current machine is used$Del = Flag to indicate if you want to remove the profile or simply display the profile. A 1 would mean to delete the profile. Again - Test and Use at your own risk I highly recommend using a Virtual machine or other Test machine first to verify this code operates as expected. I have done minimal testing and it appears to work, but due to the nature of this type of code any errors could be quite dangerous. Break On Dim $SO $SO =SetOption ('Explicit' ,'On' )$SO =SetOption ('NoVarsInStrings' ,'On' )$SO =SetOption ('NoMacrosInStrings' ,'On' ) RemoveOldProfiles (90 ,'SomeRemoteComputer' ,0 );change the 0 to a 1 and uncomment code below where shown to actually remove the profiles Function RemoveOldProfiles($Days , Optional $sComputer , $Del ) Dim $ProfilePath , $ProfileList , $PL Dim $SystemRoot , $SystemDrive , $Age $sComputer =IIf (Not $sComputer ,'' ,'\\' +Join (Split ($sComputer ,'\' ),'' ,3 )+'\' ) $ProfilePath = Split (ReadValue ($sComputer +'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' ,'ProfilesDirectory' ),'\' )[1 ] $SystemRoot = ReadValue ($sComputer +'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion' ,'SystemRoot' ) $SystemDrive = Left ($SystemRoot ,1 ) Select Case $sComputer = "" ; LOCAL COMPUTER If InStr ($ProfilePath ,'Documents and Settings' ) $ProfileList = ExpandEnvironmentVars (ReadValue ('HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' ,'ProfilesDirectory' )) $PL = Dir ($ProfileList ) While $PL < >'' If $PL < >'' If Not InStr ($PL ,'..' ) And Not InStr ($PL ,'All Users' ) And Not InStr ($PL ,'Default User' ) And Not InStr ($PL ,'LocalService' ) And Not InStr ($PL ,'NetworkService' ) If Exist ($ProfileList +'\' +$PL +'\' +'NTUSER.DAT' ) $Age = DateCalc (@Date ,SubStr (GetFileTime ($ProfileList +'\' +$PL +'\' +'NTUSER.DAT' ),1 ,10 )) If $Age > $Days '' + $sComputer + $PL + ' : ' + $Age + ' days old.' ? ;To actually remove the profile uncomment the code below 'Would remove profile: ' + $ProfileList +'\' +$PL ? ;If Len($ProfileList+'\'+$PL) >9 ;If $Del = 1 ;RD $ProfileList+'\'+$PL /S ;EndIf ;EndIf EndIf EndIf EndIf EndIf $PL = Dir () Loop Else ; LOCAL COMPUTER If GetFileAttr ($SystemRoot +'\' +'Profiles' ) & 16 $ProfileList = $SystemRoot +'\' +'Profiles' $PL = Dir ($ProfileList ) While $PL < >'' If $PL < >'' If Not InStr ($PL ,'..' ) And Not InStr ($PL ,'All Users' ) And Not InStr ($PL ,'Default User' ) And Not InStr ($PL ,'LocalService' ) And Not InStr ($PL ,'NetworkService' ) If Exist ($ProfileList +'\' +$PL +'\' +'NTUSER.DAT' ) $Age = DateCalc (@Date ,SubStr (GetFileTime ($ProfileList +'\' +$PL +'\' +'NTUSER.DAT' ),1 ,10 )) If $Age > $Days '' + $sComputer + $PL + ' : ' + $Age + ' days old.' ? ;To actually remove the profile uncomment the code below 'Would remove profile: ' + $ProfileList +'\' +$PL ? ;If Len($ProfileList+'\'+$PL) >9 ;If $Del = 1 ;RD $ProfileList+'\'+$PL /S ;EndIf ;EndIf EndIf EndIf EndIf EndIf $PL = Dir () Loop EndIf EndIf Case $sComputer < > "" ; REMOTE COMPUTER If InStr ($ProfilePath ,'Documents and Settings' ) $ProfileList = $sComputer +$SystemDrive +'$' +'\' +'Documents and Settings' $PL = Dir ($ProfileList ) While $PL < >'' If $PL < >'' If Not InStr ($PL ,'..' ) And Not InStr ($PL ,'All Users' ) And Not InStr ($PL ,'Default User' ) And Not InStr ($PL ,'LocalService' ) And Not InStr ($PL ,'NetworkService' ) If Exist ($ProfileList +'\' +$PL +'\' +'NTUSER.DAT' ) $Age = DateCalc (@Date ,SubStr (GetFileTime ($ProfileList +'\' +$PL +'\' +'NTUSER.DAT' ),1 ,10 )) If $Age > $Days '' + $sComputer + $PL + ' : ' + $Age + ' days old.' ? ;To actually remove the profile uncomment the code below 'Would remove REMOTE profile: ' + $ProfileList +'\' +$PL ? ;If Len($ProfileList+'\'+$PL) >9 ;If $Del = 1 ;SHELL '%COMSPEC% /C RD ' + $sComputer+$SystemDrive+'$'+'\'+'DOCUME~1'+'\'+$PL+'\' +' /S /Q' ;'ERROR: ' + @ERROR + ' - ' + @SERROR ? ;EndIf ;EndIf EndIf EndIf EndIf EndIf $PL = Dir () Loop Else ; REMOTE COMPUTER If GetFileAttr ($sComputer +$SystemDrive +'$' +'\' +Right ($SystemRoot ,-3 )+'\' +'Profiles' ) & 16 $ProfileList = $sComputer +$SystemDrive +'$' +'\' +Right ($SystemRoot ,-3 )+'\' +'Profiles' $PL = Dir ($ProfileList ) While $PL < >'' If $PL < >'' If Not InStr ($PL ,'..' ) And Not InStr ($PL ,'All Users' ) And Not InStr ($PL ,'Default User' ) And Not InStr ($PL ,'LocalService' ) And Not InStr ($PL ,'NetworkService' ) If Exist ($ProfileList +'\' +$PL +'\' +'NTUSER.DAT' ) $Age = DateCalc (@Date ,SubStr (GetFileTime ($ProfileList +'\' +$PL +'\' +'NTUSER.DAT' ),1 ,10 )) If $Age > $Days '' + $sComputer + $PL + ' : ' + $Age + ' days old.' ? ;To actually remove the profile uncomment the code below 'Would remove REMOTE profile: ' + $ProfileList +'\' +$PL ? ;If Len($ProfileList+'\'+$PL) >9 ;If $Del = 1 ;SHELL '%COMSPEC% /C RD ' + $sComputer+$SystemDrive+'$'+'\'+Right($SystemRoot,-3)+'\'+'Profiles'+'\'+$PL+'\' +' /S /Q' ;'ERROR: ' + @ERROR + ' - ' + @SERROR ? ;EndIf ;EndIf EndIf EndIf EndIf EndIf $PL = Dir () Loop EndIf EndIf Case 1 ;unable to determine profile location Exit 3 EndSelect EndFunction 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