A WMI perl script for local/ remote OS info.


Below is a simple perl script to fetch local / remote Operating System Information (for Microsoft Platform). Needs Windows Management and Instrumentation Service Enabled on the remote machine. Tested on Active Perl 5.6+ and XP. Useful for maintaining inventory information.

use strict;
use Win32::OLE(‘in’);

use constant bFlagReturnImmediately => 0×10;
use constant bFlagForwardOnly => 0×20;

my @computers = (“localhost”);
foreach my $computer (@computers)
{
print “\n”;
print “—————————————————————–\n”;
print “| System Name : $computer |\n”;
print “—————————————————————–\n”;

my $oWMIService = Win32::OLE->GetObject( “winmgmts:\\\\$computer\\root\\CIMV2″)
or die “WMI connection failed.\n”;

my $colItems = $oWMIService->ExecQuery ( “SELECT * FROM Win32_ComputerSystem”, “WQL”, bFlagReturnImmediately | bFlagForwardOnly
);
foreach my $objItem (in $colItems)
{
print “AdminPasswordStatus: $objItem->{AdminPasswordStatus}\n”;
print “AutomaticResetBootOption: $objItem->{AutomaticResetBootOption}\n”;
print “AutomaticResetCapability: $objItem->{AutomaticResetCapability}\n”;
print “BootOptionOnLimit: $objItem->{BootOptionOnLimit}\n”;
print “BootOptionOnWatchDog: $objItem->{BootOptionOnWatchDog}\n”;
print “BootROMSupported: $objItem->{BootROMSupported}\n”;
print “BootupState: $objItem->{BootupState}\n”;
print “Caption: $objItem->{Caption}\n”;
print “ChassisBootupState: $objItem->{ChassisBootupState}\n”;
print “CreationClassName: $objItem->{CreationClassName}\n”;
print “CurrentTimeZone: $objItem->{CurrentTimeZone}\n”;
print “DaylightInEffect: $objItem->{DaylightInEffect}\n”;
print “Description: $objItem->{Description}\n”;
print “Domain: $objItem->{Domain}\n”;
print “DomainRole: $objItem->{DomainRole}\n”;
print “EnableDaylightSavingsTime: $objItem->{EnableDaylightSavingsTime}\n”;
print “FrontPanelResetStatus: $objItem->{FrontPanelResetStatus}\n”;
print “InfraredSupported: $objItem->{InfraredSupported}\n”;
print “InitialLoadInfo: ” . join(“,”, (in $objItem->{InitialLoadInfo})) . “\n”;
print “InstallDate: $objItem->{InstallDate}\n”;
print “KeyboardPasswordStatus: $objItem->{KeyboardPasswordStatus}\n”;
print “LastLoadInfo: $objItem->{LastLoadInfo}\n”;
print “Manufacturer: $objItem->{Manufacturer}\n”;
print “Model: $objItem->{Model}\n”;
print “Name: $objItem->{Name}\n”;
print “NameFormat: $objItem->{NameFormat}\n”;
print “NetworkServerModeEnabled: $objItem->{NetworkServerModeEnabled}\n”;
print “NumberOfProcessors: $objItem->{NumberOfProcessors}\n”;
print “OEMLogoBitmap: ” . join(“,”, (in $objItem->{OEMLogoBitmap})) . “\n”;
print “OEMStringArray: ” . join(“,”, (in $objItem->{OEMStringArray})) . “\n”;
print “PartOfDomain: $objItem->{PartOfDomain}\n”;
print “PauseAfterReset: $objItem->{PauseAfterReset}\n”;
print “PowerManagementCapabilities: ” . join(“,”, (in $objItem->{PowerManagementCapabilities})) . “\n”;
print “PowerManagementSupported: $objItem->{PowerManagementSupported}\n”;
print “PowerOnPasswordStatus: $objItem->{PowerOnPasswordStatus}\n”;
print “PowerState: $objItem->{PowerState}\n”;
print “PowerSupplyState: $objItem->{PowerSupplyState}\n”;
print “PrimaryOwnerContact: $objItem->{PrimaryOwnerContact}\n”;
print “PrimaryOwnerName: $objItem->{PrimaryOwnerName}\n”;
print “ResetCapability: $objItem->{ResetCapability}\n”;
print “ResetCount: $objItem->{ResetCount}\n”;
print “ResetLimit: $objItem->{ResetLimit}\n”;
print “Roles: ” . join(“,”, (in $objItem->{Roles})) . “\n”;
print “Status: $objItem->{Status}\n”;
print “SupportContactDescription: ” . join(“,”, (in $objItem->{SupportContactDescription})) . “\n”;
print “SystemStartupDelay: $objItem->{SystemStartupDelay}\n”;
print “SystemStartupOptions: ” . join(“,”, (in $objItem->{SystemStartupOptions})) . “\n”;
print “SystemStartupSetting: $objItem->{SystemStartupSetting}\n”;
print “SystemType: $objItem->{SystemType}\n”;
print “ThermalState: $objItem->{ThermalState}\n”;
print “TotalPhysicalMemory: $objItem->{TotalPhysicalMemory}\n”;
print “UserName: $objItem->{UserName}\n”;
print “WakeUpType: $objItem->{WakeUpType}\n”;
print “Workgroup: $objItem->{Workgroup}\n”;
print “\n”;
}
}

 

Blogged with Flock

About these ads

