Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Update HomeDirectory Using Win32::Ole

by Anonymous Monk
on Jun 06, 2005 at 23:05 UTC ( #464106=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Perl Version: Activestate 5.6.1, Environment: Windows 2003 running NTEmulation

Problem:

I'm trying to convert some VBScript code to Perl. Specifically, I'm trying to change the home directory path for some users.

Vbscript Code:

Set User = GetObject("WinNT://Domain/" & username & ",user") User.HomeDirectory = "\\server\homeshare" User.SetInfo

Perl Code:

$User=Win32::OLE-> GetObject("WinNT://wsi/$username", "user"); #Reports error - not allowed $User->HomeDirectory="\\server\homeshare" +; #Doesn't Work $update=$User->HomeDirectory("\\server\homeshare"); $set=$update->SetInfo;

How should the vbscript function, User.HomeDirectory = "\\server\homeshare" be written in Perl?

Any help would be much appreciated.

Replies are listed 'Best First'.
Re: Update HomeDirectory Using Win32::Ole
by ikegami (Pope) on Jun 07, 2005 at 01:01 UTC

    A found a few problems.

    According to you VB snippet, ",user" is appended to the first string; it's not a seperate argument.
    Win32::OLE->GetObject("WinNT://wsi/$username", "user");
    should be
    Win32::OLE->GetObject("WinNT://wsi/$username,user");

    Don't forget to double backslashes in hard-coded strings.
    "\\server\homeshare"
    should be
    "\\\\server\\homeshare"

    HomeDirectory is proably a property, not a method, so
    $User->HomeDirectory("\\server\homeshare");
    should be
    $User->{HomeDirectory} = "\\\\server\\homeshare";

    Unfortunately, that doesn't seem to be enough.

    use strict; use warnings; use Win32::OLE (); my $user_name = 'Administrator'; my $user = Win32::OLE->GetObject("WinNT://wsi/$user_name,user") or die("\$user is undef\n"); # <---- dies here print($User->{HomeDirectory}, "\n"); # $user->{HomeDirectory} = "\\\\server\\homeshare"; # $user->SetInfo;

    But that's probably cause I'm not running a compatible platform. MSDN says this interface is only available on Windows 2000 Server and Windows Server 2003, which I don't have.

      HomeDirectory is proably a property, not a method, so
      $User->HomeDirectory("\\server\homeshare");
      should be
      $User->{HomeDirectory} = "\\\\server\\homeshare";
      Win32::OLE uses a lot of AUTOLOAD magic, so both might be exchangeable. Well, at least, I do think the accessor version of the property without parameters, is equivalent to just reading the property:
      $home = $User->HomeDirectory();
      vs.
      $home = $User->{HomeDirectory};
Re: Update HomeDirectory Using Win32::Ole
by johnnywang (Priest) on Jun 07, 2005 at 06:11 UTC
    The module Win32::NetAdmin has a method
    UserSetAttributes(server, userName, password, passwordAge, privilege, +homeDir, comment, flags, scriptPath)
    for doing that (the homeDir parameter).
Re: Update HomeDirectory Using Win32::Ole
by DaWolf (Curate) on Jun 07, 2005 at 09:06 UTC
    I've noticed that you have a blank space in your perl code:

    So, where you have:
    $User=Win32::OLE-> GetObject("WinNT://wsi/$username", "user");
    It should be:
    $User=Win32::OLE->GetObject("WinNT://wsi/$username", "user");
    Just my 2 cents.
Re: Update HomeDirectory Using Win32::Ole
by Anonymous Monk on Jun 07, 2005 at 16:38 UTC
    Thanks everyone for your help. The below code works:

    $User=Win32::OLE->GetObject("WinNT://Domain/$user", "user"); $User->{HomeDirectory}="$homedir"; $user->SetInfo;
    Thanks again.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://464106]
Approved by moot
Front-paged by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2021-03-07 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (122 votes). Check out past polls.

    Notices?