# Adapted from VBScript code contained in the book:
# "Windows Server Cookbook" by Robbie Allen
# ISBN: 0-596-00633-0
use Win32::OLE 'in';
$Win32::OLE::Warn = 3;
$Win32::OLE::Warn = 3;
# ------ SCRIPT CONFIGURATION ------
$strComputer = '.';
$strExcelPath = 'd:\\procs.xls';
# ------ END CONFIGURATION ---------
$objExcel = Win32::OLE->new('Excel.Application');
if (Win32::OLE::LastError()) {
print "Excel application not installed.\n";
exit 0;
}
# Create a new workbook.
$objExcel->Workbooks->Add();
# Bind to worksheet.
$objSheet = $objExcel->ActiveWorkbook->Worksheets(1);
$objSheet->{Name} = 'Processes';
# Populate spreadsheet cells with user attributes.
$objSheet->Cells(1, 1)->{Value} = 'Process Name';
$objSheet->Cells(1, 2)->{Value} = 'Command Line';
$objSheet->Cells(1, 3)->{Value} = 'PID';
$objSheet->Cells(1, 4)->{Value} = 'Owner';
$objSheet->Range('A1:D1')->Font->{Bold} = 1;
# Query process information
$objWMI = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\cimv2');
$intProcessCount = 1;
foreach my $objProcess (in $objWMI->InstancesOf('Win32_Process')) {
# For each process, write the name, command-line options and process ID
# to the spreadsheet
$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;
}
# This formats the columns
$objExcel->Columns(1)->{ColumnWidth} = 20;
$objExcel->Columns(2)->{ColumnWidth} = 50;
$objExcel->Columns(3)->{ColumnWidth} = 5;
$objExcel->Columns(4)->{ColumnWidth} = 30;
# Save the spreadsheet, close the workbook and exit.
$objExcel->ActiveWorkbook->SaveAs($strExcelPath);
$objExcel->ActiveWorkbook->Close();
$objExcel->Application->Quit();
print "Done\n";
|