# This script uses WMI to generate a process list from remote machines. use Win32::OLE qw( in ); use Win32::OLE::Variant; $User = new Win32::OLE::Variant( VT_BYREF | VT_BSTR, "" ); $Domain = new Win32::OLE::Variant( VT_BYREF | VT_BSTR, "" ); $Machine = "myterminalserver.domain.ext" unless( $Machine = shift @ARGV ); $Machine =~ s#^[\\/]+## if( $ARGV[0] =~ m#^[\\/]{2}# ); # This is the WMI moniker that will connect to a machine's # CIM (Common Information Model) repository $CLASS = "WinMgmts:{impersonationLevel=impersonate}!//$Machine"; # Get the WMI (Microsoft's implementation of WBEM) interface $WMI = Win32::OLE->GetObject( $CLASS ) || die "Unable to connect to \\$Machine:" . Win32::OLE->LastError(); # Get the collection of Win32_Process objects $ProcList = $WMI->InstancesOf( "Win32_Process" ); $~ = PROCESS_HEADER; write; $~ = PROCESS_INFO; # Cycle through each Win32_Process object # and write out its details... foreach $Proc ( in( $ProcList ) ) { $status = $Proc->GetOwner($User,$Domain); write; } sub SortProcs { lc $a->{Name} cmp lc $b->{Name}; } sub FormatNumber { my( $Number ) = @_; my( $Suffix ) = ""; my $K = 1024; my $M = 1024 * $K; if( $M <= $Number ) { $Suffix = "M"; $Number /= $M; } elsif( $K <= $Number ) { $Suffix = "K"; $Number /= $K; } $Number =~ s/(\.\d{0,2})\d*$/$1/; {} while ($Number =~ s/^(-?\d+)(\d{3})/$1,$2/); return( $Number . $Suffix ); } sub FormatDate { my( $Date ) = @_; $Date =~ s/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2}).*/$1.$2.$3 $4:$5:$6/; return( $Date ); } format PROCESS_HEADER = @||| @||||| @||||||||||||||| PID, "User Name", "CPU Time" ---- ------------- --------- . format PROCESS_INFO = @||| @||||| @<<<<<<< $Proc->{'ProcessID'}, $User, $Proc->{'UserModeTime'}*0.0000001 .