490 said:
I am trying to get win NT services and drives detailes.
I used the code:
'WINMSD /f' ;
'set >info.txt';
And then i go through the details and get what i want form the txt
file.
The problem is that this is a very ugly way and it uses alot of CPU &
Memory Usage.
Does any body have a better and faster way?!
I'd use Win32::OLE and the WMI for that. The WMI classes are
well documented over at msdn.microsoft.com (there you will find
all attributes explained, like e.g. the DriveType numbers I'm
using in the example), and using them is quite straight forward.
Though if I recall correctly, WMI has to be installed manually
on NT 4 (requiring SP4 or above), but this has the bonus of
running on later MS-OSes without changes, whereas the "write
text" option of winmsd got lost somewhere along the way.
----------------------------------------------------------------
#!/perl
use strict;
use warnings;
use Win32::OLE qw(in);
my $computer = "."; # MS's short notation for "this host"
my $wmi = Win32::OLE->GetObject(
'winmgmts:{impersonationLevel=impersonate,(security)}//' .
$computer .
'/root/CIMV2'
);
print "Logical Drives:" . $/;
print "===============" . $/;
my $drives = $wmi->ExecQuery(
'SELECT * FROM Win32_LogicalDisk ' .
'WHERE DriveType = 2 OR DriveType = 3'
);
foreach my $drive ( in($drives) )
{
printf( "%s %-12s Bytes, %-12s Bytes free$/",
$drive->{'Name'},
$drive->{'Size'},
$drive->{'FreeSpace'}
);
}
print "Services:" . $/;
print "=========" . $/;
my $services = $wmi->ExecQuery('SELECT * FROM Win32_Service');
foreach my $srvc ( in($services) )
{
printf("%-30s %10s: %-10s (%s)$/",
$srvc->{'Name'},
"[" . $srvc->{'StartMode'} . "]",
$srvc->{'State'},
$srvc->{'PathName'}
);}