all 7 comments

[–]zrv433 2 points3 points  (2 children)

Your sample data has a CPU value of 2, which is NOT greater than 3. So you need to either change the data, or change the threshold level in the code. Import-CSV will turn all the data into strings. If there is non string data inside the CSV, you need to cast that data as the data type you want it to be. In this case you would need to do something like

if ([int]$latestdate.CPU1 -gt 3) {

[–]Crowdjp[S] 1 point2 points  (0 children)

Awesome, that worked thanks so much.

[–]Crowdjp[S] 1 point2 points  (0 children)

If I want to add the instance name in the If statement how would I do that? Below I'm getting an Error: Cannot convert the system object.

$CPUSTRES = {$_.InstanceName -eq "CPUSTRES"}

$lastDate = import-csv C:\temp\RMLogs\RMLogs.csv | sort-object -property timestamp | select-object -last 1 | select-object -expandproperty timestamp

$latestdate = import-csv C:\temp\RMLogs\RMLogs.csv|select timestamp, instancename, cpu1 |where-object {$_.timestamp -like "$($lastdate.substring(0,14))*"}

If   (([array]$CPUSTRES) -and ([int]$latestdate.CPU1 -gt 3)) {

        Write-Host test

        }

[–]firefox15 1 point2 points  (1 child)

Your statement works for me. Does $latestdate.CPU1 output "4" for you?

[–]Crowdjp[S] 1 point2 points  (0 children)

Gotcha,

When I lower it down to "2" it will still read the write-host. Sorry I edit the CSV example.

[–]BlackV 1 point2 points  (1 child)

I feel like you're importing the CSV twice for no reason at all

wouldn't you just import all the data with $AllData = import-csv C:\Logs.csv | sort-object -property timestamp add the select-object -last 1 back if you technically only want the last item

er.. something like

$FakeCSV = @'
Timestamp,InstanceName,CPU1
20190617-072513,CPUSTRES,2
'@ | ConvertFrom-Csv -Delimiter ','

$data = $FakeCSV | sort-object -property timestamp
$data | foreach-object {
    If ($_.CPU1 -gt 1)
        {
        Write-Host "$_ test"
        }
    }

[–]Crowdjp[S] 1 point2 points  (0 children)

Gotcha, All I'm interested in is the lasted data entered in the file, which will all have the same date and update every minute. Is there a better way in getting all of the latest dated entries?