http://qs321.pair.com?node_id=340422
Category: Win32 Stuff
Author/Contact Info Christian Vandaele
Description: Allows you to retrieve all your hardware and OS configuration using WMI.
The information is saved to an ini file in order to parse data easily and reproduce a "phpSysInfo" script(works on Win2000/XP).
#!perl -w

# by Christian Vandaele
# This program is free software, it may be used, modified
# and redistributed under the same terms as Perl itself.

use strict;

use Win32::OLE qw(in);
use Time::Local qw(timegm);

my $WMI = Win32::OLE->new("WbemScripting.SWbemLocator");
my $Services = $WMI->ConnectServer();

my $instances;
my $idx = 0;
my %cpu = ();
my %temp = ();
my %system = ();
my %mem = ();
my %video = ();
my %sound = ();
my %cdrom = ();
my %disk = ();
my %scsi = ();
my %logical = ();
my %eth = ();
my %interface = ();

$instances = $Services->InstancesOf("Win32_ComputerSystem");
foreach my $item (in($instances)) {  
  $system{"Caption"} = $item->{"Caption"};
}

$idx = 0;
$instances = $Services->InstancesOf("Win32_Processor");
foreach my $item (in($instances)) {
  $cpu{$idx}->{"Name"}              = $item->{"Name"};
  $cpu{$idx}->{"L2CacheSize"}       = $item->{"L2CacheSize"};
  $cpu{$idx}->{"L2CacheSpeed"}      = $item->{"L2CacheSpeed"};
  $cpu{$idx}->{"CurrentClockSpeed"} = $item->{"CurrentClockSpeed"};
  $cpu{$idx}->{"LoadPercentage"}    = $item->{"LoadPercentage"};
  $cpu{$idx}->{"CurrentVoltage"}    = $item->{"CurrentVoltage"} / 10;
  $cpu{$idx}->{"ExtClock"}          = $item->{"ExtClock"};
  $idx++;
}

$instances = $Services->InstancesOf("Win32_PageFileUsage");
foreach my $item (in($instances)) {
  $mem{"PagingFilesSize"}       = $item->{"CurrentUsage"} * 1048576;
  $mem{"PagingFilesTotalSpace"} = $item->{"AllocatedBaseSize"} * 10485
+76;
  $mem{"PagingFilesFreeSpace"}  = $mem{"PagingFilesTotalSpace"} - $mem
+{"PagingFilesSize"};
}

$instances = $Services->InstancesOf("Win32_OperatingSystem");
foreach my $item (in($instances)) {
  $system{"Caption"}        = $item->{"Caption"};
  $system{"Version"}        = $item->{"Version"};
  $system{"CSDVersion"}     = $item->{"CSDVersion"};
  $system{"LastBootUpTime"} = $item->{"LastBootUpTime"};

  my $h = substr($system{"LastBootUpTime"}, 8, 2);
  my $i = substr($system{"LastBootUpTime"}, 10, 2);
  my $s = substr($system{"LastBootUpTime"}, 12, 2);
  my $d = substr($system{"LastBootUpTime"}, 6, 2);
  my $m = substr($system{"LastBootUpTime"}, 4, 2) - 1;
  my $y = substr($system{"LastBootUpTime"}, 2, 2);

  $system{"LastBootUpTime"} = timegm($s, $i, $h, $d, $m, $y);

  $mem{"TotalVisibleMemorySize"} = $item->{"TotalVisibleMemorySize"} *
+ 1024;
  $mem{"FreePhysicalMemory"}     = $item->{"FreePhysicalMemory"} * 102
+4;
  $mem{"UsedPhysicalMemory"}     = $mem{"TotalVisibleMemorySize"} - $m
+em{"FreePhysicalMemory"};
}

