Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Win32::AdminMisc question 3

Status
Not open for further replies.

Stiddy

IS-IT--Management
Joined
Dec 5, 2001
Messages
781
Location
US
Platform - Windows NT 4.0 server sp6a
Perl version - 5.6.1 build 633

I am looking for a module that can access user account information. I was hoping Dave Roth's Win32::AdminMisc was my answer but have not figured it out yet. I need to query for all users in a domain. Then figure out the expiration date of the account for each user. If the user is not expired, then great, but if so I got to do some work with the account. Any ideas??? Thanks....Oh, I have also already looked into Win32::NetAdmin module, but could not see where it was my answer either...
 
Scratch last comment
Give me 10 mins


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Instead of
$then=$my_userexpdate+(60*24*60*60);
user
$then=$attribs{USER_ACCT_EXPIRES}+(5184000);

and convert from there

60*60*60*24 => 5184000


Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
This should do what you're looking for, again couldn't test because of NON NT+ box on home network

Code:
#!D:/perl/bin/perl -w

use Win32::NetAdmin;
use Win32::AdminMisc;
use Time::Localtime;

#### Get the domain controller for the domain
Win32::NetAdmin::GetDomainController('', 'TADLP0' , $PDC);

#### retrieve all of the local users
Win32::NetAdmin::GroupGetMembers($PDC, 'Students', \@users) or die ("Unable to get users: $!\n");
#### get their attributes and print them
$count = 0;
foreach $user (@users){
    Win32::AdminMisc::UserGetMiscAttributes('',$user,\%attribs) or warn ("Unable to get attrib: $!\n");
    $count++;
    $now=&Get_d8(time);
    $expdate = &Get_d8 ($attribs{'USER_ACCT_EXPIRES'});
    $then=&Get_d8($attribs{'USER_ACCT_EXPIRES'}+5184000);
    print "$user\t$now\t$expdate\t$then\t$count\n";
}
sub Get_d8 {
 my $dte = @_;
 if (defined $dte) {
    @date=localtime($dte);
    $date[5]+=1900;
    $date[4]++;
    return $date[5].sprintf("%0.2d",$date[4]).sprintf("%0.2d",$date[3]);
  } else {
    return "Never Expires";
  }
}

HTH
Paul
 
Now isn't

TWAD4188 will never expire
TWAD4188 20030618 19000100 19700301 1
JMAM1422 20030618 20030817 20031016 2
TGAM4420 20030618 20030822 20031021 3
KSAM8548 20030618 20030826 20031025 4
RMAM0550 20030618 20030618 20030817 5
JSAM6198 20030618 20030801 20030930 6
KHAM9582 20030618 20030823 20031022 7
CTAM5836 20030618 20030618 20030817 8
LDAM3247 20030618 20030729 20030927 9
JVAM7184 20030618 20031121 20040120 10 Paul, got to be proud of that one
DRAM7735 20030618 20030606 20030805 11
AEAM9128 20030618 20030619 20030818 12
JAAM7073 20030618 20030816 20031015 13
ERAM7558 20030618 20030922 20031121 14
CAAM3808 20030618 20030517 20030716 15
SCAM6032 20030618 20030801 20030930 16

such a beautiful thing....HAHAHAHA...YEAAAA...YEEEE, thanks alot Paul. SO I have this...

#### User account expiration date plus sixty days
$then=$attribs{USER_ACCT_EXPIRES}+(5184000);
@then1=localtime($then);
$then1[5]+=1900;
$then1[4]++;
$then_str=$then1[5].sprintf("%0.2d",$then1[4]).sprintf("%0.2d",$then1[3]);
print "$user $now_str $my_userexpdate $then_str $count\n";
}


I am going to work on the actually deleting the user part now. I think that when comparing the dates, something like this will work:


if ($then_str >= $now_str) {
###delete that sucker, he should have renewed his account. Now he has to explain why he forgot to renew. That lazy *&@$%^#!
}


Sorry, lost connectivity for a min. Your privious suggestion worked. I will try your latest in a sec. Thanks Paul...hopefully I can finish from here. But don't be surprised if you see additions to this post. haha. STAR for you...I know that (5184000) is equal to (60*24*60*60) and it works, but what is (60*24*60*60). I can see 60 minutes in an hour * 24 hours * 60 days * what?
 
