1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #!/usr/bin/perl
use strict;
use warnings;
use utf8;
use DBI; # Pour interroger la base WMI sous Windows
use Win32::GUI (); # Pour afficher une fenêtre d'ATTENTION
use Encode; # Pour encoder en iso-8859-16
use File::Spec; # Pour gérée les noms des chemins sous tous les OS
use File::Path; # Pour effacement récursif de répertoire
my $dbh = DBI->connect('dbi:WMI:');
my $sth = $dbh->prepare(<<WQL);
SELECT * FROM __instanceoperationevent
WITHIN 5
WHERE TargetInstance ISA 'Win32_LogicalDisk'
and TargetInstance.Name != 'C:'
WQL
$sth->execute();
ActiverAccents();
my ( $Disk, $FhRep ) = ();
while (defined (my $row = $sth->fetchrow_arrayref())) {
my $ev = $row->[0];
if ($ev->Path_->Class eq '__InstanceCreationEvent') {
$Disk = $ev->{TargetInstance}->{Name};
$Disk = $Disk ."\\";
my @Cde = "E:\\_DEV\\USB\\commande.bat";
system @Cde;
}
}
#==============================================================
# Pour avoir les accents sur la console DOS
# http://perl.developpez.com/faq/perl/?page=Terminal#AccentsDOS
#==============================================================
sub ActiverAccents {
my $encodage;
# Windows
if ( lc($^O ) eq 'mswin32') {
eval {
my ($codepage) = ( `chcp` =~ m/:\s+(\d+)/ );
$encodage = "cp$codepage";
foreach my $h ( \*STDOUT, \*STDERR, \*STDIN, ) {
binmode $h, ":encoding($encodage)";
}
};
}
else {
$encodage = `locale charmap`;
eval {
foreach my $h ( \*STDOUT, \*STDERR, \*STDIN, ) {
binmode $h, ":encoding($encodage)";
}
};
}
return $encodage;
}
#Sinon :
#ü \x81 à \x85 è \x8A
#é \x82 ç \x87 ï \x8B
#â \x83 ê \x88 î \x8C
#ä \x84 ë \x89
=pod
##############################################
#
# DOC & ORIGINES
#
#http://www.perlmonks.org/bare/?node_id=827106
#!/usr/bin/perl -w
package main;
use strict;
use DBI;
use Data::Dumper;
my $dbh = DBI->connect('dbi:WMI:');
my $sth = $dbh->prepare(<<WQL);
SELECT * FROM __instanceoperationevent
WITHIN 5
WHERE TargetInstance ISA 'Win32_LogicalDisk'
and TargetInstance.Name != 'C:'
WQL
$sth->execute();
# loops forever
while (defined (my $row = $sth->fetchrow_arrayref())) {
my $ev = $row->[0];
# Windows XP only :(
print "Class\t",$ev->Path_->Class,"\n";
for (qw(Name DeviceID Caption MediaType InterfaceType)) {
print join "\t", $_, ($ev->TargetInstance->{$_} || '<none>');
print "\n";
if ($ev->Path_->Class eq '__InstanceCreationEvent') {
print "Device (letter) $ev->{TargetInstance}->{Name} added\n";
}
}
}
VOIR AUSSI :
http://powershell-scripting.com/index.php?option=com_joomlaboard&Itemid=76&func=view&catid=14&id=4844 |
Partager