Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm working on a behemoth of a script to inventory Windows Oracle servers, using Win32::OLE and Microsoft's 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 map function.

The code might also be useful to those looking to dive into 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_<instance>_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(<<WQL); select PartComponent from Win32_GroupUser where GroupComponent = "Win32_Group.Domain='$group->{Domain}',Name='$group->{Name}'" WQL # print the users' names foreach my $dba_groupuser ( in $domain_ora_dba ) { print "$1\n" if $dba_groupuser->{PartComponent} =~ /,Name="(\w+)"$/; } } print "\n"; # And now in the spirit of TMTOWTDI, let's hear it for map! print "These need to be active Oracle admins with DBA privileges:\n", join( "\n", # list the names of users map $_->{PartComponent} =~ /,Name="(\w+)"$/, # that are members of in $WMI_Services->ExecQuery( 'select PartComponent from Win32_GroupUser where ' . join ' or ', map "GroupComponent = \"Win32_Group.Domain='$_->{Domain}',Name='$_->{Name}'\"", # the Windows groups named in $WMI_Services->ExecQuery( # ORA_DBA "select * from Win32_Group where Name = 'ORA_DBA' or " . join ' or ', # or ORA_<instance>_DBA map "Name = 'ORA_${_}_DBA'", keys %oracle_instance ) ) ), "\n\n";

In reply to TMTOWTDI, WMI, and map abuse by mjg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-03-29 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found