$idx = 0;
$instances = $Services->InstancesOf("Win32_VideoController");
foreach my $item (in($instances)) {
  $video{$idx}->{"Description"}                 = $item->{"Description
+"};
  $video{$idx}->{"CurrentHorizontalResolution"} = $item->{"CurrentHori
+zontalResolution"};
  $video{$idx}->{"CurrentVerticalResolution"}   = $item->{"CurrentVert
+icalResolution"};
  $video{$idx}->{"CurrentRefreshRate"}          = $item->{"CurrentRefr
+eshRate"};
  $video{$idx}->{"CurrentNumberOfColors"}       = $item->{"CurrentNumb
+erOfColors"};
  $video{$idx}->{"AdapterRAM"}                  = $item->{"AdapterRAM"
+};
  $idx++;
}

$idx = 0;
$instances = $Services->InstancesOf("Win32_SoundDevice");
foreach my $item (in($instances)) {
  $sound{$idx}->{"ProductName"} = $item->{"ProductName"};
  $idx++;
}

$idx = 0;
$instances = $Services->InstancesOf("Win32_CDROMDrive");
foreach my $item (in($instances)) {
  $cdrom{$idx}->{"Caption"}  = $item->{"Caption"};
  $cdrom{$idx}->{"Drive"}    = $item->{"Drive"};
  $idx++;
}

$idx = 0;
$instances = $Services->InstancesOf("Win32_DiskDrive");
foreach my $item (in($instances)) {
  if ($item->{"InterfaceType"} eq "IDE") {
    $disk{$idx}->{"Caption"}       = $item->{"Caption"};
    $disk{$idx}->{"Partitions"}    = $item->{"Partitions"};
    $disk{$idx}->{"InterfaceType"} = $item->{"InterfaceType"};
    $disk{$idx}->{"Size"}          = $item->{"Size"};
  } elsif ($item->{"InterfaceType"} eq "SCSI") {
    $scsi{$idx}->{"Caption"}       = $item->{"Caption"};
    $scsi{$idx}->{"Partitions"}    = $item->{"Partitions"};
    $scsi{$idx}->{"InterfaceType"} = $item->{"InterfaceType"};
    $scsi{$idx}->{"Size"}          = $item->{"Size"};
  }

  $idx++;
}

$idx = 0;
$instances = $Services->InstancesOf("Win32_LogicalDisk");
foreach my $item (in($instances)) {
  if ($item->{"DriveType"} == 3) {
    $logical{$idx}->{"Name"}       = $item->{"Name"};
    $logical{$idx}->{"FileSystem"} = $item->{"FileSystem"};
    $logical{$idx}->{"Size"}       = $item->{"Size"};
    $logical{$idx}->{"FreeSpace"}  = $item->{"FreeSpace"};
    $logical{$idx}->{"UsedSpace"}  = $logical{$idx}->{"Size"} - $logic
+al{$idx}->{"FreeSpace"};
    $logical{$idx}->{"VolumeName"} = $item->{"VolumeName"};
    $idx++;
  }
}

$idx = 0;
$instances = $Services->InstancesOf("Win32_NetworkAdapterConfiguration
+");
foreach my $item (in($instances)) {
  if ($item->{"IPEnabled"} == 1) {
    $eth{$idx}->{"Caption"}     = $item->{"Caption"};
    $eth{$idx}->{"Description"} = $item->{"Description"};
    $idx++;
  }
}

$idx = 0;
$instances = $Services->InstancesOf("Win32_PerfRawData_Tcpip_NetworkIn
+terface");
foreach my $item (in($instances)) {
  $interface{$idx}->{"Name"}                     = $item->{"Name"};
  $interface{$idx}->{"BytesReceivedPerSec"}      = $item->{"BytesRecei
+vedPerSec"};
  $interface{$idx}->{"BytesSentPerSec"}          = $item->{"BytesSentP
+erSec"};
  $interface{$idx}->{"CurrentBandwidth"}         = $item->{"CurrentBan
+dwidth"};
  $interface{$idx}->{"PacketsOutboundDiscarded"} = $item->{"PacketsOut
+boundDiscarded"};
  $interface{$idx}->{"PacketsOutboundErrors"}    = $item->{"PacketsOut
+boundErrors"};
  $interface{$idx}->{"PacketsReceivedDiscarded"} = $item->{"PacketsRec
+eivedDiscarded"};
  $interface{$idx}->{"PacketsReceivedErrors"}    = $item->{"PacketsRec
+eivedErrors"};
  $idx++;
}

if (!open(FILE, ">system.ini")) { die(); }

print FILE "[info]\n";
print FILE "time=" . time() . "\n";

print FILE "\n";
print FILE "[cpu]\n";
print FILE "total=" . (scalar keys %cpu) . "\n";
for ($idx = 0; $idx < scalar keys %cpu; $idx++) {
  foreach my $key (keys %{$cpu{$idx}}) {
    print FILE "cpu" . $idx . ":" . $key . "=" . $cpu{$idx}->{$key} . 
+"\n";
  }
}

print FILE "\n";
print FILE "[os]\n";
foreach my $key (keys %system) {
  print FILE $key . "=" . $system{$key} . "\n";
}

print FILE "\n";
print FILE "[mem]\n";
foreach my $key (keys %mem) {
  print FILE $key . "=" . $mem{$key} . "\n";
}

print FILE "\n";
print FILE "[video]\n";
print FILE "total=" . (scalar keys %video) . "\n";
for ($idx = 0; $idx < scalar keys %video; $idx++) {
  foreach my $key (keys %{$video{$idx}}) {
    print FILE "video" . $idx . ":" . $key . "=" . $video{$idx}->{$key
+} . "\n";
  }
}

print FILE "\n";
print FILE "[sound]\n";
print FILE "total=" . (scalar keys %sound) . "\n";
for ($idx = 0; $idx < scalar keys %sound; $idx++) {
  foreach my $key (keys %{$sound{$idx}}) {
    print FILE "sound" . $idx . ":" . $key . "=" . $sound{$idx}->{$key
+} . "\n";
  }
}

print FILE "\n";
print FILE "[cdrom]\n";
print FILE "total=" . (scalar keys %cdrom) . "\n";
for ($idx = 0; $idx < scalar keys %cdrom; $idx++) {
  foreach my $key (keys %{$cdrom{$idx}}) {
    print FILE "cdrom" . $idx . ":" . $key . "=" . $cdrom{$idx}->{$key
+} . "\n";
  }
}

print FILE "\n";
print FILE "[disk]\n";
print FILE "total=" . (scalar keys %disk) . "\n";
for ($idx = 0; $idx < scalar keys %disk; $idx++) {
  foreach my $key (keys %{$disk{$idx}}) {
    print FILE "disk" . $idx . ":" . $key . "=" . $disk{$idx}->{$key} 
+. "\n";
  }
}

print FILE "\n";
print FILE "[scsi]\n";
print FILE "total=" . (scalar keys %scsi) . "\n";
for ($idx = 0; $idx < scalar keys %scsi; $idx++) {
  foreach my $key (keys %{$scsi{$idx}}) {
    print FILE "scsi" . $idx . ":" . $key . "=" . $scsi{$idx}->{$key} 
+. "\n";
  }
}

print FILE "\n";
print FILE "[logical]\n";
print FILE "total=" . (scalar keys %logical) . "\n";
for ($idx = 0; $idx < scalar keys %logical; $idx++) {
  foreach my $key (keys %{$logical{$idx}}) {
    print FILE "logical" . $idx . ":" . $key . "=" . $logical{$idx}->{
+$key} . "\n";
  }
}

print FILE "\n";
print FILE "[eth]\n";
print FILE "total=" . (scalar keys %eth) . "\n";
for ($idx = 0; $idx < scalar keys %eth; $idx++) {
  foreach my $key (keys %{$eth{$idx}}) {
    print FILE "eth" . $idx . ":" . $key . "=" . $eth{$idx}->{$key} . 
+"\n";
  }
}

print FILE "\n";
print FILE "[interface]\n";
print FILE "total=" . (scalar keys %interface) . "\n";
for ($idx = 0; $idx < scalar keys %interface; $idx++) {
  foreach my $key (keys %{$interface{$idx}}) {
    print FILE "if" . $idx . ":" . $key . "=" . $interface{$idx}->{$ke
+y} . "\n";
  }
}

close(FILE);