Freitag, 25. Dezember 2009

Compare-Object works different

Sorry Mr. Snover. I don't grock the way Compare-Object is intented to work.

Or I'm just misinterpreting the examples in http://technet.microsoft.com/en-us/library/ee156812.aspx for a second time.

The first time I was led to think, that Compare-Object is a good basis to produce diffs of text files. It hardly is capable to tell whether they are equal or different.

This time I thought it adequat to determine the processes which where started between two calls of GET-Process.
Well it correctly tells the name of the processes involved, but having a lot of chrome processes
(Chrome Plus to be precise) I do not get the PID of correct chrome process stopped.
And for me a process is it primary it's PID and not it's name.

With a little work around I get Compare-Object to work here as expected.


function Get-ProcessProperty ( )            
{
[CmdletBinding()]
PARAM(
$process = '*',
$timestamp = "{0:T}" -f (Get-Date)
)

$hash1 = @{}
Get-Process -Name $process | % {
$hash2 = @{}
#$hash2['Time'] = $timestamp
$hash2['Name'] = $_.name
#$hash2['CPU'] = $_.CPU
#$hash2['PM'] = "{0,3:f0}" -f ( $_.PM / 1MB )
#$hash2['NPM'] = "{0,4:f1}" -f ( $_.NPM / 1KB )
#... add further properties above
$hash1[($_.Id)] = $hash2
}
$hash1
}


$psold = get-ProcessProperty

(
'\PhysicalDisk(_total)\Current Disk Queue Length',
'\PhysicalDisk(_total)\Disk Transfers/sec'
) | Get-Counter -cont -sample 1 |% {
$timestamp = "{0:T}" -f $_.Timestamp

$diskQueue = ($_.CounterSamples[0]).CookedValue
$transfer = ($_.CounterSamples[1]).CookedValue

$msg = "{0} {1,4:f0} {2,4:f0}" -f $timestamp, $diskQueue, $transfer

$psnew = get-ProcessProperty

# Show Processes that started or stopped
Compare-Object @($psold.keys) @($psnew.keys) |%{
$Id = $_.inputObject
if ($_.Sideindicator -eq '=>')
{
$Name = ($psnew[$id]).Name
"$timestamp Start $Id $Name"
}
if ($_.Sideindicator -eq '<=')
{
$Name = ($psold[$id]).Name
"$timestamp $Id $Name stop"
}
}
$psold = $psnew

# the following code is special to a problem on my Windows 7 installation
# my problem is, when Current Disk Queue Length > 0, but Disk Transfers/sec 0
if ($diskQueue -gt 0 -and $transfer -eq 0)
{
$msg
}
}

Keine Kommentare:

Kommentar veröffentlichen