The other suggestion yeilds:

TWAD4188 19691231 19691231 19691231 1
JMAM1422 19691231 19691231 19691231 2
TGAM4420 19691231 19691231 19691231 3
KSAM8548 19691231 19691231 19691231 4
RMAM0550 19691231 19691231 19691231 5
JSAM6198 19691231 19691231 19691231 6
KHAM9582 19691231 19691231 19691231 7
CTAM5836 19691231 19691231 19691231 8
LDAM3247 19691231 19691231 19691231 9
JVAM7184 19691231 19691231 19691231 10
DRAM7735 19691231 19691231 19691231 11
AEAM9128 19691231 19691231 19691231 12
JAAM7073 19691231 19691231 19691231 13
ERAM7558 19691231 19691231 19691231 14
CAAM3808 19691231 19691231 19691231 15
SCAM6032 19691231 19691231 19691231 16
DHAM8918 19691231 19691231 19691231 17
SLAM5465 19691231 19691231 19691231 18
 
Stiddy,

60 seconds *....

For kicks could you pass me a testfile with usernames, and expirydates as per $attribs{'USER_ACCT_EXPIRE'}

To my twisted mind that code should work, and it will

-Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Stiddy,
after the declaration
Code:
sub Get_d8 {
#$dte=@_;
#use either
#$dte=shift;
#or
{$dte)=@_;

Then it should run OK

Paul
 
PaulTEG,

Here is a bunch of users with their expiration date:

TWAD4188 19000100
JMAM1422 20030817
TGAM4420 20030822
KSAM8548 20030826
RMAM0550 20030618
JSAM6198 20030801
KHAM9582 20030823
CTAM5836 20030618
LDAM3247 20030729
JVAM7184 20031121
DRAM7735 20030606
AEAM9128 20030619
JAAM7073 20030816
ERAM7558 20030922
CAAM3808 20030517
SCAM6032 20030801
DHAM8918 20030909
SLAM5465 20031130
CVAM1718 20030524
SQAM4250 20030922
RMAM6020 20030524
CPAM6342 20031110
MFAM0230 20030801
HFAM6243 20031007
JBAM0219 20030608
WGAM0634 20030627
KAAM3907 20030711
JMAM8709 20030621
RNAM1234 20030702
FHAM9499 20031130
JPAM7297 20031018
KWAM2677 20030601
DMAM8158 20030922
TCAM6268 20030616
JWAM0713 20031128
MJAM1065 20030518
ELAM0574 20031018
DJAD6377 20030530
CSAM6235 20031018
DCAM4110 20031015
ERAM9649 20031106
JHAM7051 20030519

I have not tried your latest suggestions yet
 
Thanks to you both for a really enlightening discussion. My 1/2 pennyworth is to replace
Code:
   @then1=localtime($then);
    $then1[5]+=1900;
    $then1[4]++;
    $then_str=$then1[5].sprintf("%0.2d",$then1[4]).sprintf("%0.2d",$then1[3]);
with
Code:
    my($d,$m,$y)=(localtime($then))[3,4,5];
    $then_str=sprintf('%4d%02d%02d', 1900+$y, 1+$m, $d);
which might be easier to understand in a month or so (been there!).

Yours,




fish

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Stiddy,
with fish's suggestion incorporated.

BTW, I meant with the time as a scalar not the converted string, but it's working now

Code:
#!D:/perl/bin/perl -w

use Win32::NetAdmin;
use Win32::AdminMisc;
use Time::Localtime;

#### Get the domain controller for the domain
Win32::NetAdmin::GetDomainController('', 'TADLP0' , $PDC);

#### retrieve all of the local users
Win32::NetAdmin::GroupGetMembers($PDC, 'Students', \@users) or die ("Unable to get users: $!\n");
### get their attributes and print them

$count = 0;
foreach $user (@users){
    Win32::AdminMisc::UserGetMiscAttributes('',$user,\%attribs) or warn ("Unable to get attrib: $!\n");
    $count++;
    $now=&Get_d8(time);
    $expdate = &Get_d8 ($attribs{'USER_ACCT_EXPIRES'});
    $then=&Get_d8($attribs{'USER_ACCT_EXPIRES'}+5184000);
    print "$user\t$now\t$expdate\t$then\t$count\n";
}

sub Get_d8 {
 my $dte=shift;
 if (defined $dte) {
    my($d,$m,$y)=(localtime($dte))[3,4,5];
    my $date=sprintf('%4d%02d%02d', 1900+$y, 1+$m, $d);
    return $date;
  } else {
    return "Never Expires";
  }
}

Still can't stand over the Win32 - can't test, but I tested the rest

Paul



It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Fish

Re the enlightenment, you're welcome

Paul ;-)
 
