Win32:OLE and properties enumeration

M

Mariusz Gadula

Hi,

Using examples from Internet I am writing library to manage IIS
metabase in Perl (ActiveState). I have problem with procedure that
will dump metabase settings. The problem is with enumeration of
properties. In line where I should get properties names I get nothing.
Part of the code bellow:

########################################################################
package IIS;

use strict;
use warnings;
use Win32;
use Win32::OLE qw(in with);
use Data::Dump qw(dump);

#Win32::OLE->Option(Warn => 0);

my $VERSION=1.0.0.2;

$|=1;

#
# new - create and instance
#

sub new {

my $class = shift;
$class = ref( $class ) || $class;

my $hostname = shift;

$hostname = defined $hostname ? $hostname : 'localhost';

my $self = {};

$self->{ 'hostname' } = $hostname;

$self->{ 'W3SVC'} = Win32::OLE->GetObject("IIS://$hostname/W3SVC");

$self->{ 'error_string' } = 'success';

bless( $self, $class );

return $self;
}

#
# get_error_string - retrieve the error string
#

sub get_error_string {

my $self = shift;

return $self->{ 'error_string' };

}

#
# IIS subs
#

[...]

#
# Not ready!
#
sub dump_www {

my $self = shift;

my $vdirObj = $self->{ 'W3SVC'};

my %DUMP = ();

if (defined $vdirObj) {
&show_object($vdirObj,0);
$self->{ 'error_string' } = $^E if (Win32::OLE->LastError());
} else {
$self->{ 'error_string' } = $^E;
return 0;
}
# return %;
}

sub show_object {

my $hash = shift (@_);
my $count = shift (@_);
my $ptab = '';

for(my $i=0; $i<$count; $i++) { $ptab .= "\t"; }

print "$ptab",$hash->{Name},"\n";
my %PROPERTIES = %$hash; # <- HERE
map {print "\t$ptab",$_,"\n";} sort keys %PROPERTIES;
print "\n";
for my $object (in $hash) {
&show_object($object,$count + 1);
}
}

sub destroy {
Win32::OLE->FreeUnusedLibraries();
Win32::OLE->Uninitialize();
1;
};

1;
###################################################################
The result is :
----------------
W3SVC
1
Filters
IIsCertMapper
ROOT
AppPools
DefaultAppPool
Filters
Compression
deflate
gzip
Parameters
Info
Templates
Public Web Site
Root
Secure Web Site
Root
 
M

Mariusz Gadula

I solve the problem. Below part of the code in case someone else have
to work with IIS metabase from perl :)

#################################################################
# Name: IIS.pm #
# Date: Monday, November 29, 2004 #
# #
# Author: Mariusz Gadula #
# #
#################################################################

package IIS;

use strict;
use warnings;
use Win32;
use Win32::OLE qw(in with);

my $VERSION=1.0.0.3;

$|=1;

#
# new - create and instance
#

sub new {
my $class = shift;
$class = ref( $class ) || $class;

my $hostname = shift;
$hostname = defined $hostname ? $hostname : 'localhost';

my $self = {};
$self->{ 'hostname' } = $hostname;
$self->{ 'W3SVC'} = Win32::OLE->GetObject("IIS://$hostname/W3SVC");
$self->{ 'error_string' } = 'success';

bless( $self, $class );
return $self;
}

#
# get_error_string - retrieve the error string
#

sub get_error_string {
my $self = shift;
return $self->{ 'error_string' };

}

#
# IIS subs
#

#
# [..] Some other procedures not published here
#

#
# READY!
#
sub ENUMALL {

my $self = shift;
my $MetabaseRef = shift;

my $vdirObj = $self->{ 'W3SVC'};

if (defined $vdirObj) {
&EnumCommand($vdirObj,'',0,$MetabaseRef);
$self->{ 'error_string' } = $^E if (Win32::OLE->LastError());
} else {
$self->{ 'error_string' } = $^E;
return 0;
}
return $MetabaseRef;
}

sub EnumCommand {

my $IISObject = shift (@_);
my $IISObjectName = $IISObject->{Name};
my $IISObjectPath = shift (@_) . '/' . $IISObjectName;
my $count = shift (@_); #how many times to tab
my $DUMP = shift @_;

my $IIsSchemaPath = $IISObject->{Schema};
my $IIsSchemaObject = Win32::OLE->GetObject($IIsSchemaPath);
my $PropertyList = \( @{$IIsSchemaObject->{MandatoryProperties}},
@{$IIsSchemaObject->{OptionalProperties}});

foreach my $PropertyName (@$PropertyList) {
my $PropertyAttribObj =
$IISObject->GetPropertyAttribObj($PropertyName);
next if (Win32::OLE->LastError());
my $IsInherit = 0;
if (defined $PropertyAttribObj && $PropertyAttribObj->{IsInherit}) {
$IsInherit = 1;
}
if (not($IsInherit)) {
my $PropertyValue = '';
$PropertyValue = $IISObject->{$PropertyName};
next if ref($IISObject->{$PropertyName}) =~ /^Win32::OLE/;
$$DUMP{$IISObjectPath}{$PropertyName} = $PropertyValue;
}
} ;
# Iteration
for my $object (in $IISObject) {
&EnumCommand($object,$IISObjectPath,$count + 1,$DUMP);
}
}

sub destroy {
Win32::OLE->FreeUnusedLibraries();
Win32::OLE->Uninitialize();
1;
};

1;
######################################################################

Example usage:

my $IIS = new IIS();

my %IIS_DUMP = ();
$IIS->ENUMALL(\%IIS_DUMP);

print 'AnonymousUserName: ', $IIS_DUMP{"/W3SVC"}{AnonymousUserName},
"\n";
print 'AnonymousUserPass: ', $IIS_DUMP{"/W3SVC"}{AnonymousUserPass},
"\n";

my @ServerBindings = @{$IIS_DUMP{"/W3SVC/1"}{ServerBindings}};
print "ServerBindings:\n";
map {print $_,"\n"} sort @ServerBindings;

# or
use Data::Dump qw(dump);
dump(%IIS_DUMP);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top