TMTOWTDI, WMI, and map abuse
mjg
created: 2006-03-30 11:31:34
I'm working on a behemoth of a script to inventory Windows Oracle servers, using [mod://Win32::OLE] and Microsoft's [href://http://msdn.microsoft.com/library/en-us/dnanchor/html/anch_wmi.asp|WMI] to pick up various interesting facts. This is probably of interest to only a few people (if that's you, I feel your pain), but I ended up writing two snippets that do the same thing, and I thought it would make a good example of traditional procedural code vs. utter and total abuse of the [doc://map] function.

The code might also be useful to those looking to dive into [href:http://msdn.microsoft.com/library/en-us/wmisdk/wmi/wql_sql_for_wmi.asp|WQL] (WMI Query Language). Again, probably not applicable to most, but hey, it's my job.

# %oracle_instance's keys are names of Oracle instances on the server
# $WMI_Services is a Win32::OLE object corresponding to a server's
# "root/cimv2" WMI namespace
# And the "in" function is an import from Win32::OLE

# Here's the traditional code
print "These need to be active Oracle admins with DBA privileges:\n";

# get ORA_DBA and ORA__DBA groups
my $group_query = "select * from Win32_Group where Name = 'ORA_DBA'";
foreach my $instance ( keys %oracle_instance ) {
    $group_query .= " or Name = 'ORA_${instance}_DBA'";
}
my $ora_groups = $WMI_Services->ExecQuery($group_query);

# get users in the above groups
foreach my $group ( in $ora_groups ) {
    my $domain_ora_dba = $WMI_Services->ExecQuery(<{PartComponent} =~ /,Name="(\w+)"$/;
    }
}
print "\n";


# And now in the spirit of TMTOWTDI, let's hear it for data flow pipelines!
print "These need to be active Oracle admins with DBA privileges:\n",
    join "\n", map $_->{PartComponent} =~ /,Name="(\w+)"$/, # names of users
        in $WMI_Services->ExecQuery(                        # that are members
            'select PartComponent from Win32_GroupUser where GroupComponent=' .
                "\"Win32_Group.Domain='$_->{Domain}',Name='$_->{Name}'\"")
        foreach in $WMI_Services->ExecQuery(				
            "select * from Win32_Group where Name = 'ORA_DBA' or " .
            join ' or ', map "Name = 'ORA_${_}_DBA'", # of ORA_DBA group
                keys %oracle_instance);               # or ORA__DBA
print "\n\n";

perlmonks.org content © perlmonks.org and mjg

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03