[EMAIL PROTECTED] writes:

> Hi Jim,
> 
> I have written some script using WMI, but I don't exactly know how
> to enable file and printer sharing.  Do you have little snippets
> that I can use?  Thanks.

You are in luck; I feel like avoiding real work today.

Attached is a script which creates a network share on a local or
remote system.  For example, if you invoke it like this:

  share.pl --remote myhost C:\Perl perl

...it will export C:\Perl as a share named "perl" on myhost, so that
you can access it as \\myhost\perl.

It uses the Win32_Share class to work its magic; see
<http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_share.asp>.
To decipher the error status codes, follow the link for the "Create"
method.

 - Pat

use warnings;
use strict;
use Getopt::Long;
use Win32::OLE;

# Your usual option-processing sludge.
my %opts;
GetOptions (\%opts, 'remote=s')
    or die "Usage: $0 [--remote <host>] <path> <share>\n";

# Ensure exactly two arguments after options.
scalar @ARGV == 2
    or die "Usage: $0 [--remote <host>] <path> <share>\n";

my ($path, $share) = @ARGV;

# Bomb out completely if COM engine encounters any trouble.
Win32::OLE->Option ('Warn' => 3);

# Get a handle to the SWbemServices object of the machine.
my $computer = Win32::OLE->GetObject (exists $opts{'remote'}
                                      ? "WinMgmts://$opts{'remote'}/"
                                      : 'WinMgmts:');

# Get the Win32_Share class object.  See:
# <http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_share.asp>
my $share_class = $computer->Get ('Win32_Share');

my $ret = $share_class->Create ($path, $share, 0);

$ret == 0
    or die "Unable to share $path as $share; error $ret\n";

exit 0;

Reply via email to