Windows Perl Scripting Forums » Perl

help with translating vb script to work in perl script

(9 posts)

  1. cstGirl
    Member

    Hi
    I am a student in a Co-op position and have the great opportunity to learn perl. :)
    one of my tasks is to write a script that will search the user to get the size of the home file and add it to the Users Group to get a total for the month.

    With great effort I have a bit of perl code that retrieves the user name... but am having a difficult time with the rest.

    I have tried to use the GetObject but find that it does not like that call & filter at the same time so I used the createObject.

    Anyway... below is the vb script i am hoping to translate into some perl script that will produce some positive results. If anyone out there has any ideas it would certainly put a big :D on my face!

    ##########VB SCRIPT############################
    Set objUserClass = GetObject("LDAP://schema/user")
    Set objSchemaClass = GetObject(objUserClass.Parent)

    i = 0
    WScript.Echo "Mandatory attributes:"
    For Each strAttribute in objUserClass.MandatoryProperties
    i= i + 1
    WScript.Echo i & vbTab & strAttribute
    Set objAttribute = objSchemaClass.GetObject("Property", strAttribute)
    WScript.Echo " (Syntax: " & objAttribute.Syntax & ")"
    If objAttribute.MultiValued Then
    WScript.Echo " Multivalued"
    Else
    WScript.Echo " Single-valued"
    End If
    Next

    WScript.Echo VbCrLf & "Optional attributes:"
    For Each strAttribute in objUserClass.OptionalProperties
    i=i + 1
    WScript.Echo i & vbTab & strAttribute
    Set objAttribute = objSchemaClass.GetObject("Property", strAttribute)
    WScript.Echo " [Syntax: " & objAttribute.Syntax & "]"
    If objAttribute.MultiValued Then
    WScript.Echo " Multivalued"
    Else
    WScript.Echo " Single-valued"
    End If
    Next
    ########END VB SCRIPT#################


    below is the perl script that I have so far

    # ------ SCRIPT CONFIGURATION ------
    my $strBase = "<LDAP://dc=coe,dc=ads>;"; # BaseDN should be the search base
    my $strFilter = "(&(objectCategory=user)(ObjectClass=user)(samAccountName=katluc));";
    # Valid LDAP search filter
    my $strAttrs = "displayname;"; # Comma-seperated list
    my $strScope = "subtree"; # Should be on of Subtree, Onelevel or Base
    # ------ END CONFIGURATION ---------
    use Win32::OLE;
    $Win32::OLE::Warn = 3;
    my $objConn = Win32::OLE->CreateObject("ADODB.Connection");
    $objConn->{Provider} = "ADsDSOObject";
    $objConn->Open;
    my $objRS = $objConn->Execute($strBase . $strFilter . $strAttrs . $strScope);
    $objRS->MoveFirst;
    while (not $objRS->EOF) {
    print $objRS->Fields(0)->Value,"\n";
    $objRS->MoveNext;
    }
    Posted 12 years ago #
  2. Dave
    Perl guy

    It isn't really clear from your code what you are trying to do since the Perl and VB code use different techiques, yet neither of them do what you said you need. The VB script simply displays what properties a user account can have using LDAP/ADSI but the Perl code attempts to enumerate them using an ADO object.

    I tried to combine your requirements with your code. The first part displays all possible properties a user account can have along with the actual property values for your user.
    The second part displays the user's home directory (assuming that one has been provisioned for him).
    Using this you can determine the user's home directory and scan it for file sizes--I think that this is what you were intending??

    I hope this helps.


    use Win32::OLE qw( in );
    my $UserName = "Dave Roth"; #Note this is the display name, not the SAM account name


    my $Adsi = Win32::OLE->GetObject( "LDAP://RootDse" ) || die; # Locate the root of data tree
    my $DomainContext = $Adsi->Get( "DefaultNamingContext" ) || die; # Determine the default domain name
    my $User = Win32::OLE->GetObject( "LDAP://CN=$UserName,CN=Users, $DomainContext" );

    # Now let's figure out what the schema (aka data format) for user accounts is:
    my $SchemaClassUser = Win32::OLE->GetObject( "LDAP://schema/user" ) || die;

    # For every mandatory property of a user account display it for this user:
    print "**** Mandatory Properties ****\n";
    foreach my $Property ( sort( in( $SchemaClassUser->{MandatoryProperties} ) ) )
    {
       print "$Property: $User->{$Property}\n";
    }

    # For every optional property of a user account display it for this user:
    print "\n\n**** Optional Properties ****\n";
    foreach my $Property ( sort( in( $SchemaClassUser->{OptionalProperties} ) ) )
    {
       print "$Property: $User->{$Property}\n";
    }
    print "\n\n\n\n";

    # Print the user's home directory and drive (if he has any)
    print "$User->{Name}'s home directory is: \"$User->{HomeDirectory}\"\n";
    print "$User->{Name}'s home drive is: \"$User->{HomeDrive}\"\n";

    # For fun, let's see what the user's mailbox database path is (assuming
    # he has one)...
    print "$User->{Name}'s mailbox databse path: \"$User->{homeMDB}\"\n" if( "" ne $User->{homeMDB} );

    Posted 12 years ago #
  3. cstGirl
    Member

    Thank you for your reply, I am sure it will work as long as I can figure why I am having the following issue.

    I think one of my biggest challenges right now is making sure I have the correct modules in place. It seems the download I did has a lot of them missing. Even with installing Perl again as well as downloading the modules from your site & CPAN... I get errors stating that Win32::OLE or Win32::AdminMisc can not be found.

    Is there a simply way to do this download???

    We are running Perl v5.6.
    Would it make a difference if we were to run Perl 5.8??? Is 5.8 compatible with scripts written in 5.6?

    And does it make a difference where we are running our scripts from???

    right now I have Perl in
    C:\Perl

    Should I have a folder called Scripts
    C:\Perl\Scripts
    and run them from there

    or do the scripts need to be with in the
    C:\Perl\lib
    to work properly????

    or if they need Win32::OLE should they be in
    C:\Perl\lib\WIN32
    to run properly?

    so many Questions I know, but your reply is greatly appreciated!!!!

    Sincerly,
    CSTGirl

    [/b]
    Posted 12 years ago #
  4. Dave
    Perl guy

    You can simply download ActivePerl (http://www.activestate.com/) and run the setup application. That will put everything where you need it. OR, you can copy over the perl directory tree from another machine. Either way they are just files on the hard drive. (unless you use PerlScript in .ASP pages then you have to register the PerlScript engine)

    Win32::OLE comes with ActivePerl, so you should not need to install anything special for it. AdminMisc, however, may need to be installed.
    You can install AdminMisc using the PPM utility from a command line:

    c:\perl\bin\ppm.bat -install http://www.roth.net/perl/packages/adminmisc.ppd



    Or you can download it from our FTP site (ftp://ftp.roth.net/pub/ntperl). If you do this, open the .zip file and...
      Create a directory c:\perl\site\lib\auto\win32\adminmisc and copy adminmisc.dll into it

      Copy the AdminMisc.pm file into the c:\perl\site\lib\win32 directory


    You scripts can be anywhere. Once you run the perl.exe it will assume that it can find the package files (.pm files) and libraries (.DLL files) in \perl\site or \perl\lib.
    Posted 12 years ago #
  5. cstGirl
    Member

    Thanks for the information.

    I think I will uninstall Perl then reinstall it through activestate as I did not try active state yet. I copied the Perl directory tree from another machine and I think that might be the problem.

    Thank you so much!

    CSTGirl
    Posted 12 years ago #
  6. cstGirl
    Member

    ok so I reinstalled perl from activestate, then took tried to run this command in many different ways. And what I kept getting for errors is what you see below.
    ######################################

    c:\perl\bin\ppm.bat -install http://www.roth.net/perl/packages/adminmisc.ppd



    PPM> c:\perl\bin\ppm.bat -install http://www.roth.net/perl/packages/adminmisc.pp
    d
    Unknown or ambiguous command 'c:\perl\bin\ppm.bat'; type 'help' for commands.
    PPM> help
    Commands:
    exit - leave the program.
    help [command] - prints this screen, or help on 'command'.
    install PACKAGES - installs specified PACKAGES.
    quit - leave the program.
    query [options] - query information about installed packages.
    remove PACKAGES - removes the specified PACKAGES from the system.
    search [options] - search information about available packages.
    set [options] - set/display current options.
    verify [options] - verifies current install is up to date.
    version - displays PPM version number

    PPM> install c:\perl\bin\ppm.bat -install http://www.roth.net/perl/packages/admi
    nmisc.ppd
    Unknown option: install
    Can't find unicode character property definition via main->e or e.pl at unicode/
    Is/e.pl line 0

    C:\Perl\scripts>install PACKAGES c:\perl\bin\ppm.bat -install http://www.roth.ne
    t/perl/packages/adminmisc.ppd
    'install' is not recognized as an internal or external command,
    operable program or batch file.

    C:\Perl\scripts>ppm
    PPM interactive shell (2.2.0) - type 'help' for available commands.
    PPM>
    PPM> install PACKAGES c:\perl\bin\ppm.bat -install http://www.roth.net/perl/pack
    ages/adminmisc.ppd
    Unknown option: install
    Install package 'PACKAGES?' (y/N): y
    Installing package 'PACKAGES'...
    Error installing package 'PACKAGES': Could not locate a PPD file for package PAC
    KAGES
    PPM>
    ##############################################

    :? :? :? :? :? :? :? :? :?

    ANy ideas????

    thanks
    Posted 12 years ago #
  7. Dave
    Perl guy

    My bad, the command line should be:

    ppm.bat install http://www.roth.net/perl/packages/win32-adminmisc.ppd


    Refer to help for more details (ppm install help).
    Posted 12 years ago #
  8. StarkRavingCalm
    Member

    Dave,
    Should this script just display the names of the properties themselves?
    They all show up as blank as below (random selection):

    extensionAttribute1:
    msExchHomeServerName:
    mail:

    even these show up as blank:
    's home directory is: ""
    's home drive is: ""

    This is being run as domain admin so permissions should not be an issue.

    Posted 9 years ago #
  9. Anonymous

    If you think you are going to be using container and contained objects,[145] you’ll want to either import the in primitive or load Win32::OLE::Enum:

    # 'in' is another way to say Win32::OLE::Enum->All()
    use Win32::OLE qw(in);
    # or
    use Win32::OLE;
    use Win32::OLE::Enum;

    It can also be helpful to load Win32::OLE::Const and use it to import constants from an application or OS library for use in your programs. We’ll see an example of this later in this appendix.

    There are other primitives, such as with and valof, that you might also want to consider importing if you are translating more sophisticated scripts. However, using them typically requires more in-depth knowledge of Windows programming principles. See the Win32::OLE doc for more info on these primitives and their usage.

    Posted 8 years ago #

RSS feed for this topic

Reply

You must log in to post.