use Win32::OLE 'in';
$Win32::OLE::Warn = 3;
$Win32::OLE::Warn = 3;

# ------ KONFIGURACJA SKRYPTU ------
$strComputer = '.';
$strExcelPath = 'd:\\procs.xls';
# ------ KONIEC KONFIGURACJI ---------
$objExcel = Win32::OLE->new('Excel.Application');
if (Win32::OLE::LastError()) {
    print "Excel nie jest zainstalowany.\n";
    exit 0;
}

# Utworzenie nowego skoroszytu.
$objExcel->Workbooks->Add();
# Powizanie z arkuszem.
$objSheet = $objExcel->ActiveWorkbook->Worksheets(1);

$objSheet->{Name} = 'Procesy';
# Wypenienie komrek arkusza atrybutami uytkownika.
$objSheet->Cells(1, 1)->{Value} = 'Nazwa procesu';
$objSheet->Cells(1, 2)->{Value} = 'Wiersz polece';
$objSheet->Cells(1, 3)->{Value} = 'Identyfikator procesu';
$objSheet->Cells(1, 4)->{Value} = 'Waciciel';
$objSheet->Range('A1:D1')->Font->{Bold} = 1;
# Uzyskanie informacji dotyczcych procesu
$objWMI = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\cimv2');
$intProcessCount = 1;
foreach my $objProcess (in $objWMI->InstancesOf('Win32_Process')) {
    # Zapisanie w arkuszu dla kadego procesu jego nazwy, 
    # parametrw wiersza polece i identyfikatora
    $intProcessCount = $intProcessCount + 1;
    $objSheet->Cells($intProcessCount, 1)->{Value} = $objProcess->Name;
    $objSheet->Cells($intProcessCount, 2)->{Value} = $objProcess->CommandLine;
    $objSheet->Cells($intProcessCount, 3)->{Value} = $objProcess->ProcessID;
    $objProcess->GetOwner($strUser, $strDomain);
    $objSheet->Cells($intProcessCount, 4)->{Value} = $strDomain . '\\' . $strUser;
}
# Formatowanie kolumn
$objExcel->Columns(1)->{ColumnWidth} = 20;
$objExcel->Columns(2)->{ColumnWidth} = 50;
$objExcel->Columns(3)->{ColumnWidth} = 5;
$objExcel->Columns(4)->{ColumnWidth} = 30;
# Zapisanie arkusza, zamknicie skoroszytu i programu.
$objExcel->ActiveWorkbook->SaveAs($strExcelPath);
$objExcel->ActiveWorkbook->Close();
$objExcel->Application->Quit();
print "Gotowe\n";