7 thoughts on “A WMI perl script for local/ remote OS info.

  1. shaneel says:

    Hi i want to check the OS for linux platforms somewhere in my prohect and on basis of that i want to proceed forther.
    Can you please suggest for–> my $oWMIService = Win32::OLE->GetObject lines in your code what should be done if i want to check for linux.

  2. Michael Jones says:

    Here’s a friendly notepad version:

    #!/usr/bin/perl
    use strict;
    use Win32::OLE(‘in’);
    use constant bFlagReturnImmediately => 0×10;
    use constant bFlagForwardOnly => 0×20;
    my @computers = (“localhost”);
    foreach my $computer (@computers)
    {
    print “\n”;
    print “—————————————————————–\n”;
    print “| System Name : $computer |\n”;
    print “—————————————————————–\n”;
    my $oWMIService = Win32::OLE->GetObject( “winmgmts:\\\\$computer\\root\\CIMV2″) or die “WMI connection failed.\n”;
    my $colItems = $oWMIService->ExecQuery ( “SELECT * FROM Win32_ComputerSystem”, “WQL”, bFlagReturnImmediately | bFlagForwardOnly);
    foreach my $objItem (in $colItems)
    {
    print “AdminPasswordStatus: $objItem->{AdminPasswordStatus}\n”;
    print “AutomaticResetBootOption: $objItem->{AutomaticResetBootOption}\n”;
    print “AutomaticResetCapability: $objItem->{AutomaticResetCapability}\n”;
    print “BootOptionOnLimit: $objItem->{BootOptionOnLimit}\n”;
    print “BootOptionOnWatchDog: $objItem->{BootOptionOnWatchDog}\n”;
    print “BootROMSupported: $objItem->{BootROMSupported}\n”;
    print “BootupState: $objItem->{BootupState}\n”;
    print “Caption: $objItem->{Caption}\n”;
    print “ChassisBootupState: $objItem->{ChassisBootupState}\n”;
    print “CreationClassName: $objItem->{CreationClassName}\n”;
    print “CurrentTimeZone: $objItem->{CurrentTimeZone}\n”;
    print “DaylightInEffect: $objItem->{DaylightInEffect}\n”;
    print “Description: $objItem->{Description}\n”;
    print “Domain: $objItem->{Domain}\n”;
    print “DomainRole: $objItem->{DomainRole}\n”;
    print “EnableDaylightSavingsTime: $objItem->{EnableDaylightSavingsTime}\n”;
    print “FrontPanelResetStatus: $objItem->{FrontPanelResetStatus}\n”;
    print “InfraredSupported: $objItem->{InfraredSupported}\n”;
    print “InitialLoadInfo: ” . join(“,”, (in $objItem->{InitialLoadInfo})) . “\n”;
    print “InstallDate: $objItem->{InstallDate}\n”;
    print “KeyboardPasswordStatus: $objItem->{KeyboardPasswordStatus}\n”;
    print “LastLoadInfo: $objItem->{LastLoadInfo}\n”;
    print “Manufacturer: $objItem->{Manufacturer}\n”;
    print “Model: $objItem->{Model}\n”;
    print “Name: $objItem->{Name}\n”;
    print “NameFormat: $objItem->{NameFormat}\n”;
    print “NetworkServerModeEnabled: $objItem->{NetworkServerModeEnabled}\n”;
    print “NumberOfProcessors: $objItem->{NumberOfProcessors}\n”;
    print “OEMLogoBitmap: ” . join(“,”, (in $objItem->{OEMLogoBitmap})) . “\n”;
    print “OEMStringArray: ” . join(“,”, (in $objItem->{OEMStringArray})) . “\n”;
    print “PartOfDomain: $objItem->{PartOfDomain}\n”;
    print “PauseAfterReset: $objItem->{PauseAfterReset}\n”;
    print “PowerManagementCapabilities: ” . join(“,”, (in $objItem->{PowerManagementCapabilities})) . “\n”;
    print “PowerManagementSupported: $objItem->{PowerManagementSupported}\n”;
    print “PowerOnPasswordStatus: $objItem->{PowerOnPasswordStatus}\n”;
    print “PowerState: $objItem->{PowerState}\n”;
    print “PowerSupplyState: $objItem->{PowerSupplyState}\n”;
    print “PrimaryOwnerContact: $objItem->{PrimaryOwnerContact}\n”;
    print “PrimaryOwnerName: $objItem->{PrimaryOwnerName}\n”;
    print “ResetCapability: $objItem->{ResetCapability}\n”;
    print “ResetCount: $objItem->{ResetCount}\n”;
    print “ResetLimit: $objItem->{ResetLimit}\n”;
    print “Roles: ” . join(“,”, (in $objItem->{Roles})) . “\n”;
    print “Status: $objItem->{Status}\n”;
    print “SupportContactDescription: ” . join(“,”, (in $objItem->{SupportContactDescription})) . “\n”;
    print “SystemStartupDelay: $objItem->{SystemStartupDelay}\n”;
    print “SystemStartupOptions: ” . join(“,”, (in $objItem->{SystemStartupOptions})) . “\n”;
    print “SystemStartupSetting: $objItem->{SystemStartupSetting}\n”;
    print “SystemType: $objItem->{SystemType}\n”;
    print “ThermalState: $objItem->{ThermalState}\n”;
    print “TotalPhysicalMemory: $objItem->{TotalPhysicalMemory}\n”;
    print “UserName: $objItem->{UserName}\n”;
    print “WakeUpType: $objItem->{WakeUpType}\n”;
    print “Workgroup: $objItem->{Workgroup}\n”;
    print “\n”;
    }
    }

  3. Paul says:

    shaneel — if you want to check windows from linux using wmi, install wmi-client (a debian/ubuntu package, originally it’s from the zenoss project, they’ll have the source)

    Obviously you cant check a linux machine using wmi, linux will usually give you information via snmp.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s