---off topic---

I realised recently that I had a bug in my sig, attributing the quote to "Maurice Wilkes, 19". I could teach Alanis Moriset a thing or two about irony...

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Paul and fish,

Code works much faster now. Only having a problem when a users expiration date is set to never expire which as we learned will be equal to 19000100. SO, need to somehow incorporate that value into the script. In an earlierversion I had this:

if ($my_userexpdate == 19000100) {&Never_expires;}
if ($my_userexpdate != 19000100) {
if ($then_str <= $now_str) {
#Win32::NetAdmin::UserDelete($PDC, $user) or warn (&quot;Unable to delete user: $!\n&quot;);
print &quot;$user $drive will be deleted\n&quot;;
} else {print &quot;$user $drive $my_userexpdate $then_str\n&quot;;
}
}
}

sub Never_expires {
print &quot;$user will never expire\n&quot;;
}
####END

The last code:
#!D:/perl/bin/perl -w

use Win32::NetAdmin;
use Win32::AdminMisc;
use Time::Localtime;

#### Get the domain controller for the domain
Win32::NetAdmin::GetDomainController('', 'TADLP0' , $PDC);

#### retrieve all of the local users
Win32::NetAdmin::GroupGetMembers($PDC, 'Students', \@users) or die (&quot;Unable to get users: $!\n&quot;);
### get their attributes and print them

$count = 0;
foreach $user (@users){
Win32::AdminMisc::UserGetMiscAttributes('',$user,\%attribs) or warn (&quot;Unable to get attrib: $!\n&quot;);
$count++;
$now=&Get_d8(time);
$expdate = &Get_d8 ($attribs{'USER_ACCT_EXPIRES'});
$then=&Get_d8($attribs{'USER_ACCT_EXPIRES'}+5184000);
print &quot;$user $now $expdate $then $count\n&quot;;
}

sub Get_d8 {
my $dte=shift;
if (defined $dte) {
my($d,$m,$y)=(localtime($dte))[3,4,5];
my $date=sprintf('%4d%02d%02d', 1900+$y, 1+$m, $d);
return $date;
} else {
return &quot;Never Expires&quot;;
}
}
### END

Had this result:

Use of uninitialized value in addition (+) at A:\paul.pl line 28.
Use of uninitialized value in addition (+) at A:\paul.pl line 28.
Use of uninitialized value in sprintf at A:\paul.pl line 28.
TWAD4188 20030619 19000100 19700301 1
JMAM1422 20030619 20030817 20031016 2
TGAM4420 20030619 20030822 20031021 3
KSAM8548 20030619 20030826 20031025 4
I took out a few
MJAM1065 20030619 20030518 20030717 36
ELAM0574 20030619 20031018 20031216 37
DJAD6377 20030619 20030530 20030729 38
CSAM6235 20030619 20031018 20031216 39
I took out a few
RVAM4558 20030619 20030519 20030718 45
JSAM1839 20030619 20030823 20031022 46
SCAM8108 20030619 20030817 20031016 47
Use of uninitialized value in addition (+) at A:\paul.pl line 28.
Use of uninitialized value in addition (+) at A:\paul.pl line 28.
Use of uninitialized value in sprintf at A:\paul.pl line 28.
RAAC7033 20030619 19000100 19700301 48
HLAM5896 20030619 20030608 20030807 49
 
I think that earlier when we had:
if ($userexpdate[3] eq &quot;&quot;) {$userexpdate[3] = 0;}
I think that resetting the &quot;null&quot; value equal to 0 was the key...so I think that performing the same on the $expdate[3] varible may yeild that results we arte looking for, what do you think?
 
Stiddy,

#!/usr/bin/perl -w
Just take out the -w when you've finished the development, as these are only warnings

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top