Print the running processes to a file (Perl)

This code can be found in Chapter 1 of Windows Server Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# 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;
# ------ SCRIPT CONFIGURATION ------
$strFile = 'c:\output.txt'; # e.g. c:\output.txt
# ------ END CONFIGURATION ---------
use constant ForWriting => 2;
$objFSO = Win32::OLE->new('Scripting.FileSystemObject');
$objFile = $objFSO->OpenTextFile($strFile, ForWriting, 1);
$objFile->WriteLine('Script started: ' . scalar localtime);
$objFile->WriteLine('List of processes:');
$objWMI = Win32::OLE->GetObject('winmgmts:root\cimv2');
foreach my $objProcess (in $objWMI->InstancesOf('Win32_Process')) {
    $objFile->WriteLine("\t" . $objProcess->Name);
}
$objFile->WriteLine('Script completed: ' . scalar localtime);
$objFile->Close();

This code has been viewed 1345 times.

New from the creators of TechTasks.com: StatSheet.com