Friday, June 8, 2012

Retrieve Windows System Information with Perl


For anyone that has to troubleshoot Windows systems, being able to quickly and easily identify information about the computer such as OS info, drive information, network settings, and so on, can be a real time saver.  This Perl script makes use of several Win32 modules to determine information about the installed operating system, the computer name, processor information, RAM information, drive information, and computer network settings.  When running the script, you may want to take advantage of redirection to save the output to a file since it can be a bit on the lengthy side if the computer has multiple processors, multiple network adapters, etc.  To take advantage of redirection run the script as follows:

perl WinInfo.pl > output.txt

This will save the output of the script to a file called output.txt. 

use Win32;
use Win32::SystemInfo;
use Win32::DriveInfo;
use Win32::IPConfig;
use strict;
use warnings;

print "OS Information\n";
my $computer=Win32::NodeName();
print "The computer name is $computer\n";

my $domain=Win32::DomainName();
print "The computer is a member of the $domain domain/workgroup\n";

my $OS=Win32::GetOSDisplayName();
print "The OS is $OS\n";

my $fs=Win32::FsType();
print "The filesytem is $fs\n";

my $user=Win32::LoginName();
print "The current user is $user\n";

my $admin=Win32::IsAdminUser();
if($admin!=0){
    print "$user is running this script as admin\n\n\n";
}
else{
    print "$user is not running this script as admin\n\n\n";
}

print "Processor and RAM Information\n";
my %processor;
Win32::SystemInfo::ProcessorInfo(%processor);
for (my $i=0;$i<$processor{NumProcessors};$i++) {
    print "Processor$i\n";
    print "Processor Name: " . $processor{"Processor$i"}{ProcessorName} . "\n";
    print "Processor Info: " . $processor{"Processor$i"}{Identifier} . "\n";
    print "Processor Speed: " . $processor{"Processor$i"}{MHZ} . "MHz\n\n";
}

my %memory;
Win32::SystemInfo::MemoryStatus(%memory, 'GB');
print "The computer has $memory{TotalPhys} GB of RAM\n\n\n";

my %dtypes=(0 => "Undertmined",
1 => "Does Not Exist",
2 => "Removable",
3 => "Hardrive",
4 => "Network",
5 => "CDROM",
6 => "RAM Disk");

print "Drive Information\n";
my @drives = Win32::DriveInfo::DrivesInUse();
foreach my $drive (@drives){
    my $type=Win32::DriveInfo::DriveType($drive);
    print "Drive $drive is a $dtypes{$type}\n";
  
}

print "\n\nNetwork Information";
my $ipconfig = Win32::IPConfig->new($computer)
        or die "Unable to connect to $computer\n";
foreach my $adapter ($ipconfig->get_adapters) {
    print "\nAdapter '", $adapter->get_name, "':\n";

    print "Description=", $adapter->get_description, "\n";

    print "DHCP enabled=",
    $adapter->is_dhcp_enabled ? "Yes" : "No", "\n";

    my @ipaddresses = $adapter->get_ipaddresses;
    print "IP addresses=@ipaddresses (", scalar @ipaddresses, ")\n";

    my @subnet_masks = $adapter->get_subnet_masks;
    print "subnet masks=@subnet_masks (", scalar @subnet_masks, ")\n";

    my @gateways = $adapter->get_gateways;
    print "gateways=@gateways (", scalar @gateways, ")\n";

    print "domain=", $adapter->get_domain, "\n";

    my @dns = $adapter->get_dns;
    print "dns=@dns (", scalar @dns, ")\n";

    my @wins = $adapter->get_wins;
    print "wins=@wins (", scalar @wins, ")\n";
}

Kobo has over 2 million ebooks to choose from!

3 comments:

Andrew DeFaria said...
This comment has been removed by the author.
Andrew DeFaria said...

Neptune:wininfo.pl
OS Information
The computer name is NEPTUNE
The computer is a member of the UNIVERSE domain/workgroup
Use of inherited AUTOLOAD for non-method Win32::GetOSDisplayName() is deprecated at ./wininfo.pl line 16.
Can't locate auto/Win32/GetOSDispla.al in @INC (@INC contains: /usr/lib/perl5/5.10/i686-cygwin /usr/lib/perl5/5.10 /usr/lib/perl5/site_perl/5.10/i686-cygwin /usr/lib/perl5/site_perl/5.10 /usr/lib/perl5/vendor_perl/5.10/i686-cygwin /usr/lib/perl5/vendor_perl/5.10 /usr/lib/perl5/vendor_perl/5.10 /usr/lib/perl5/site_perl/5.8 /usr/lib/perl5/vendor_perl/5.8 .) at ./wininfo.pl line 16

cfrenz said...

I would check the version of your Win32 module. This script was developed with the 0.44 version of the module. Older versions of module (0.40 and earlier I believe) do not have the GetOSDisplayName function incorporated into the module. This would explain the error you are receiving.