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

I understand this may not be the best place to ask this, but I'm about fed up with ADSI.

I'm trying to create a utility that will add to the IPSecurity/IPGrant/IPDeny array in ADSI. Using the Win32::OLE module, I have this code:

use strict; use Win32::OLE; use Win32::OLE::Enum; use Carp; eval { my $newip = "13.11.11.11,255.255.255.0"; Win32::OLE->Option(Warn => 3); my $secobj = Win32::OLE->GetObject("IIS://ogre/W3svc/1/Root") or +croak "Couldn't get object: $!\n"; my $MyIPSec = $secobj->{IPSecurity}; my $ref_IPList = $MyIPSec->{IPGrant}; my @IPList = @{$ref_IPList}; foreach my $i (@IPList) { print "Granted IP: $i\n"; } push(@IPList,$newip); my $ref_newIPList = \@IPList; $MyIPSec->IPGrant = \@IPList or croak "ERROR: Couldn't set variab +le: $!\n"; # $MyIPSec->SetProperty("IPDeny",$ref_newIPList) or croak "ERROR: + Couldn't SetProperty: $!\n"; # $MyIPSec->{IPGrant}->create($newip) or croak "ERROR: Couldn't cr +eate new grant: $!\n"; # $obj_grant->($newip) or croak "ERROR: Couldn't create new grant: +$!\n"; # $MyIPSec->IPGrant(@IPList) or carp "Couldn't do SetInfo: $!\n"; $MyIPSec->SetProperty("IPGrant",$ref_IPList) or carp "WARNING: Cou +ldn't SetInfo(): ".Win32::OLE->LastError().".\n"; my $secobjnew = Win32::OLE->GetObject("IIS://ogre/W3svc/1/Root") o +r croak "Couldn't get object: $!\n"; my $MyIPSecnew = $secobjnew->{IPSecurity}; foreach my $newip (@{$MyIPSecnew->{IPGrant}}) { print "IP Granted (new): $newip\n"; } }; print Win32::OLE->LastError()."\n" if (Win32::OLE->LastError());

As you can probably see, I have tried almost every method I can think of to set the $MyIPSec->IPGrant Property/Method to the correct value. I can get my script to recognize the right data, and return it when I dereference the array returned by $MyIPSec->{IPGrant}. The right data, however, is never committed to the server.

J. J. Horner
Linux, Perl, Apache, Stronghold, Unix
jhorner@knoxlug.org http://www.knoxlug.org/

Replies are listed 'Best First'.
Re: IIS/ADSI/Win32-OLE
by $code or die (Deacon) on Jan 15, 2002 at 05:44 UTC

    I've written code to do this before, but don't have access to it at the moment. So I'll do my best with what I can remember.

    Are any errors produced when you do:
    $MyIPSec->{IPGrant} = \@IPList; $secobj->SetInfo();

    I did a lot of work on IIS last year with ADSI and perl. The following websites were helpful: IISFAQ.com, cwashington.netreach.net and MSDN. If you can comprehend VBScript they will help. Also, download MetaEdit from MS which lets you browse the IIS metabase - gives you a quick access to what properties are where and what they are currently set to..

    NOTE: Also, whatever you do keep backups of your metabase - I've corrupted quite a few during the course of my work (of course - it was only a test machine)

    updated: changed [ $newip ] to \@IPList to be more consistent with original code



    Simon Flack ($code or die)
    $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
    =~y'_"' ';eval"die";print $_,lc substr$@,0,3;

      I didn't get any errors, but the array didn't change. I changed things around just to see if I could change anything. I can create redirects for directories off of the wwwroot using a Create("IISWebDirectory") type call, but I can't change any of the IPSecurity settings.

      The following code outputs:

      1: 1 2: 0 3: 1
      when ran:
      use strict; use Win32::OLE; use Win32::OLE::Enum; use Carp; eval { my $newip = "13.11.11.11,255.255.255.255"; my $server = "ogre"; Win32::OLE->Option(Warn => 3); my $secobj = Win32::OLE->GetObject("IIS://$server/W3svc/1/Root") +or croak "Couldn't get object: $!\n"; my $MyIPSec = $secobj->{IPSecurity}; print "1: $MyIPSec->{GrantByDefault}\n"; $MyIPSec->{GrantByDefault} = 0; print "2: $MyIPSec->{GrantByDefault}\n"; $secobj->{IPSecurity}->{GrantByDefault} = 0; $secobj->SetInfo(); print "3: $secobj->{IPSecurity}->{GrantByDefault}\n"; }; print Win32::OLE->LastError()."\n" if (Win32::OLE->LastError());
      Thanks, JJ
      J. J. Horner
      Linux, Perl, Apache, Stronghold, Unix
      jhorner@knoxlug.org http://www.knoxlug.org/
      
        According to MSDN this should work...
        use strict; use Win32::OLE; my $server = "ogre"; my $secobj = Win32::OLE->GetObject("IIS://$server/W3svc/1/Root") or die "Couldn't get object:$!\n"; my $MyIPSec = $secobj->{IPSecurity}; $MyIPSec->{GrantByDefault} = 0; $secobj->{IPSecurity} = $MyIPSec; $secobj->SetInfo();


        Simon Flack ($code or die)
        $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
        =~y'_"' ';eval"die";print $_,lc substr$@,0,3;